Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some compilers into Component
Browse files Browse the repository at this point in the history
loic425 committed Oct 17, 2023
1 parent 9d30aec commit b455485
Showing 21 changed files with 577 additions and 274 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ parameters:

excludePaths:
- %currentWorkingDirectory%/src/Bundle/Controller/*
- %currentWorkingDirectory%/src/Bundle/DependencyInjection/Compiler/*
- %currentWorkingDirectory%/src/Bundle/DependencyInjection/Configuration.php
- %currentWorkingDirectory%/src/Bundle/DependencyInjection/PagerfantaConfiguration.php
- %currentWorkingDirectory%/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineODMDriver.php
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -183,6 +183,7 @@

<TypeDoesNotContainType>
<errorLevel type="suppress">
<directory name="src/Bundle/DependencyInjection/Compiler" />
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
</TypeDoesNotContainType>
17 changes: 3 additions & 14 deletions src/Bundle/DependencyInjection/Compiler/CsrfTokenManagerPass.php
Original file line number Diff line number Diff line change
@@ -13,21 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\CsrfTokenManagerPass::class);

/**
* TODO Remove on sylius/resource-bundle 2.0
*/
final class CsrfTokenManagerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class CsrfTokenManagerPass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\CsrfTokenManagerPass
{
if (!$container->hasDefinition('security.csrf.token_manager')) {
return;
}

$csrdTokenManagerDefinition = $container->getDefinition('security.csrf.token_manager');
$csrdTokenManagerDefinition->setPublic(true);
}
}
72 changes: 3 additions & 69 deletions src/Bundle/DependencyInjection/Compiler/PagerfantaBridgePass.php
Original file line number Diff line number Diff line change
@@ -13,76 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\PagerfantaBridgePass::class);

/**
* Compiler pass to bridge the configuration from WhiteOctoberPagerfantaBundle to BabDevPagerfantaBundle
*
* @internal
*/
final class PagerfantaBridgePass implements CompilerPassInterface
{
public function __construct(private bool $internalUse = false)
{
}

public function process(ContainerBuilder $container): void
{
if (false === $this->internalUse) {
trigger_deprecation(
'sylius/resource-bundle',
'1.7',
'The "%s" class is deprecated. Migrate your Pagerfanta configuration from WhiteOctoberPagerfantaBundle to BabDevPagerfantaBundle, the configuration bridge will be removed in 2.0.',
self::class,
);
}

$this->changeViewFactoryClass($container);
$this->aliasRenamedServices($container);
}

private function changeViewFactoryClass(ContainerBuilder $container): void
{
if (!$container->hasParameter('white_october_pagerfanta.view_factory.class') || !$container->hasDefinition('pagerfanta.view_factory')) {
return;
}

/** @var string $viewFactoryClass */
$viewFactoryClass = $container->getParameter('white_october_pagerfanta.view_factory.class');

$container->getDefinition('pagerfanta.view_factory')
->setClass($viewFactoryClass)
;
}

private function aliasRenamedServices(ContainerBuilder $container): void
if (false) {
final class PagerfantaBridgePass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\PagerfantaBridgePass
{
$setDeprecatedMethod = (new \ReflectionClass(Alias::class))->getMethod('setDeprecated');

if ($container->hasDefinition('pagerfanta.twig_extension')) {
if (2 === $setDeprecatedMethod->getNumberOfParameters()) {
$container->setAlias('twig.extension.pagerfanta', 'pagerfanta.twig_extension')
->setDeprecated(true, 'The "%alias_id%" service alias is deprecated since Sylius 1.8, use the "pagerfanta.twig_extension" service ID instead.')
;
} else {
$container->setAlias('twig.extension.pagerfanta', 'pagerfanta.twig_extension')
->setDeprecated('sylius/resource-bundle', '1.8', 'The "%alias_id%" service alias is deprecated since Sylius 1.8, use the "pagerfanta.twig_extension" service ID instead.')
;
}
}

if ($container->hasDefinition('pagerfanta.view_factory')) {
if (2 === $setDeprecatedMethod->getNumberOfParameters()) {
$container->setAlias('white_october_pagerfanta.view_factory', 'pagerfanta.view_factory')
->setDeprecated(true, 'The "%alias_id%" service alias is deprecated since Sylius 1.8, use the "pagerfanta.view_factory" service ID instead.')
;
} else {
$container->setAlias('white_october_pagerfanta.view_factory', 'pagerfanta.view_factory')
->setDeprecated('sylius/resource-bundle', '1.8', 'The "%alias_id%" service alias is deprecated since Sylius 1.8, use the "pagerfanta.view_factory" service ID instead.')
;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -13,67 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\PrioritizedCompositeServicePass::class);

abstract class PrioritizedCompositeServicePass implements CompilerPassInterface
{
private string $serviceId;

private string $compositeId;

private string $tagName;

private string $methodName;

public function __construct(string $serviceId, string $compositeId, string $tagName, string $methodName)
{
$this->serviceId = $serviceId;
$this->compositeId = $compositeId;
$this->tagName = $tagName;
$this->methodName = $methodName;
}

public function process(ContainerBuilder $container): void
{
if (!$container->has($this->compositeId)) {
return;
}

$this->injectTaggedServicesIntoComposite($container);
$this->addAliasForCompositeIfServiceDoesNotExist($container);
}

private function injectTaggedServicesIntoComposite(ContainerBuilder $container): void
{
$contextDefinition = $container->findDefinition($this->compositeId);

$taggedServices = $container->findTaggedServiceIds($this->tagName);
foreach ($taggedServices as $id => $tags) {
$this->addMethodCalls($contextDefinition, $id, $tags);
}
}

private function addAliasForCompositeIfServiceDoesNotExist(ContainerBuilder $container): void
{
if ($container->has($this->serviceId)) {
return;
}

$container->setAlias($this->serviceId, $this->compositeId)->setPublic(true);
}

private function addMethodCalls(Definition $contextDefinition, string $id, array $tags): void
{
foreach ($tags as $attributes) {
$this->addMethodCall($contextDefinition, $id, $attributes);
}
}

private function addMethodCall(Definition $contextDefinition, string $id, array $attributes): void
if (false) {
abstract class PrioritizedCompositeServicePass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\PrioritizedCompositeServicePass
{
$contextDefinition->addMethodCall($this->methodName, [new Reference($id), $attributes['priority'] ?? 0]);
}
}
Original file line number Diff line number Diff line change
@@ -13,28 +13,11 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterFormBuilderPass::class);

final class RegisterFormBuilderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class RegisterFormBuilderPass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterFormBuilderPass
{
if (!$container->hasDefinition('sylius.registry.form_builder')) {
return;
}

$registry = $container->findDefinition('sylius.registry.form_builder');

foreach ($container->findTaggedServiceIds('sylius.default_resource_form.builder') as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['type'])) {
throw new \InvalidArgumentException('Tagged grid drivers needs to have "type" attribute.');
}

$registry->addMethodCall('register', [$attribute['type'], new Reference($id)]);
}
}
}
}

Original file line number Diff line number Diff line change
@@ -13,30 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourceRepositoryPass::class);

final class RegisterResourceRepositoryPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class RegisterResourceRepositoryPass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourceRepositoryPass
{
if (!$container->hasParameter('sylius.resources') || !$container->has('sylius.registry.resource_repository')) {
return;
}

/** @var array $resources */
$resources = $container->getParameter('sylius.resources');

$repositoryRegistry = $container->findDefinition('sylius.registry.resource_repository');

foreach ($resources as $alias => $configuration) {
[$applicationName, $resourceName] = explode('.', $alias, 2);
$repositoryId = sprintf('%s.repository.%s', $applicationName, $resourceName);

if ($container->has($repositoryId)) {
$repositoryRegistry->addMethodCall('register', [$alias, new Reference($repositoryId)]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -13,39 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass::class);

final class RegisterResourceStateMachinePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class RegisterResourceStateMachinePass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass
{
if (!$container->hasParameter('sylius.resources')) {
return;
}

/** @var array $resources */
$resources = $container->getParameter('sylius.resources');

foreach ($resources as $alias => $configuration) {
[$applicationName, $resourceName] = explode('.', $alias, 2);
$stateMachineId = sprintf('%s.controller_state_machine.%s', $applicationName, $resourceName);

$stateMachineComponent = $configuration['state_machine_component'] ?? null;

if (null === $stateMachineComponent) {
$container->setAlias($stateMachineId, 'sylius.resource_controller.state_machine');

continue;
}

$specificStateMachineId = sprintf('sylius.resource_controller.state_machine.%s', $stateMachineComponent);

if (!$container->hasDefinition($specificStateMachineId)) {
throw new \LogicException(sprintf('State machine "%s" is not available.', $stateMachineComponent));
}

$container->setAlias($stateMachineId, $specificStateMachineId);
}
}
}
38 changes: 4 additions & 34 deletions src/Bundle/DependencyInjection/Compiler/RegisterResourcesPass.php
Original file line number Diff line number Diff line change
@@ -13,40 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourcesPass::class);

final class RegisterResourcesPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class RegisterResourcesPass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\RegisterResourcesPass
{
try {
/** @var array $resources */
$resources = $container->getParameter('sylius.resources');
$registry = $container->findDefinition('sylius.resource_registry');
} catch (InvalidArgumentException $exception) {
return;
}

foreach ($resources as $alias => $configuration) {
$this->validateSyliusResource($configuration['classes']['model']);
$registry->addMethodCall('addFromAliasAndConfiguration', [$alias, $configuration]);
}
}

private function validateSyliusResource(string $class): void
{
/** @var array $interfaces */
$interfaces = class_implements($class);

if (!in_array(ResourceInterface::class, $interfaces, true)) {
throw new InvalidArgumentException(sprintf(
'Class "%s" must implement "%s" to be registered as a Sylius resource.',
$class,
ResourceInterface::class,
));
}
}
}
}
17 changes: 3 additions & 14 deletions src/Bundle/DependencyInjection/Compiler/TwigPass.php
Original file line number Diff line number Diff line change
@@ -13,21 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
\class_exists(\Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\TwigPass::class);

/**
* TODO Remove on sylius/resource-bundle 2.0
*/
final class TwigPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
if (false) {
final class TwigPass extends \Sylius\Component\Resource\Symfony\Bundle\DependencyInjection\Compiler\TwigPass
{
if (!$container->hasDefinition('twig')) {
return;
}

$twigDefinition = $container->getDefinition('twig');
$twigDefinition->setPublic(true);
}
}
Loading

0 comments on commit b455485

Please sign in to comment.