Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian-Merle committed Dec 13, 2024
1 parent 9233c4e commit f8896b1
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 106 deletions.
17 changes: 17 additions & 0 deletions src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
use Sylius\Resource\Annotation\AsExpressionProvider;
use Sylius\Resource\Annotation\AsExpressionVariables;
use Sylius\Resource\Factory\Factory;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Metadata\AsResource;
Expand All @@ -33,6 +35,7 @@
use Sylius\Resource\Twig\Context\Factory\ContextFactoryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand Down Expand Up @@ -100,6 +103,20 @@ public function load(array $configs, ContainerBuilder $container): void
$container->addObjectResource(DoctrineORMDriver::class);
$container->addObjectResource(DoctrineODMDriver::class);
$container->addObjectResource(DoctrinePHPCRDriver::class);

$container->registerAttributeForAutoconfiguration(
AsExpressionVariables::class,
static function (ChildDefinition $definition, AsExpressionVariables $attribute, \Reflector $reflector): void {
$definition->addTag(sprintf(AsExpressionVariables::BASE_SERVICE_TAG, $attribute->name));
},
);

$container->registerAttributeForAutoconfiguration(
AsExpressionProvider::class,
static function (ChildDefinition $definition, AsExpressionProvider $attribute, \Reflector $reflector): void {
$definition->addTag(sprintf(AsExpressionProvider::BASE_SERVICE_TAG, $attribute->name));
},
);
}

public function getConfiguration(array $config, ContainerBuilder $container): Configuration
Expand Down
77 changes: 53 additions & 24 deletions src/Bundle/Resources/config/services/expression_language.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,77 @@

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.resource_factory.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
<service id="sylius.repository.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
<service id="sylius.routing.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
<service id="sylius.resource_factory.expression_language_factory" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionLanguageFactory">
<argument type="tagged_iterator" tag="sylius.resource_factory.expression_language.provider" />
</service>

<service id="sylius.expression_language.variables.token" class="Sylius\Resource\Symfony\ExpressionLanguage\TokenVariables">
<argument type="service" id="security.token_storage" on-invalid="null" />
<tag name="sylius.resource_factory_variables" />
<tag name="sylius.repository_variables" />
<service id="sylius.resource_factory.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage">
<factory service="sylius.resource_factory.expression_language_factory" />
</service>

<service id="sylius.expression_language.variables.request" class="Sylius\Resource\Symfony\ExpressionLanguage\RequestVariables">
<argument type="service" id="request_stack" />
<tag name="sylius.resource_factory_variables" />
<tag name="sylius.repository_variables" />
<tag name="sylius.routing_variables" />
<service id="sylius.repository.expression_language_factory" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionLanguageFactory">
<argument type="tagged_iterator" tag="sylius.repository.expression_language.provider" />
</service>

<service id="sylius.expression_language.variables_collection.factory" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollection">
<argument type="tagged_iterator" tag="sylius.resource_factory_variables" />
<service id="sylius.repository.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage">
<factory service="sylius.repository.expression_language_factory" />
</service>

<service id="sylius.expression_language.variables_collection.repository" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollection">
<argument type="tagged_iterator" tag="sylius.repository_variables" />
<service id="sylius.routing.expression_language_factory" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionLanguageFactory">
<argument type="tagged_iterator" tag="sylius.routing.expression_language.provider" />
</service>

<service id="sylius.expression_language.variables_collection.routing" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollection">
<argument type="tagged_iterator" tag="sylius.routing_variables" />
<service id="sylius.routing.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage">
<factory service="sylius.routing.expression_language_factory" />
</service>

<service id="sylius.expression_language.argument_parser.factory" class="Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParser">


<service id="sylius.resource_factory.expression_evaluator" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionEvaluator">
<argument type="service" id="sylius.resource_factory.expression_language" />
<argument type="service" id="sylius.expression_language.variables_collection.factory" />
<argument type="service" id="sylius.resource_factory.variables_collection_aggregate" />
</service>

<service id="sylius.expression_language.argument_parser.repository" class="Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParser">
<service id="sylius.repository.expression_evaluator" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionEvaluator">
<argument type="service" id="sylius.repository.expression_language" />
<argument type="service" id="sylius.expression_language.variables_collection.repository" />
<argument type="service" id="sylius.repository.variables_collection_aggregate" />
</service>

<service id="sylius.expression_language.argument_parser.routing" class="Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParser">
<service id="sylius.routing.expression_evaluator" class="Sylius\Resource\Symfony\ExpressionLanguage\ExpressionEvaluator">
<argument type="service" id="sylius.routing.expression_language" />
<argument type="service" id="sylius.expression_language.variables_collection.routing" />
<argument type="service" id="sylius.routing.variables_collection_aggregate" />
</service>




<service id="sylius.resource_factory.variables_collection_aggregate" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollectionAggregate">
<argument type="tagged_iterator" tag="sylius.resource_factory_variables" />
</service>

<service id="sylius.repository.variables_collection_aggregate" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollectionAggregate">
<argument type="tagged_iterator" tag="sylius.repository_variables" />
</service>

<service id="sylius.routing.variables_collection_aggregate" class="Sylius\Resource\Symfony\ExpressionLanguage\VariablesCollectionAggregate">
<argument type="tagged_iterator" tag="sylius.routing_variables" />
</service>





