From bd6caf414472cc26658d597b23c5d7116df5145e Mon Sep 17 00:00:00 2001 From: Ruben Smets Date: Thu, 26 Oct 2017 21:37:30 +0200 Subject: [PATCH 1/4] Issue#282: call setContainer on ContainerAwareInterface Autowired controllers break when not calling setContainer on ContainerAwareInterface implementations. --- HttpKernel/ControllerResolver.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/HttpKernel/ControllerResolver.php b/HttpKernel/ControllerResolver.php index 71e7366..ec205bc 100644 --- a/HttpKernel/ControllerResolver.php +++ b/HttpKernel/ControllerResolver.php @@ -75,12 +75,16 @@ protected function createController($controller) */ protected function instantiateController($class) { + $controller = null; + if ($this->container->has($class)) { - return $this->container->get($class); + $controller = $this->container->get($class); } - $injector = $this->createInjector($class); - $controller = call_user_func($injector, $this->container); + if (null === $controller) { + $injector = $this->createInjector($class); + $controller = call_user_func($injector, $this->container); + } if ($controller instanceof ContainerAwareInterface) { $controller->setContainer($this->container); From c5b35a78690b5b82b22a86c6670f70ba5e36c22b Mon Sep 17 00:00:00 2001 From: Ruben Smets Date: Sat, 28 Oct 2017 10:55:42 +0200 Subject: [PATCH 2/4] Issue#282: test if container is set on autowired controller --- Tests/Functional/AutowiredControllerTest.php | 39 +++++++++++++++++++ .../Controller/AutowiredController.php | 37 ++++++++++++++++++ .../config/autowired_controller.yml | 8 ++++ 3 files changed, 84 insertions(+) create mode 100644 Tests/Functional/AutowiredControllerTest.php create mode 100644 Tests/Functional/Bundle/TestBundle/Controller/AutowiredController.php create mode 100644 Tests/Functional/config/autowired_controller.yml diff --git a/Tests/Functional/AutowiredControllerTest.php b/Tests/Functional/AutowiredControllerTest.php new file mode 100644 index 0000000..9c10158 --- /dev/null +++ b/Tests/Functional/AutowiredControllerTest.php @@ -0,0 +1,39 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\DiExtraBundle\Tests\Functional; + +class AutowiredControllerTest extends BaseTestCase +{ + /** + * @runInSeparateProcess + */ + public function testAutowiredController() + { + $client = $this->createClient(array( + 'config' => 'autowired_controller.yml', + )); + $client->request('GET', '/autowired-controller-test'); + + $expected = ''; + $expected .= "\$has container: OK\n"; + + + $this->assertEquals($expected, $client->getResponse()->getContent()); + } +} diff --git a/Tests/Functional/Bundle/TestBundle/Controller/AutowiredController.php b/Tests/Functional/Bundle/TestBundle/Controller/AutowiredController.php new file mode 100644 index 0000000..c0fb645 --- /dev/null +++ b/Tests/Functional/Bundle/TestBundle/Controller/AutowiredController.php @@ -0,0 +1,37 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller; + +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpFoundation\Response; + +class AutowiredController extends Controller +{ + /** + * @Route("/autowired-controller-test") + */ + public function testAction() + { + $content = ''; + $content .= sprintf("\$has container: %s\n", $this->container !== null ? 'OK' : 'FAILED'); + + return new Response($content); + } +} diff --git a/Tests/Functional/config/autowired_controller.yml b/Tests/Functional/config/autowired_controller.yml new file mode 100644 index 0000000..6503270 --- /dev/null +++ b/Tests/Functional/config/autowired_controller.yml @@ -0,0 +1,8 @@ +imports: + - { resource: default.yml } + +services: + autowired_controller: + class: JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller\AutowiredController + autowired: true + From 5e1d3ff8cacee841517b5449d0cc7fd534471bef Mon Sep 17 00:00:00 2001 From: Ruben Smets Date: Sat, 28 Oct 2017 15:03:10 +0200 Subject: [PATCH 3/4] Issue#282: move controller definition to default config Cleans up some unnecessary files. --- Tests/Functional/AutowiredControllerTest.php | 5 +---- Tests/Functional/config/autowired_controller.yml | 8 -------- Tests/Functional/config/default.yml | 3 +++ 3 files changed, 4 insertions(+), 12 deletions(-) delete mode 100644 Tests/Functional/config/autowired_controller.yml diff --git a/Tests/Functional/AutowiredControllerTest.php b/Tests/Functional/AutowiredControllerTest.php index 9c10158..ef64553 100644 --- a/Tests/Functional/AutowiredControllerTest.php +++ b/Tests/Functional/AutowiredControllerTest.php @@ -25,15 +25,12 @@ class AutowiredControllerTest extends BaseTestCase */ public function testAutowiredController() { - $client = $this->createClient(array( - 'config' => 'autowired_controller.yml', - )); + $client = $this->createClient(); $client->request('GET', '/autowired-controller-test'); $expected = ''; $expected .= "\$has container: OK\n"; - $this->assertEquals($expected, $client->getResponse()->getContent()); } } diff --git a/Tests/Functional/config/autowired_controller.yml b/Tests/Functional/config/autowired_controller.yml deleted file mode 100644 index 6503270..0000000 --- a/Tests/Functional/config/autowired_controller.yml +++ /dev/null @@ -1,8 +0,0 @@ -imports: - - { resource: default.yml } - -services: - autowired_controller: - class: JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller\AutowiredController - autowired: true - diff --git a/Tests/Functional/config/default.yml b/Tests/Functional/config/default.yml index 8daa730..12bedac 100644 --- a/Tests/Functional/config/default.yml +++ b/Tests/Functional/config/default.yml @@ -13,6 +13,9 @@ services: controller.hello: class: JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller\ServiceController arguments: [ "@router" ] + controller.autowired: + class: JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Controller\AutowiredController + autowired: true sensio_framework_extra: request: { converters: false } From 8c6da9d618f1ee9b6f9ab84017fe55257be95da0 Mon Sep 17 00:00:00 2001 From: Ruben Smets Date: Tue, 31 Oct 2017 15:02:37 +0100 Subject: [PATCH 4/4] Issue#282: add Controller to LegacyTestBundle for older versions --- .../Controller/AutowiredController.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Tests/Functional/Bundle/LegacyTestBundle/Controller/AutowiredController.php diff --git a/Tests/Functional/Bundle/LegacyTestBundle/Controller/AutowiredController.php b/Tests/Functional/Bundle/LegacyTestBundle/Controller/AutowiredController.php new file mode 100644 index 0000000..b47e378 --- /dev/null +++ b/Tests/Functional/Bundle/LegacyTestBundle/Controller/AutowiredController.php @@ -0,0 +1,37 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\DiExtraBundle\Tests\Functional\Bundle\LegacyTestBundle\Controller; + +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpFoundation\Response; + +class AutowiredController extends Controller +{ + /** + * @Route("/autowired-controller-test") + */ + public function testAction() + { + $content = ''; + $content .= sprintf("\$has container: %s\n", $this->container !== null ? 'OK' : 'FAILED'); + + return new Response($content); + } +}