8000 Deprecate constructing AdminPoolLoader with a third argument · sonata-project/SonataAdminBundle@489f9de · GitHub
[go: up one dir, main page]

Skip to content

Commit 489f9de

Browse files
franmomuVincentLanglet
authored andcommitted
Deprecate constructing AdminPoolLoader with a third argument
The container was added to the router cache in symfony/framework-bundle in 4.4.6. See symfony/symfony#36143
1 parent 8d5478b commit 489f9de

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

UPGRADE-3.x.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ UPGRADE 3.x
44
UPGRADE FROM 3.xx to 3.xx
55
=========================
66

7+
### `Sonata\AdminBundle\Route\AdminPoolLoader`
8+
9+
- Deprecated constructing it passing more than one argument.
10+
- Deprecated `Sonata\AdminBundle\Route\AdminPoolLoader` service alias.
11+
712
## Deprecated not setting "sonata.admin.audit_reader" tag in audit reader services
813

914
If you are using [autoconfiguration](https://symfony.com/doc/4.4/service_container.html#the-autoconfigure-option),

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"symfony/event-dispatcher-contracts": "^1.1 || ^2.0",
4747
"symfony/expression-language": "^4.4 || ^5.1",
4848
"symfony/form": "^4.4.12",
49-
"symfony/framework-bundle": "^4.4",
49+
"symfony/framework-bundle": "^4.4.6",
5050
"symfony/http-foundation": "^4.4 || ^5.1",
5151
"symfony/http-kernel": "^4.4",
5252
"symfony/options-resolver": "^4.4 || ^5.1",

src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ static function (array $a, array $b): int {
238238
$pool->replaceArgument(1, $admins);
239239
$pool->replaceArgument(2, $groups);
240240
$pool->replaceArgument(3, $classes);
241-
242-
$routeLoader = $container->getDefinition('sonata.admin.route_loader');
243-
$routeLoader->replaceArgument(1, $admins);
244241
}
245242

246243
/**

src/Resources/config/core.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@
7878
->tag('routing.loader')
7979
->args([
8080
new ReferenceConfigurator('sonata.admin.pool'),
81-
[],
82-
new ReferenceConfigurator('service_container'),
8381
])
8482

8583
->alias(AdminPoolLoader::class, 'sonata.admin.route_loader')
84+
->deprecate(...BCDeprecationParameters::forConfig(
85+
'The "%alias_id%" alias is deprecated since sonata-project/admin-bundle 3.x and will be removed in 4.0.',
86+
'3.x'
87+
))
8688

8789
->set('sonata.admin.helper', AdminHelper::class)
8890
->public()

src/Route/AdminPoolLoader.php

Lines changed: 37 additions & 6 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,33 @@ class AdminPoolLoader extends Loader
3434
protected $pool;
3535

3636
/**
37+
* NEXT_MAJOR: Remove this property.
38+
*
3739
* @var array
3840
*/
3941
protected $adminServiceIds = [];
4042

4143
/**
42-
* @var ContainerInterface
44+
* NEXT_MAJOR: Remove this property.
45+
*
46+
* @var ContainerInterface|null
4347
*/
4448
protected $container;
4549

46-
public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
50+
// NEXT_MAJOR: Remove $adminServiceIds and $container parameters.
51+
public function __construct(Pool $pool, array $adminServiceIds = [], ?ContainerInterface $container = null)
4752
{
4853
$this->pool = $pool;
54+
// NEXT_MAJOR: Remove next line.
55+
if (\func_num_args() > 1) {
56+
@trigger_error(sprintf(
57+
'Passing more than one argument to "%s()" is deprecated since'
58+
.' sonata-project/admin-bundle 3.x.',
59+
__METHOD__
60+
), \E_USER_DEPRECATED);
61+
}
62+
63+
// NEXT_MAJOR: Remove the following two lines.
4964
$this->adminServiceIds = $adminServiceIds;
5065
$this->container = $container;
5166
}
@@ -58,7 +73,8 @@ public function supports($resource, $type = null)
5873
public function load($resource, $type = null)
5974
{
6075
$collection = new SymfonyRouteCollection();
61-
foreach ($this->adminServiceIds as $id) {
76+
// NEXT_MAJOR: Replace $this->getAdminServiceIds() with $this->pool->getAdminServiceIds()
77+
foreach ($this->getAdminServiceIds() as $id) {
6278
$admin = $this->pool->getInstance($id);
6379

6480
foreach ($admin->getRoutes()->getElements() as $code => $route) {
@@ -71,11 +87,26 @@ public function load($resource, $type = null)
7187
}
7288
}
7389

74-
$reflection = new \ReflectionObject($this->container);
75-
if (file_exists($reflection->getFileName())) {
76-
$collection->addResource(new FileResource($reflection->getFileName()));
90+
// NEXT_MAJOR: Remove this block.
91+
if (null !== $this->container) {
92+
$reflection = new \ReflectionObject($this->container);
93+
if (file_exists($reflection->getFileName())) {
94+
$collection->addResource(new FileResource($reflection->getFileName()));
95+
}
7796
}
7897

7998
return $collection;
8099
}
100+
101+
/**
102+
* @return string[]
103+
*/
104+
private function getAdminServiceIds(): array
105+
{
106+
if ([] !== $this->adminServiceIds) {
107+
return $this->adminServiceIds;
108+
}
109+
110+
return $this->pool->getAdminServiceIds();
111+
}
81112
}

tests/Route/AdminPoolLoaderTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sonata\AdminBundle\Admin\Pool;
1919
use Sonata\AdminBundle\Route\AdminPoolLoader;
2020
use Sonata\AdminBundle\Route\RouteCollection;
21+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
2122
use Symfony\Component\DependencyInjection\Container;
2223
use Symfony\Component\Routing\Route as SymfonyRoute;
2324
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
@@ -27,12 +28,15 @@
2728
*/
2829
class AdminPoolLoaderTest extends TestCase
2930
{
31+
// NEXT_MAJOR: Remove next line.
32+
use ExpectDeprecationTrait;
33+
3034
public function testSupports(): void
3135
{
3236
$container = new Container();
33-
$pool = new Pool($container);
37+
$pool = new Pool($container, ['foo_admin', 'bar_admin']);
3438

35-
$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
39+
$adminPoolLoader = new AdminPoolLoader($pool);
3640

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

46-
$adminPoolLoader = new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
50+
$adminPoolLoader = new AdminPoolLoader($pool);
4751

4852
$routeCollection1 = new RouteCollection('base.Code.Route.foo', 'baseRouteNameFoo', 'baseRoutePatternFoo', 'baseControllerNameFoo');
4953
$routeCollection2 = new RouteCollection('base.Code.Route.bar', 'baseRouteNameBar', 'baseRoutePatternBar', 'baseControllerNameBar');
@@ -73,4 +77,32 @@ public function testLoad(): void
7377
$this->assertInstanceOf(SymfonyRoute::class, $collection->get('baseRouteNameBar_bar'));
7478
$this->assertInstanceOf(SymfonyRoute::class, $collection->get('baseRouteNameBar_bar'));
7579
}
80+
81+
/**
82+
* NEXT_MAJOR: Remove this method.
83+
*
84+
* @group legacy
85+
*/
86+
public function testThrowsADeprecationConstructingWithContainer(): void
87+
{
88+
$container = new Container();
89+
$pool = new Pool($container);
90+
91+
$this->expectDeprecation('Passing more than one argument to "Sonata\AdminBundle\Route\AdminPoolLoader::__construct()" is deprecated since sonata-project/admin-bundle 3.x.');
92+
new AdminPoolLoader($pool, ['foo_admin', 'bar_admin'], $container);
93+
}
94+
95+
/**
96+
* NEXT_MAJOR: Remove this method.
97+
*
98+
* @group legacy
99+
*/
100+
public function testThrowsADeprecationConstructingWithAdminServicesIds(): void
101+
{
102+
$container = new Container();
103+
$pool = new Pool($container);
104+
105+
$this->expectDeprecation('Passing more than one argument to "Sonata\AdminBundle\Route\AdminPoolLoader::__construct()" is deprecated since sonata-project/admin-bundle 3.x.');
106+
new AdminPoolLoader($pool, ['foo_admin', 'bar_admin']);
107+
}
76108
}

0 commit comments

Comments
 (0)
0