Skip to content

Commit

Permalink
Deprecate constructing AdminPoolLoader with a third argument
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Mar 28, 2021
1 parent 06877d7 commit a4106e0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-3.x.md
@@ -1,6 +1,13 @@
UPGRADE 3.x
===========

UPGRADE FROM 3.xx to 3.xx
=========================

### `Sonata\AdminBundle\Route\AdminPoolLoader`

Deprecated constructing it passing a third argument.

UPGRADE FROM 3.92 to 3.93
=========================

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/core.php
Expand Up @@ -79,7 +79,6 @@
->args([
new ReferenceConfigurator('sonata.admin.pool'),
[],
new ReferenceConfigurator('service_container'),
])

->alias(AdminPoolLoader::class, 'sonata.admin.route_loader')
Expand Down
27 changes: 22 additions & 5 deletions src/Route/AdminPoolLoader.php
Expand Up @@ -39,14 +39,28 @@ class AdminPoolLoader extends Loader
protected $adminServiceIds = [];

/**
* @var ContainerInterface
* NEXT_MAJOR: Remove this property.
*
* @var ContainerInterface|null
*/
protected $container;

public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
// NEXT_MAJOR: Remove $container parameter.
public function __construct(Pool $pool, array $adminServiceIds, ?ContainerInterface $container = null)
{
$this->pool = $pool;
$this->adminServiceIds = $adminServiceIds;

// NEXT_MAJOR: Remove this block.
if (\func_num_args() > 2) {
@trigger_error(sprintf(
'Passing an instance of "%s" as argument 3 to "%s()" is deprecated since'
.' sonata-project/admin-bundle 3.x.',
ContainerInterface::class,
__METHOD__
), \E_USER_DEPRECATED);
}

$this->container = $container;
}

Expand All @@ -71,9 +85,12 @@ public function load($resource, $type = null)
}
}

$reflection = new \ReflectionObject($this->container);
if (file_exists($reflection->getFileName())) {
$collection->addResource(new FileResource($reflection->getFileName()));
// NEXT_MAJOR: Remove this block.
if (null !== $this->container) {
$reflection = new \ReflectionObject($this->container);
if (file_exists($reflection->getFileName())) {
$collection->addResource(new FileResource($reflection->getFileName()));
}
}

return $collection;
Expand Down
23 changes: 21 additions & 2 deletions tests/Route/AdminPoolLoaderTest.php
Expand Up @@ -18,6 +18,7 @@
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Route\AdminPoolLoader;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Routing\Route as SymfonyRoute;
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
Expand All @@ -27,12 +28,15 @@
*/
class AdminPoolLoaderTest extends TestCase
{
// NEXT_MAJOR: Remove next line.
use ExpectDeprecationTrait;

public function testSupports(): void
{
$container = new Container();
$pool = new Pool($container);

$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin']);

$this->assertTrue($adminPoolLoader->supports('foo', 'sonata_admin'));
$this->assertFalse($adminPoolLoader->supports('foo', 'bar'));
Expand All @@ -43,7 +47,7 @@ public function testLoad(): void
$container = new Container();
$pool = new Pool($container, ['foo_admin', 'bar_admin']);

$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin']);

$routeCollection1 = new RouteCollection('base.Code.Route.foo', 'baseRouteNameFoo', 'baseRoutePatternFoo', 'baseControllerNameFoo');
$routeCollection2 = new RouteCollection('base.Code.Route.bar', 'baseRouteNameBar', 'baseRoutePatternBar', 'baseControllerNameBar');
Expand Down Expand Up @@ -73,4 +77,19 @@ public function testLoad(): void
$this->assertInstanceOf(SymfonyRoute::class, $collection->get('baseRouteNameBar_bar'));
$this->assertInstanceOf(SymfonyRoute::class, $collection->get('baseRouteNameBar_bar'));
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testThrowsADeprecationConstructingWithContainer(): void
{
$container = new Container();
$pool = new Pool($container);

$this->expectDeprecation('Passing an instance of "Symfony\Component\DependencyInjection\ContainerInterface" as argument 3 to "Sonata\AdminBundle\Route\AdminPoolLoader::__construct()" is deprecated since sonata-project/admin-bundle 3.x.');

new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
}
}

0 comments on commit a4106e0

Please sign in to comment.