├── .gitignore ├── README.md ├── composer.json └── src ├── DependencyInjection ├── Configuration.php └── ReactjsPhpExtension.php ├── ReactJS.php ├── ReactjsPhpBundle.php ├── Resources └── config │ └── services.xml ├── Templating └── Helper │ └── ReactJSHelper.php └── Twig └── Extension └── ReactJSExtension.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | 3 | tests/app/cache 4 | tests/app/logs 5 | 6 | .DS_Store 7 | 8 | composer.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reactjs-php-bundle 2 | 3 | Render ReactJS components in server side using [reactjs/react-php-v8js](https://github.com/reactjs/react-php-v8js) 4 | 5 | ## Prerequisites 6 | 7 | [V8Js PHP extension](http://php.net/v8js) 8 | 9 | ## Installation 10 | 11 | ```bash 12 | composer require qpautrat/reactjs-php-bundle 13 | ``` 14 | 15 | Whitelist following library in require section until maintainers release a version on packagist 16 | 17 | ```json 18 | "reactjs/react-php-v8js": "*@dev" 19 | ``` 20 | 21 | Register the bundle in your `AppKernel` 22 | 23 | ```php 24 | new QPautrat\ReactjsPhpBundle\ReactjsPhpBundle() 25 | ``` 26 | 27 | ## Configuration 28 | 29 | Add in your `config.yml` file 30 | 31 | ```yaml 32 | reactjs_php: 33 | library_path: path_to_reactjs_library 34 | app_path: path_to_app_components 35 | ``` 36 | 37 | ## Usage 38 | 39 | Use helper with php engine 40 | 41 | ```php 42 | renderMarkup('Component', array('foo' => 'bar')) ?> 43 | renderJS('Component', '#dom_element', array('foo' => 'bar')) ?> 44 | ``` 45 | 46 | Or you can use our twig extension as well 47 | 48 | ```html 49 | {{ 'Component'|reactjs_render_markup({'foo':'bar'}) }} 50 | {{ 'Component'|reactjs_render_js('#component', {'foo':'bar'}) }} 51 | ``` 52 | 53 | ## Demo 54 | 55 | We provide a small demo application in another [repository](https://github.com/qpautrat/demo-reactjs-php-bundle), living in a docker container, that you can use to test. 56 | 57 | ## License 58 | 59 | This bundle is available under the MIT License -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qpautrat/reactjs-php-bundle", 3 | "description": "Render ReactJS component in server side using reactjs/react-php-v8js", 4 | "type": "symfony-bundle", 5 | "version": "1.0.0", 6 | "keywords": ["react", "reactjs", "php", "bundle", "symfony", "srr", "v8js"], 7 | "homepage": "https://github.com/qpautrat/reactjs-php-bundle", 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "Quentin Pautrat", 12 | "email": "quentin.pautrat@gmail.com" 13 | } 14 | ], 15 | "require": { 16 | "reactjs/react-php-v8js": "dev-master", 17 | "symfony/http-kernel": "~2.0", 18 | "symfony/templating": "~2.0", 19 | "symfony/dependency-injection": "~2.0", 20 | "symfony/config": "~2.0" 21 | }, 22 | "suggest": { 23 | "twig/twig": "Allow to use twig templating engine" 24 | }, 25 | "autoload": { 26 | "psr-4": { "QPautrat\\ReactjsPhpBundle\\": "src" } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Configuration implements ConfigurationInterface 12 | { 13 | public function getConfigTreeBuilder() 14 | { 15 | $treeBuilder = new TreeBuilder(); 16 | $rootNode = $treeBuilder->root('reactjs_php'); 17 | 18 | $rootNode 19 | ->children() 20 | ->scalarNode('library_path') 21 | ->isRequired() 22 | ->cannotBeEmpty() 23 | ->end() 24 | ->scalarNode('app_path') 25 | ->isRequired() 26 | ->cannotBeEmpty() 27 | ->end() 28 | ->end(); 29 | 30 | return $treeBuilder; 31 | } 32 | } -------------------------------------------------------------------------------- /src/DependencyInjection/ReactjsPhpExtension.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class ReactjsPhpExtension extends Extension 14 | { 15 | public function load(array $configs, ContainerBuilder $container) 16 | { 17 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 18 | $loader->load('services.xml'); 19 | 20 | $configuration = new Configuration(); 21 | $config = $this->processConfiguration($configuration, $configs); 22 | 23 | $container 24 | ->getDefinition('reactjs_php.reactjs') 25 | ->setArguments(array( 26 | $config['library_path'], 27 | $config['app_path'] 28 | )) 29 | ; 30 | } 31 | } -------------------------------------------------------------------------------- /src/ReactJS.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ReactJS extends BaseReactJS 11 | { 12 | /** 13 | * Overwrite constructor due to avoid this kind of stuff in Extension: str_replace('%', '%%', $library_path) 14 | */ 15 | public function __construct($libpath, $apppath) 16 | { 17 | parent::__construct(file_get_contents($libpath), file_get_contents($apppath)); 18 | } 19 | } -------------------------------------------------------------------------------- /src/ReactjsPhpBundle.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ReactjsPhpBundle extends Bundle 11 | { 12 | } -------------------------------------------------------------------------------- /src/Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | QPautrat\ReactjsPhpBundle\ReactJS 9 | QPautrat\ReactjsPhpBundle\Templating\Helper\ReactJSHelper 10 | QPautrat\ReactjsPhpBundle\Twig\Extension\ReactJSExtension 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Templating/Helper/ReactJSHelper.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class ReactJSHelper extends Helper 13 | { 14 | /** 15 | * @var ReactJS 16 | */ 17 | protected $reactJs; 18 | 19 | public function __construct(ReactJS $reactJs) 20 | { 21 | $this->reactJs = $reactJs; 22 | } 23 | 24 | /** 25 | * Render markup of given component. 26 | * 27 | * @param string $component Component name 28 | * @param array $props Optional. Array of properties 29 | * 30 | * @return string Markup 31 | */ 32 | public function renderMarkup($component, $props = null) 33 | { 34 | return $this->reactJs 35 | ->setComponent($component, $props) 36 | ->getMarkup() 37 | ; 38 | } 39 | 40 | /** 41 | * Render javascript of given component 42 | * 43 | * @param string $component Component name 44 | * @param string $element DOM element where to render component 45 | * @param array $props Optional. Array of properties 46 | * @param string $variable Optional. Name of variable on return 47 | * 48 | * @return string Javascript 49 | */ 50 | public function renderJS($component, $element, $props = null, $variable = null) 51 | { 52 | return $this->reactJs 53 | ->setComponent($component, $props) 54 | ->getJS($element, $variable) 55 | ; 56 | } 57 | 58 | public function getName() 59 | { 60 | return 'reactjs'; 61 | } 62 | } -------------------------------------------------------------------------------- /src/Twig/Extension/ReactJSExtension.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ReactJSExtension extends \Twig_Extension 11 | { 12 | /** 13 | * @var ReactJSHelper 14 | */ 15 | protected $helper; 16 | 17 | public function __construct(ReactJSHelper $helper) 18 | { 19 | $this->helper = $helper; 20 | } 21 | 22 | public function getFilters() 23 | { 24 | return array( 25 | 'reactjs_render_markup' => new \Twig_Filter_Method($this, 'renderMarkup', array('is_safe' => array('html'))), 26 | 'reactjs_render_js' => new \Twig_Filter_Method($this, 'renderJS', array('is_safe' => array('html'))) 27 | ); 28 | } 29 | 30 | public function renderMarkup($component, $props = null) 31 | { 32 | return $this->helper->renderMarkup($component, $props); 33 | } 34 | 35 | public function renderJS($component, $element, $props = null, $variable = null) 36 | { 37 | return $this->helper->renderJS($component, $element, $props, $variable); 38 | } 39 | 40 | public function getName() 41 | { 42 | return 'reactjs'; 43 | } 44 | } --------------------------------------------------------------------------------