Skip to content

Commit

Permalink
Feat(web-twig): Connect params into twig
Browse files Browse the repository at this point in the history
  • Loading branch information
janicekt authored and literat committed Jul 30, 2022
1 parent bfeba38 commit 7d771cf
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/web-twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<!-- There should always be "Unreleased" section at the beginning. -->
## Unreleased
- Add syntax example into README
- Fix connect yaml configuration into Twig

## 0.0.1 - 2021-11-24
- Initial release.
24 changes: 14 additions & 10 deletions packages/web-twig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
```html
<ComponentName attr="value">Some other content</ComponentName>
```

...
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 %}
```
14 changes: 13 additions & 1 deletion packages/web-twig/src/Compiler/ComponentLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
}
}
15 changes: 12 additions & 3 deletions packages/web-twig/src/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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']);
}
}
25 changes: 23 additions & 2 deletions packages/web-twig/src/Factory/TwigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Lmc\TwigComponentsBundle\Compiler\ComponentLexer;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class TwigFactory
{
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/web-twig/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
19 changes: 16 additions & 3 deletions packages/web-twig/tests/Factory/TwigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}

0 comments on commit 7d771cf

Please sign in to comment.