Limenius / ReactBundle

Client and Server-side React.js rendering in a Symfony Bundle
MIT License
390 stars 53 forks source link

How to install in Symfony 3 #14

Closed enzolutions closed 8 years ago

enzolutions commented 8 years ago

I tried to install ReactBundle in Symfony 3.1.3 with the following command

$ composer require limenius/react-bundle:^0.9.0@dev

But twig function isn't available

 $ bin/console debug:twig | grep react
enzolutions commented 8 years ago

I forge to include in AppKernel

$bundles[] = new Limenius\ReactBundle\LimeniusReactBundle();
enzolutions commented 8 years ago

When I try to use the twig extension I got the following error

 [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
  The service "limenius_react.render_extension" has a dependency on a non-existent service "limenius_react.react_renderer".
enzolutions commented 8 years ago

I found the problem , if in my config.yml I set 'client_side"

limenius_react:
    # Other options are "server_side" and "client_side"
    default_rendering: "client_side"

The code https://github.com/Limenius/ReactBundle/blob/master/DependencyInjection/LimeniusReactExtension.php#L34

$serverSideEnabled = $config['default_rendering'];
        if (in_array($serverSideEnabled, array('both', 'server_side'))) {
            $serverSideMode = $config['serverside_rendering']['mode'];
            if ($serverSideMode == 'external_server') {
                if ($serverSocketPath = $config['serverside_rendering']['server_socket_path']) {
                    $container
                        ->getDefinition('limenius_react.external_react_renderer')
                        ->addMethodCall('setServerSocketPath', array($serverSocketPath))
                        ;
                }
                $renderer = $container->getDefinition('limenius_react.external_react_renderer');
            } else {
                if ($serverBundlePath = $config['serverside_rendering']['server_bundle_path']) {
                    $container
                        ->getDefinition('limenius_react.phpexecjs_react_renderer')
                        ->addMethodCall('setServerBundlePath', array($serverBundlePath))
                        ;
                }
                $renderer = $container->getDefinition('limenius_react.phpexecjs_react_renderer');
            }
            $container->setDefinition('limenius_react.react_renderer', $renderer);
        }

Tthrow the exception

 [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
  The service "limenius_react.render_extension" has a dependency on a non-existent service "limenius_react.react_renderer".
RuslanZavacky commented 8 years ago

Hey @nacmartin maybe you can suggest, what renderer should be in case of client_side? As its not related to Symfony3, but just to configuration of Bundle. When client_side is set, its not loading a renderer.

RuslanZavacky commented 8 years ago

I've added a fallback to phpexecjs renderer, and it seems to work, but I think it might be better to implement ability to pass on-invalid="null" in service definition.

<?php

namespace Limenius\ReactBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration.
 *
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
 */
class LimeniusReactExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('limenius_react.default_rendering', $config['default_rendering']);
        $container->setParameter('limenius_react.fail_loud', $config['serverside_rendering']['fail_loud']);
        $container->setParameter('limenius_react.trace', $config['serverside_rendering']['trace']);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
        $loader->load('twig.xml');

        $serverSideEnabled = $config['default_rendering'];
        if (in_array($serverSideEnabled, array('both', 'server_side'))) {
            $serverSideMode = $config['serverside_rendering']['mode'];
            if ($serverSideMode == 'external_server') {
                if ($serverSocketPath = $config['serverside_rendering']['server_socket_path']) {
                    $container
                        ->getDefinition('limenius_react.external_react_renderer')
                        ->addMethodCall('setServerSocketPath', array($serverSocketPath))
                        ;
                }
                $renderer = $container->getDefinition('limenius_react.external_react_renderer');
            } else {
                if ($serverBundlePath = $config['serverside_rendering']['server_bundle_path']) {
                    $container
                        ->getDefinition('limenius_react.phpexecjs_react_renderer')
                        ->addMethodCall('setServerBundlePath', array($serverBundlePath))
                        ;
                }
                $renderer = $container->getDefinition('limenius_react.phpexecjs_react_renderer');
            }
            $container->setDefinition('limenius_react.react_renderer', $renderer);

        } else {
            $renderer = $container->getDefinition('limenius_react.phpexecjs_react_renderer');
            $container->setDefinition('limenius_react.react_renderer', $renderer);
        }

    }
}
nacmartin commented 8 years ago

@enzolutions thanks for the report!

@RuslanZavacky I agree that your solution of on-ignore=null is good, thanks! 👍