From e564cc67ae1d2fbb72a3dc1bbc19cd8e5d5c6e1c Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Mon, 12 Oct 2020 01:04:36 +0200 Subject: [PATCH] Deprecate Pool::getContainer method The code that uses this method is already deprecated. --- UPGRADE-3.x.md | 4 ++++ docs/cookbook/recipe_image_previews.rst | 6 +++--- src/Admin/AbstractAdmin.php | 2 +- src/Admin/Pool.php | 13 ++++++++++++- src/Mapper/BaseGroupedMapper.php | 2 +- tests/Admin/PoolTest.php | 5 +++++ tests/Mapper/BaseGroupedMapperTest.php | 3 ++- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/UPGRADE-3.x.md b/UPGRADE-3.x.md index 4d9715ed24..86fa84b56c 100644 --- a/UPGRADE-3.x.md +++ b/UPGRADE-3.x.md @@ -1,6 +1,10 @@ UPGRADE 3.x =========== +### Deprecated `Sonata\AdminBundle\Admin\Pool::getContainer()` method. + +This method has been deprecated without replacement. + ### Deprecated using shortcut notation when specifying the `user_model` option in `sonata:admin:generate-object-acl` command. The shortcut notation (`AppBundle:User`) has been deprecated in favor of the FQCN (`App\Model\User`) when passing diff --git a/docs/cookbook/recipe_image_previews.rst b/docs/cookbook/recipe_image_previews.rst index 708873b546..ee68493046 100644 --- a/docs/cookbook/recipe_image_previews.rst +++ b/docs/cookbook/recipe_image_previews.rst @@ -51,9 +51,9 @@ we are manipulating form fields we do this from within ``ImageAdmin::configureFo // use $fileFormOptions so we can add other options to the field $fileFormOptions = ['required' => false]; if ($image && ($webPath = $image->getWebPath())) { - // get the container so the full path to the image can be set - $container = $this->getConfigurationPool()->getContainer(); - $fullPath = $container->get('request_stack')->getCurrentRequest()->getBasePath().'/'.$webPath; + // get the request so the full path to the image can be set + $request = $this->getRequest(); + $fullPath = $request->getBasePath().'/'.$webPath; // add a 'help' option containing the preview's img tag $fileFormOptions['help'] = ''; diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 2a6819c6c0..5b59bb8c29 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -2377,7 +2377,7 @@ final public function getBreadcrumbsBuilder() ), E_USER_DEPRECATED); if (null === $this->breadcrumbsBuilder) { $this->breadcrumbsBuilder = new BreadcrumbsBuilder( - $this->getConfigurationPool()->getContainer()->getParameter('sonata.admin.configuration.breadcrumbs') + $this->getConfigurationPool()->getContainer('sonata_deprecation_mute')->getParameter('sonata.admin.configuration.breadcrumbs') ); } diff --git a/src/Admin/Pool.php b/src/Admin/Pool.php index afc4fa6c05..8881dbe522 100644 --- a/src/Admin/Pool.php +++ b/src/Admin/Pool.php @@ -387,10 +387,21 @@ public function getInstance($id) } /** - * @return ContainerInterface|null + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. + * + * @return ContainerInterface */ public function getContainer() { + if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) { + @trigger_error(sprintf( + 'Method "%s()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + } + return $this->container; } diff --git a/src/Mapper/BaseGroupedMapper.php b/src/Mapper/BaseGroupedMapper.php index f461842dfa..1e9d3fcf6d 100644 --- a/src/Mapper/BaseGroupedMapper.php +++ b/src/Mapper/BaseGroupedMapper.php @@ -88,7 +88,7 @@ public function with($name, array $options = []) // NEXT_MAJOR: remove this code if ($this->admin instanceof AbstractAdmin && $pool = $this->admin->getConfigurationPool()) { - if ($pool->getContainer()->getParameter('sonata.admin.configuration.translate_group_label')) { + if ($pool->getContainer('sonata_deprecation_mute')->getParameter('sonata.admin.configuration.translate_group_label')) { $defaultOptions['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($name, $this->getName(), 'group'); } } diff --git a/tests/Admin/PoolTest.php b/tests/Admin/PoolTest.php index cebdce833a..68bb73bf77 100644 --- a/tests/Admin/PoolTest.php +++ b/tests/Admin/PoolTest.php @@ -494,6 +494,11 @@ public function testGetAdminServiceIds(): void $this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds()); } + /** + * NEXT_MAJOR: Remove this test. + * + * @group legacy + */ public function testGetContainer(): void { $this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer()); diff --git a/tests/Mapper/BaseGroupedMapperTest.php b/tests/Mapper/BaseGroupedMapperTest.php index be5c46bca3..c55b89539c 100644 --- a/tests/Mapper/BaseGroupedMapperTest.php +++ b/tests/Mapper/BaseGroupedMapperTest.php @@ -227,10 +227,11 @@ public function labelDataProvider(): array */ public function testLabel(bool $translated, string $name, ?string $label, string $expectedLabel): void { + // NEXT_MAJOR: Remove $container variable and the call to setParameter. $container = $this->baseGroupedMapper ->getAdmin() ->getConfigurationPool() - ->getContainer(); + ->getContainer('sonata_deprecation_mute'); $container->setParameter('sonata.admin.configuration.translate_group_label', $translated);