<service id="sylius.expression_language.variables.token" class="Sylius\Resource\Symfony\ExpressionLanguage\TokenVariables">
<argument type="service" id="security.token_storage" on-invalid="null" />
<tag name="sylius.resource_factory_variables" />
<tag name="sylius.repository_variables" />
</service>

<service id="sylius.expression_language.variables.request" class="Sylius\Resource\Symfony\ExpressionLanguage\RequestVariables">
<argument type="service" id="request_stack" />
<tag name="sylius.resource_factory_variables" />
<tag name="sylius.repository_variables" />
<tag name="sylius.routing_variables" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

<service id="sylius.routing.redirect_handler" class="Sylius\Resource\Symfony\Routing\RedirectHandler">
<argument type="service" id="router" />
<argument type="service" id="sylius.expression_language.argument_parser.routing" />
<argument type="service" id="sylius.routing.expression_evaluator" />
<argument type="service" id="sylius.routing.factory.operation_route_name_factory" />
</service>
<service id="Sylius\Resource\Symfony\Routing\RedirectHandlerInterface" alias="sylius.routing.redirect_handler" />
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services/state.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<service id="Sylius\Resource\Symfony\Request\State\Provider">
<argument type="tagged_locator" tag="sylius.repository" />
<argument type="service" id="sylius.repository_argument_resolver.request" />
<argument type="service" id="sylius.expression_language.argument_parser.repository" />
<argument type="service" id="sylius.repository.expression_evaluator" />
<tag name="sylius.state_provider" />
</service>

Expand Down
25 changes: 25 additions & 0 deletions src/Component/src/Annotation/AsExpressionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Annotation;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsExpressionProvider
{
public const BASE_SERVICE_TAG = 'sylius.%s.expression_language.provider';

public function __construct(
public readonly string $name,
) {
}
}
25 changes: 25 additions & 0 deletions src/Component/src/Annotation/AsExpressionVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Annotation;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsExpressionVariables
{
public const BASE_SERVICE_TAG = 'sylius.%s_variables';

public function __construct(
public readonly string $name,
) {
}
}
6 changes: 3 additions & 3 deletions src/Component/src/State/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Sylius\Resource\Factory\FactoryInterface as ResourceFactoryInterface;
use Sylius\Resource\Metadata\FactoryAwareOperationInterface;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParserInterface;
use Sylius\Resource\Symfony\ExpressionLanguage\ExpressionEvaluatorInterface;
use Webmozart\Assert\Assert;

/**
Expand All @@ -28,7 +28,7 @@ final class Factory implements FactoryInterface
{
public function __construct(
private ContainerInterface $locator,
private ArgumentParserInterface $argumentParser,
private ExpressionEvaluatorInterface $expressionEvaluator,
) {
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public function create(Operation $operation, Context $context): ?object
private function parseArgumentValues(array $arguments): array
{
foreach ($arguments as $key => $value) {
$arguments[$key] = $this->argumentParser->parseExpression($value);
$arguments[$key] = $this->expressionEvaluator->evaluateExpression($value);
}

return $arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
/**
* @experimental
*/
final class ArgumentParser implements ArgumentParserInterface
final class ExpressionEvaluator implements ExpressionEvaluatorInterface
{
public function __construct(
private ExpressionLanguage $expressionLanguage,
private VariablesCollectionInterface $variablesCollection,
) {
}

public function parseExpression(string $expression, array $variables = []): mixed
public function evaluateExpression(string $expression, array $variables = []): mixed
{
return $this->expressionLanguage->evaluate(
$expression,
array_merge(
$this->variablesCollection->getVariables(),
$this->variablesCollection->getCollection(),
$variables,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @experimental
*/
interface ArgumentParserInterface
interface ExpressionEvaluatorInterface
{
public function parseExpression(string $expression, array $variables = []): mixed;
public function evaluateExpression(string $expression, array $variables = []): mixed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Symfony\ExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Webmozart\Assert\Assert;

/**
* @experimental
*/
final class ExpressionLanguageFactory
{
/** @param iterable<int, ExpressionFunctionProviderInterface> $providers */
public function __construct(private $providers)
{
Assert::allIsInstanceOf($this->providers, ExpressionFunctionProviderInterface::class);
}

public function __invoke(): ExpressionLanguage
{
$expressionLanguage = new ExpressionLanguage();

foreach ($this->providers as $provider) {
$expressionLanguage->registerProvider($provider);
}

return $expressionLanguage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
/**
* @experimental
*/
final class RequestVariables implements VariablesInterface
final class RequestVariables implements VariablesCollectionInterface
{
public function __construct(private RequestStack $requestStack)
{
}

public function getVariables(): array
public function getCollection(): array
{
return [
'request' => $this->requestStack->getCurrentRequest(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
/**
* @experimental
*/
final class TokenVariables implements VariablesInterface
final class TokenVariables implements VariablesCollectionInterface
{
public function __construct(private ?TokenStorageInterface $tokenStorage = null)
{
}

public function getVariables(): array
public function getCollection(): array
{
if (null === $this->tokenStorage) {
throw new \LogicException('The "symfony/security-bundle" must be installed and configured to use the "token" & "user" attribute. Try running "composer require symfony/security-bundle"');
Expand Down

This file was deleted.

Loading

0 comments on commit f8896b1

Please sign in to comment.