From 7d771cfce21e25b137194ad75979c8b4c2bf5eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Jani=CC=81c=CC=8Cek?= Date: Wed, 24 Nov 2021 14:50:22 +0100 Subject: [PATCH] Feat(web-twig): Connect params into twig --- packages/web-twig/CHANGELOG.md | 2 ++ packages/web-twig/README.md | 24 ++++++++++-------- .../web-twig/src/Compiler/ComponentLexer.php | 14 ++++++++++- .../src/Compiler/ComponentTagCompiler.php | 15 ++++++++--- .../TwigComponentsExtension.php | 7 ++++-- packages/web-twig/src/Factory/TwigFactory.php | 25 +++++++++++++++++-- .../src/Resources/config/services.yml | 3 +++ .../tests/Factory/TwigFactoryTest.php | 19 +++++++++++--- 8 files changed, 88 insertions(+), 21 deletions(-) diff --git a/packages/web-twig/CHANGELOG.md b/packages/web-twig/CHANGELOG.md index b3cfc8d92e..9c6bd6c280 100644 --- a/packages/web-twig/CHANGELOG.md +++ b/packages/web-twig/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Add syntax example into README +- Fix connect yaml configuration into Twig ## 0.0.1 - 2021-11-24 - Initial release. diff --git a/packages/web-twig/README.md b/packages/web-twig/README.md index f4218f1e76..5b3fca0ad6 100644 --- a/packages/web-twig/README.md +++ b/packages/web-twig/README.md @@ -47,17 +47,21 @@ Configure parameters for this bundle. path_alias: 'ui-components' ``` -### Step 4 -Need to change the implementation of render component in controllers in symfony by Trait +## Usage +after this configuration it will be possible to use components in your symfony project syntax-like Html/JSX -```php -Some other content +``` -... -use Lmc\TwigComponentsBundle\Traits\RenderTrait; +or pure original implementation -class DefaultController extends AbstractController -{ - use RenderTrait; -... +```twig +{% embed "@ui-components/component-name.twig" with { props: { + attr: 'value' +}} %} + {% block content %} + Some other content + {% endblock %} +{% endembed %} ``` diff --git a/packages/web-twig/src/Compiler/ComponentLexer.php b/packages/web-twig/src/Compiler/ComponentLexer.php index 37dbf916fe..d38b68c898 100644 --- a/packages/web-twig/src/Compiler/ComponentLexer.php +++ b/packages/web-twig/src/Compiler/ComponentLexer.php @@ -4,12 +4,24 @@ namespace Lmc\TwigComponentsBundle\Compiler; +use Twig\Environment; use Twig\Lexer; use Twig\Source; use Twig\TokenStream; class ComponentLexer extends Lexer { + /** + * @var string + */ + private $twigPathAlias; + + public function __construct(Environment $env, array $options, string $twigPathAlias) + { + parent::__construct($env, $options); + $this->twigPathAlias = $twigPathAlias; + } + public function tokenize(Source $source): TokenStream { $preparsed = $this->preparse($source->getCode()); @@ -25,6 +37,6 @@ public function tokenize(Source $source): TokenStream protected function preparse(string $value): string { - return (new ComponentTagCompiler($value))->compile(); + return (new ComponentTagCompiler($value, $this->twigPathAlias))->compile(); } } diff --git a/packages/web-twig/src/Compiler/ComponentTagCompiler.php b/packages/web-twig/src/Compiler/ComponentTagCompiler.php index 05c22800aa..a639893f8b 100644 --- a/packages/web-twig/src/Compiler/ComponentTagCompiler.php +++ b/packages/web-twig/src/Compiler/ComponentTagCompiler.php @@ -10,11 +10,20 @@ */ class ComponentTagCompiler { + /** + * @var string + */ protected $source; - public function __construct(string $source) + /** + * @var string + */ + private $twigPathAlias; + + public function __construct(string $source, string $twigPathAlias) { $this->source = $source; + $this->twigPathAlias = $twigPathAlias; } public function compile(): string @@ -73,7 +82,7 @@ function (array $matches) { $attributes = $this->getAttributesFromAttributeString($matches['attributes']); $name = $matches[1]; - return '{% embed "@seduo-genome/twig/' . mb_strtolower($name) . ".twig\" with { props: $attributes } %}{% block content %}"; + return '{% embed "@' . $this->twigPathAlias . '/' . mb_strtolower($name) . ".twig\" with { props: $attributes } %}{% block content %}"; }, $value ); @@ -132,7 +141,7 @@ function (array $matches) { $attributes = $this->getAttributesFromAttributeString($matches['attributes']); $name = $matches[1]; - return '{% embed "@seduo-genome/twig/' . mb_strtolower($name) . ".twig\" with { props: $attributes } %}{% endembed %}"; + return '{% embed "@' . $this->twigPathAlias . '/' . mb_strtolower($name) . ".twig\" with { props: $attributes } %}{% endembed %}"; }, $value ); diff --git a/packages/web-twig/src/DependencyInjection/TwigComponentsExtension.php b/packages/web-twig/src/DependencyInjection/TwigComponentsExtension.php index 01c4cb0b79..10c517e0ae 100644 --- a/packages/web-twig/src/DependencyInjection/TwigComponentsExtension.php +++ b/packages/web-twig/src/DependencyInjection/TwigComponentsExtension.php @@ -14,6 +14,9 @@ */ class TwigComponentsExtension extends Extension { + public const PARAMETER_PATH = 'twig_components.path'; + public const PARAMETER_PATH_ALIAS = 'twig_components.path_alias'; + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); @@ -22,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container): void $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); - $container->setParameter('twig_components.path', $config['path']); - $container->setParameter('twig_components.path_alias', $config['path_alias']); + $container->setParameter(self::PARAMETER_PATH, $config['path']); + $container->setParameter(self::PARAMETER_PATH_ALIAS, $config['path_alias']); } } diff --git a/packages/web-twig/src/Factory/TwigFactory.php b/packages/web-twig/src/Factory/TwigFactory.php index 7e94489708..323ca4d874 100644 --- a/packages/web-twig/src/Factory/TwigFactory.php +++ b/packages/web-twig/src/Factory/TwigFactory.php @@ -6,6 +6,7 @@ use Lmc\TwigComponentsBundle\Compiler\ComponentLexer; use Twig\Environment; +use Twig\Loader\FilesystemLoader; class TwigFactory { @@ -14,14 +15,34 @@ class TwigFactory */ private $twig; - public function __construct(Environment $twig) + /** + * @var FilesystemLoader + */ + private $loader; + + /** + * @var string + */ + private $path; + + /** + * @var string + */ + private $alias; + + public function __construct(Environment $twig, FilesystemLoader $loader, string $path, string $alias) { $this->twig = $twig; + $this->loader = $loader; + $this->path = $path; + $this->alias = $alias; } public function create(): Environment { - $this->twig->setLexer(new ComponentLexer($this->twig)); + $this->loader->addPath($this->path, $this->alias); + $this->twig->setLoader($this->loader); + $this->twig->setLexer(new ComponentLexer($this->twig, [], $this->alias)); return $this->twig; } diff --git a/packages/web-twig/src/Resources/config/services.yml b/packages/web-twig/src/Resources/config/services.yml index 633423ddf5..1d9dfd15e7 100644 --- a/packages/web-twig/src/Resources/config/services.yml +++ b/packages/web-twig/src/Resources/config/services.yml @@ -6,6 +6,9 @@ services: Lmc\TwigComponentsBundle\Factory\TwigFactory: arguments: - '@twig' + - '@twig.loader' + - '%twig_components.path%' + - '%twig_components.path_alias%' Twig\Environment: factory: ['@Lmc\TwigComponentsBundle\Factory\TwigFactory', 'create'] \ No newline at end of file diff --git a/packages/web-twig/tests/Factory/TwigFactoryTest.php b/packages/web-twig/tests/Factory/TwigFactoryTest.php index d782c7f9f8..14ea0f887c 100644 --- a/packages/web-twig/tests/Factory/TwigFactoryTest.php +++ b/packages/web-twig/tests/Factory/TwigFactoryTest.php @@ -6,16 +6,29 @@ use Mockery; use PHPUnit\Framework\TestCase; use Twig\Environment; +use Twig\Loader\FilesystemLoader; final class TwigFactoryTest extends TestCase { public function testShouldCreate(): void { + $path = 'templates/'; + $pathAlias = 'ui-components'; + + $twigFilesystemLoaderMock = Mockery::mock(FilesystemLoader::class); + $twigFilesystemLoaderMock->shouldReceive('addPath') + ->once() + ->with($path, $pathAlias); + $twigEnvironmentMock = Mockery::mock(Environment::class); $twigEnvironmentMock->shouldReceive('setLexer') ->once() ->withArgs([ComponentLexer::class]); + $twigEnvironmentMock->shouldReceive('setLoader') + ->once() + ->with($twigFilesystemLoaderMock); + $twigEnvironmentMock->shouldReceive('getUnaryOperators') ->once() ->withNoArgs() @@ -26,9 +39,9 @@ public function testShouldCreate(): void ->withNoArgs() ->andReturn([]); - $twigFactory = new TwigFactory($twigEnvironmentMock); - $twigExtendendEnvironmentInstance = $twigFactory->create(); + $twigFactory = new TwigFactory($twigEnvironmentMock, $twigFilesystemLoaderMock, $path, $pathAlias); + $twigEnvironmentInstance = $twigFactory->create(); - $this->assertInstanceOf(Environment::class, $twigExtendendEnvironmentInstance); + $this->assertInstanceOf(Environment::class, $twigEnvironmentInstance); } }