-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shop] Introduce BC solution for deprecated and overwritten email man…
…agers
- Loading branch information
Showing
5 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...opBundle/DependencyInjection/Compiler/BackwardsCompatibility/ReplaceEmailManagersPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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\Bundle\ShopBundle\DependencyInjection\Compiler\BackwardsCompatibility; | ||
|
||
use Sylius\Bundle\ShopBundle\Controller\ContactController; | ||
use Sylius\Bundle\ShopBundle\EventListener\OrderCompleteListener; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** @internal */ | ||
final class ReplaceEmailManagersPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach ($container->getDefinitions() as $definition) { | ||
if ($definition->getDecoratedService() === null) { | ||
continue; | ||
} | ||
|
||
$decoratedServiceClass = $definition->getDecoratedService()[0]; | ||
|
||
if ($decoratedServiceClass === 'sylius.email_manager.contact') { | ||
$this->replaceArgument( | ||
$container, | ||
'sylius.controller.shop.contact', | ||
ContactController::class, | ||
6, | ||
'sylius.email_manager.contact', | ||
); | ||
|
||
continue; | ||
} | ||
|
||
if ($decoratedServiceClass === 'sylius.email_manager.order') { | ||
$this->replaceArgument( | ||
$container, | ||
'sylius.listener.order_complete', | ||
OrderCompleteListener::class, | ||
0, | ||
'sylius.email_manager.order', | ||
); | ||
} | ||
} | ||
} | ||
|
||
public function replaceArgument( | ||
ContainerBuilder $container, | ||
string $serviceId, | ||
string $serviceClass, | ||
int $argumentIndex, | ||
string $argumentId, | ||
): void { | ||
try { | ||
$listenerDefinition = $container->findDefinition($serviceId); | ||
if ($listenerDefinition->getClass() === $serviceClass) { | ||
$listenerDefinition->setArgument($argumentIndex, new Reference($argumentId)); | ||
} | ||
} catch (ServiceNotFoundException) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...ests/DependencyInjection/Compiler/BackwardsCompatibility/ReplaceEmailManagersPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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\Bundle\ShopBundle\Tests\DependencyInjection\Compiler\BackwardsCompatibility; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Sylius\Bundle\ShopBundle\DependencyInjection\Compiler\BackwardsCompatibility\ReplaceEmailManagersPass; | ||
use Sylius\Bundle\ShopBundle\EventListener\OrderCompleteListener; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class ReplaceEmailManagersPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
/** @test */ | ||
public function it_replaces_decorated_order_email_manager_service_of_order_complete_listener_arguments(): void | ||
{ | ||
$this->setDefinition('sylius.email_manager.order', new Definition()); | ||
$this->setDefinition('sylius.email_manager.order.decorated', (new Definition())->setDecoratedService('sylius.email_manager.order')); | ||
$this->setDefinition('sylius.mailer.order_email_manager.shop', new Definition()); | ||
$this->setDefinition('sylius.listener.order_complete', new Definition(OrderCompleteListener::class, [new Reference('sylius.mailer.order_email_manager.shop')])); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceDefinitionWithArgument( | ||
'sylius.listener.order_complete', | ||
0, | ||
'sylius.email_manager.order', | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_if_shipment_email_manager_service_is_not_decorated(): void | ||
{ | ||
$this->setDefinition('sylius.email_manager.order', new Definition()); | ||
$this->setDefinition('sylius.mailer.order_email_manager.shop', new Definition()); | ||
$this->setDefinition('sylius.listener.order_complete', new Definition(OrderCompleteListener::class, [new Reference('sylius.mailer.order_email_manager.shop')])); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceDefinitionWithArgument( | ||
'sylius.listener.order_complete', | ||
0, | ||
'sylius.mailer.order_email_manager.shop', | ||
); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new ReplaceEmailManagersPass()); | ||
} | ||
} |