Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TwigHooks] Implement autoprefixing feature #4

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^9.6",
"symfony/console": "^5.4 || ^6.0",
"symfony/debug-bundle": "^5.4 || ^6.0",
"symfony/dom-crawler": "^5.4 || ^6.0",
"symfony/dotenv": "^5.4 || ^6.0",
"symfony/flex": "^2.4",
Expand Down
1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
Sylius\TwigHooks\TwigHooksBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
];
5 changes: 5 additions & 0 deletions config/packages/debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
when@dev:
debug:
# Forwards VarDumper Data clones to a centralized server allowing to inspect dumps on CLI or in your browser.
# See the "server:dump" command to start a new server.
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
3 changes: 3 additions & 0 deletions config/packages/twig.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
twig:
default_path: '%kernel.project_dir%/templates'

twig_component:
anonymous_template_directory: 'components/'

when@test:
twig:
strict_variables: true
5 changes: 5 additions & 0 deletions config/packages/twig_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
twig_hooks:
hooks:
'app.base':
content:
template: 'base/content.html.twig'
1 change: 1 addition & 0 deletions monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
ComposerJsonSection::REQUIRE_DEV => [
'phpstan/phpstan' => '^1.10',
'symfony/debug-bundle' => '^5.4 || ^6.0',
'symfony/flex' => '^2.4',
'symfony/phpunit-bridge' => '^5.4 || ^6.0',
'symplify/monorepo-builder' => '11.2.*',
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/TwigHooks/src/Hookable/AbstractHookable.php
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon

Expand Down
8 changes: 4 additions & 4 deletions src/TwigHooks/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Sylius\TwigHooks\Hook\NameGenerator\TemplateNameGenerator;
use Sylius\TwigHooks\Provider\ComponentDataProvider;
use Sylius\TwigHooks\Provider\ComponentContextProvider;
use Sylius\TwigHooks\Provider\DefaultConfigurationProvider;
use Sylius\TwigHooks\Provider\DefaultDataProvider;
use Sylius\TwigHooks\Provider\DefaultContextProvider;
use Sylius\TwigHooks\Registry\HookablesRegistry;
use Sylius\TwigHooks\Twig\HooksExtension;
use Sylius\TwigHooks\Twig\Runtime\HooksRuntime;
Expand All @@ -16,9 +16,9 @@

$services = $configurator->services();

$services->set('twig_hooks.provider.default_data', DefaultDataProvider::class);
$services->set('twig_hooks.provider.default_context', DefaultContextProvider::class);

$services->set('twig_hooks.provider.component_data', ComponentDataProvider::class)
$services->set('twig_hooks.provider.component_data', ComponentContextProvider::class)
->args([
inline_service(ExpressionLanguage::class),
])
Expand Down
2 changes: 2 additions & 0 deletions src/TwigHooks/config/services/hook_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
->args([
service('twig_hooks.registry.hookables'),
service('twig_hooks.renderer.hookable'),
service('twig_hooks.provider.default_context'),
service('twig_hooks.provider.default_configuration'),
])
->alias(HookRendererInterface::class, 'twig_hooks.renderer.hook')
;
Expand Down
4 changes: 0 additions & 4 deletions src/TwigHooks/config/services/hookable_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@
$services->set('twig_hooks.renderer.hookable.component', HookableComponentRenderer::class)
->args([
service('ux.twig_component.component_renderer'),
service('twig_hooks.provider.component_data'),
service('twig_hooks.provider.default_configuration'),
])
->tag('twig_hooks.hookable_renderer')
;

$services->set('twig_hooks.renderer.hookable.template', HookableTemplateRenderer::class)
->args([
service('twig'),
service('twig_hooks.provider.default_data'),
service('twig_hooks.provider.default_configuration'),
])
->tag('twig_hooks.hookable_renderer')
;
Expand Down
6 changes: 1 addition & 5 deletions src/TwigHooks/src/DependencyInjection/TwigHooksExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new PhpFileLoader($container, new FileLocator(dirname(__DIR__, 2) . '/config'));
$loader->load('services.php');

if ($container->getParameter('kernel.debug')) {
if ($container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug')) {
$loader->load('services/debug/twig_events.php');
}

$configuration = $this->getConfiguration([], $container);
if ($configuration === null) {
return;
}

$config = $this->processConfiguration($configuration, $configs);

$this->registerHooks($container, $config['hooks'], $config['supported_hookable_types']);
Expand Down
16 changes: 16 additions & 0 deletions src/TwigHooks/src/Hook/Metadata/HookMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Metadata;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class HookMetadata
{
public function __construct(
public readonly string $name,
public readonly ParameterBagInterface $context,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct(private HookRendererInterface $innerRenderer)
{
}

public function render(array $hooksNames, array $data = []): string
public function render(array $hookNames, array $hookContext = []): string
{
$renderedParts = [];
$renderedParts[] = $this->getOpeningDebugComment($hooksNames);
$renderedParts[] = trim($this->innerRenderer->render($hooksNames, $data));
$renderedParts[] = $this->getClosingDebugComment($hooksNames);
$renderedParts[] = $this->getOpeningDebugComment($hookNames);
$renderedParts[] = trim($this->innerRenderer->render($hookNames, $hookContext));
$renderedParts[] = $this->getClosingDebugComment($hookNames);

return implode(PHP_EOL, $renderedParts);
}
Expand Down
10 changes: 5 additions & 5 deletions src/TwigHooks/src/Hook/Renderer/Debug/HookProfilerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function __construct (
) {
}

public function render(array $hooksNames, array $data = []): string
public function render(array $hookNames, array $hookContext = []): string
{
$this->profile?->registerHookStart($hooksNames);
$this->stopwatch?->start(md5(serialize($hooksNames)));
$this->profile?->registerHookStart($hookNames);
$this->stopwatch?->start(md5(serialize($hookNames)));

$rendered = $this->innerRenderer->render($hooksNames, $data);
$rendered = $this->innerRenderer->render($hookNames, $hookContext);

$this->profile?->registerHookEnd(
$this->stopwatch?->stop(md5(serialize($hooksNames)))->getDuration(),
$this->stopwatch?->stop(md5(serialize($hookNames)))->getDuration(),
);

return $rendered;
Expand Down
28 changes: 23 additions & 5 deletions src/TwigHooks/src/Hook/Renderer/HookRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,42 @@

namespace Sylius\TwigHooks\Hook\Renderer;

use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;
use Sylius\TwigHooks\Provider\ConfigurationProviderInterface;
use Sylius\TwigHooks\Provider\ContextProviderInterface;
use Sylius\TwigHooks\Registry\HookablesRegistry;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

final class HookRenderer implements HookRendererInterface
{
public function __construct(
private HookablesRegistry $hookablesRegistry,
private HookableRendererInterface $compositeHookableRenderer,
private readonly HookablesRegistry $hookablesRegistry,
private readonly HookableRendererInterface $compositeHookableRenderer,
private readonly ContextProviderInterface $contextProvider,
private readonly ConfigurationProviderInterface $configurationProvider,
) {
}

public function render(array|string $hooksNames, array $data = []): string
/**
* @param array<string> $hookNames
* @param array<string, mixed> $hookContext
*/
public function render(array $hookNames, array $hookContext = []): string
{
$hookables = $this->hookablesRegistry->getEnabledFor($hooksNames);
$hookables = $this->hookablesRegistry->getEnabledFor($hookNames);
$renderedHookables = [];

foreach ($hookables as $hookable) {
$renderedHookables[] = $this->compositeHookableRenderer->render($hookable, $data);
$hookMetadata = new HookMetadata($hookable->getHookName(), new ParameterBag($hookContext));

$context = $this->contextProvider->provide($hookable, $hookContext);
$configuration = $this->configurationProvider->provide($hookable);

$hookableMetadata = new HookableMetadata($hookMetadata, new ParameterBag($context), new ParameterBag($configuration), $hookNames);

$renderedHookables[] = $this->compositeHookableRenderer->render($hookable, $hookableMetadata);
}

return implode(PHP_EOL, $renderedHookables);
Expand Down
6 changes: 3 additions & 3 deletions src/TwigHooks/src/Hook/Renderer/HookRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
interface HookRendererInterface
{
/**
* @param array<string> $hooksNames
* @param array<string, mixed> $data
* @param array<string> $hookNames
* @param array<string, mixed> $hookContext
*/
public function render(array $hooksNames, array $data = []): string;
public function render(array $hookNames, array $hookContext = []): string;
}
32 changes: 32 additions & 0 deletions src/TwigHooks/src/Hookable/Metadata/HookableMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hookable\Metadata;

use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class HookableMetadata
{
/**
* @param array<string> $prefixes
*/
public function __construct(
public readonly HookMetadata $renderedBy,
public readonly ParameterBagInterface $context,
public readonly ParameterBagInterface $configuration,
public readonly array $prefixes = [],
) {
foreach ($prefixes as $prefix) {
if (!is_string($prefix)) {
throw new \InvalidArgumentException('Parent name must be a string.');
}
}
}

public function hasPrefixes(): bool
{
return count($this->prefixes) > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\TwigHooks\Hookable\Renderer;

use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\Exception\NoSupportedRendererException;

final class CompositeHookableRenderer implements HookableRendererInterface
Expand All @@ -28,11 +29,11 @@ public function __construct (iterable $renderers)
}
}

public function render(AbstractHookable $hookable, array $hookData = []): string
public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
{
foreach ($this->renderers as $renderer) {
if ($renderer->supports($hookable)) {
return $renderer->render($hookable, $hookData);
return $renderer->render($hookable, $metadata);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
namespace Sylius\TwigHooks\Hookable\Renderer\Debug;

use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;

final class HookableDebugCommentRenderer implements HookableRendererInterface
{
public function __construct(private HookableRendererInterface $innerRenderer)
public function __construct(private readonly HookableRendererInterface $innerRenderer)
{
}

public function render(AbstractHookable $hookable, array $hookData = []): string
public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
{
$renderedParts = [];
$renderedParts[] = $this->getOpeningDebugComment($hookable);
$renderedParts[] = trim($this->innerRenderer->render($hookable, $hookData));
$renderedParts[] = trim($this->innerRenderer->render($hookable, $metadata));
$renderedParts[] = $this->getClosingDebugComment($hookable);

return implode(PHP_EOL, $renderedParts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\TwigHooks\Hookable\Renderer\Debug;

use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;
use Sylius\TwigHooks\Profiler\Profile;
use Symfony\Component\Stopwatch\Stopwatch;
Expand All @@ -18,12 +19,12 @@ public function __construct (
) {
}

public function render(AbstractHookable $hookable, array $hookData = []): string
public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
{
$this->profile?->registerHookableRenderStart($hookable);
$this->stopwatch?->start($hookable->getId());

$rendered = $this->innerRenderer->render($hookable, $hookData);
$rendered = $this->innerRenderer->render($hookable, $metadata);

$this->profile?->registerHookableRenderEnd(
$this->stopwatch?->stop($hookable->getId())->getDuration(),
Expand Down
18 changes: 6 additions & 12 deletions src/TwigHooks/src/Hookable/Renderer/HookableComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,32 @@
namespace Sylius\TwigHooks\Hookable\Renderer;

use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\HookableComponent;
use Sylius\TwigHooks\Provider\ConfigurationProviderInterface;
use Sylius\TwigHooks\Provider\DataProviderInterface;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Symfony\UX\TwigComponent\ComponentRendererInterface;

final class HookableComponentRenderer implements SupportableHookableRendererInterface
{
public const HOOKABLE_CONFIGURATION_PARAMETER = 'hookableConfiguration';

public const HOOKABLE_DATA_PARAMETER = 'hookableData';

public function __construct(
private ComponentRendererInterface $componentRenderer,
private DataProviderInterface $dataProvider,
private ConfigurationProviderInterface $configurationProvider,
private readonly ComponentRendererInterface $componentRenderer,
) {
}

public function render(AbstractHookable $hookable, array $hookData = []): string
public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
{
if (!$this->supports($hookable)) {
throw new \InvalidArgumentException(
sprintf('Hookable must be the "%s" type, but "%s" given.', AbstractHookable::TYPE_COMPONENT, $hookable->getType()),
);
}

$data = $this->dataProvider->provide($hookable, $hookData);
$configuration = $this->configurationProvider->provide($hookable);
$context = $metadata->context->all();
$configuration = $metadata->configuration->all();

return $this->componentRenderer->createAndRender($hookable->getTarget(), [
self::HOOKABLE_CONFIGURATION_PARAMETER => $configuration,
...$data,
...$context,
]);
}

Expand Down
Loading
Loading