Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#282: call setContainer on ContainerAwareInterface #293

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions HttpKernel/ControllerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions Tests/Functional/AutowiredControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* 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();
$client->request('GET', '/autowired-controller-test');

$expected = '';
$expected .= "\$has container: OK\n";

$this->assertEquals($expected, $client->getResponse()->getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* 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);
}
}
3 changes: 3 additions & 0 deletions Tests/Functional/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }