Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle][Routing] Add a new tag to be able to use a private …
…service as a service route loader
- Loading branch information
Showing
11 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
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
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
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Routing/DependencyInjection/ServiceRouterLoaderPass.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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderContainer; | ||
|
||
class ServiceRouterLoaderPass implements CompilerPassInterface | ||
{ | ||
private $loaderServiceServiceId; | ||
private $serviceRouterLoaderTag; | ||
|
||
public function __construct(string $loaderServiceServiceId = 'routing.loader.service', string $serviceRouterLoaderTag = 'routing.route_loader') | ||
{ | ||
$this->loaderServiceServiceId = $loaderServiceServiceId; | ||
$this->serviceRouterLoaderTag = $serviceRouterLoaderTag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition($this->loaderServiceServiceId)) { | ||
return; | ||
} | ||
|
||
$servicesRouterLoaders = []; | ||
foreach ($container->findTaggedServiceIds($this->serviceRouterLoaderTag, true) as $id => $attributes) { | ||
$servicesRouterLoaders[$id] = new Reference($id); | ||
} | ||
|
||
$container | ||
->getDefinition($this->loaderServiceServiceId) | ||
->replaceArgument(0, new Definition(ServiceRouterLoaderContainer::class, [ | ||
new Reference('service_container'), | ||
ServiceLocatorTagPass::register($container, $servicesRouterLoaders), | ||
$this->serviceRouterLoaderTag, | ||
])); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Routing/Loader/DependencyInjection/ServiceRouterLoaderContainer.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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\DependencyInjection; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ServiceRouterLoaderContainer implements ContainerInterface | ||
{ | ||
private $container; | ||
private $serviceLocator; | ||
private $serviceRouterLoaderTag; | ||
|
||
public function __construct(ContainerInterface $container, ContainerInterface $serviceLocator, string $serviceRouterLoaderTag) | ||
{ | ||
$this->container = $container; | ||
$this->serviceLocator = $serviceLocator; | ||
$this->serviceRouterLoaderTag = $serviceRouterLoaderTag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($id) | ||
{ | ||
if ($this->serviceLocator->has($id)) { | ||
return $this->serviceLocator->get($id); | ||
} | ||
|
||
@trigger_error(sprintf('Not tagging the "%s" service with the "%s" tag is deprecated since Symfony 4.3.', $id, $this->serviceRouterLoaderTag), E_USER_DEPRECATED); | ||
|
||
return $this->container->get($id); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($id) | ||
{ | ||
return $this->serviceLocator->has($id) || $this->container->has($id); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/Routing/Loader/DependencyInjection/ServiceRouterLoaderInterface.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,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\DependencyInjection; | ||
|
||
interface ServiceRouterLoaderInterface | ||
{ | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Routing/Tests/DependencyInjection/ServiceRouterLoaderPassTest.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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\Routing\DependencyInjection\ServiceRouterLoaderPass; | ||
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader; | ||
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderContainer; | ||
|
||
class ServiceRouterLoaderPassTest extends TestCase | ||
{ | ||
public function testProcess(): void | ||
{ | ||
$loaderServiceServiceId = 'foo'; | ||
$serviceRouterLoaderTag = 'bar'; | ||
|
||
$container = new ContainerBuilder(); | ||
$serviceRouterLoaderDefinition = $container->setDefinition($loaderServiceServiceId, new Definition(ServiceRouterLoader::class, [ | ||
new Definition(Container::class), | ||
])); | ||
$container->register('route_service_loader_1')->addTag($serviceRouterLoaderTag); | ||
$container->register('route_service_loader_2')->addTag($serviceRouterLoaderTag); | ||
|
||
(new ServiceRouterLoaderPass($loaderServiceServiceId, $serviceRouterLoaderTag))->process($container); | ||
|
||
/** @var Definition $serviceRouterLoaderArgument */ | ||
$serviceRouterLoaderArgument = $serviceRouterLoaderDefinition->getArgument(0); | ||
$this->assertSame(ServiceRouterLoaderContainer::class, $serviceRouterLoaderArgument->getClass()); | ||
$this->assertEquals(new Reference('service_container'), $serviceRouterLoaderArgument->getArgument(0)); | ||
/** @var ServiceLocator $serviceLocator */ | ||
$serviceLocator = $container->get((string) $serviceRouterLoaderArgument->getArgument(1)); | ||
$this->assertTrue($serviceLocator->has('route_service_loader_1')); | ||
$this->assertTrue($serviceLocator->has('route_service_loader_2')); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...y/Component/Routing/Tests/Loader/DependencyInjection/ServiceRouterLoaderContainerTest.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,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\Loader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderContainer; | ||
|
||
class ServiceRouterLoaderContainerTest extends TestCase | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $serviceLocator; | ||
|
||
/** | ||
* @var ServiceRouterLoaderContainer | ||
*/ | ||
private $serviceRouterLoaderContainer; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->container = new Container(); | ||
$this->container->set('foo', new \stdClass()); | ||
|
||
$this->serviceLocator = new Container(); | ||
$this->serviceLocator->set('bar', new \stdClass()); | ||
|
||
$this->serviceRouterLoaderContainer = new ServiceRouterLoaderContainer($this->container, $this->serviceLocator, 'ccc'); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* @expectedDeprecation Not tagging the "foo" service with the "ccc" tag is deprecated since Symfony 4.3. | ||
*/ | ||
public function testGet(): void | ||
{ | ||
$this->assertSame($this->container->get('foo'), $this->serviceRouterLoaderContainer->get('foo')); | ||
$this->assertSame($this->serviceLocator->get('bar'), $this->serviceRouterLoaderContainer->get('bar')); | ||
} | ||
|
||
public function testHas(): void | ||
{ | ||
$this->assertTrue($this->serviceRouterLoaderContainer->has('foo')); | ||
$this->assertTrue($this->serviceRouterLoaderContainer->has('bar')); | ||
$this->assertFalse($this->serviceRouterLoaderContainer->has('ccc')); | ||
} | ||
} |