feature #16411 [3.0] use ContainerAwareTrait (blanchonvincent, Tobion)

This PR was merged into the 3.0-dev branch.

Discussion
----------

[3.0] use ContainerAwareTrait

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Finished version of #12595

This adds the first deprecation in symfony 3. We cannot deprecate ContainerAware in 2.8 as the alternative, the trait, cannot be used with php 5.3. So there would not be an upgrade path for people using an older php version

Commits
-------

f75a940 [VarDumper] fix filename
2309901 [VarDumper] replace VarDumperTestCase by trait
24ff770 [TwigBundle] use ContainerAwareTrait in commands
e337ce6 Remove deprecated ContainerAware class and make use of the trait in another class
52c50e3 Remove abstract class and use Interface+Trait
This commit is contained in:
Nicolas Grekas 2015-11-05 10:49:55 +01:00
commit 697e6da19e
22 changed files with 81 additions and 174 deletions

View File

@ -12,7 +12,7 @@
namespace Symfony\Bridge\Doctrine;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Doctrine\Common\Persistence\AbstractManagerRegistry;
/**
@ -22,10 +22,7 @@ use Doctrine\Common\Persistence\AbstractManagerRegistry;
*/
abstract class ManagerRegistry extends AbstractManagerRegistry implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;
use ContainerAwareTrait;
/**
* {@inheritdoc}
@ -42,12 +39,4 @@ abstract class ManagerRegistry extends AbstractManagerRegistry implements Contai
{
$this->container->set($name, null);
}
/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}

View File

@ -11,11 +11,12 @@
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@ -35,8 +36,10 @@ use Doctrine\Bundle\DoctrineBundle\Registry;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Controller extends ContainerAware
abstract class Controller implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* Generates a URL from the given parameters.
*

View File

@ -11,7 +11,8 @@
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -23,8 +24,10 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RedirectController extends ContainerAware
class RedirectController implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* Redirects to another route with the given name.
*

View File

@ -11,7 +11,8 @@
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Response;
/**
@ -19,8 +20,10 @@ use Symfony\Component\HttpFoundation\Response;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateController extends ContainerAware
class TemplateController implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* Renders a template.
*

View File

@ -11,12 +11,15 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;
class FragmentController extends ContainerAware
class FragmentController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function indexAction(Request $request)
{
return $this->container->get('templating')->renderResponse('fragment.html.php', array('bar' => new Bar()));

View File

@ -11,11 +11,14 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;
class ProfilerController extends ContainerAware
class ProfilerController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function indexAction()
{
return new Response('Hello');

View File

@ -11,13 +11,16 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
class SessionController extends ContainerAware
class SessionController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function welcomeAction(Request $request, $name = null)
{
$session = $request->getSession();

View File

@ -11,13 +11,16 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
class SubRequestController extends ContainerAware
class SubRequestController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function indexAction()
{
$handler = $this->container->get('fragment.handler');

View File

@ -11,12 +11,15 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class LoginController extends ContainerAware
class LoginController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function loginAction()
{
$form = $this->container->get('form.factory')->create('Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form\UserLoginType');

View File

@ -11,13 +11,16 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;
class LocalizedController extends ContainerAware
class LocalizedController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function loginAction(Request $request)
{
// get the login error if there is one

View File

@ -11,14 +11,17 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\DependencyInjection\ContainerAware;
class LoginController extends ContainerAware
class LoginController implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function loginAction(Request $request)
{
// get the login error if there is one

View File

@ -12,28 +12,17 @@
namespace Symfony\Bundle\TwigBundle\Command;
use Symfony\Bridge\Twig\Command\DebugCommand as BaseDebugCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
/**
* Lists twig functions, filters, globals and tests present in the current project.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class DebugCommand extends BaseDebugCommand implements ContainerAwareInterface
final class DebugCommand extends BaseDebugCommand implements ContainerAwareInterface
{
/**
* @var ContainerInterface|null
*/
private $container;
/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
use ContainerAwareTrait;
/**
* {@inheritdoc}

View File

@ -12,8 +12,8 @@
namespace Symfony\Bundle\TwigBundle\Command;
use Symfony\Bridge\Twig\Command\LintCommand as BaseLintCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Finder\Finder;
/**
@ -22,20 +22,9 @@ use Symfony\Component\Finder\Finder;
* @author Marc Weistroff <marc.weistroff@sensiolabs.com>
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class LintCommand extends BaseLintCommand implements ContainerAwareInterface
final class LintCommand extends BaseLintCommand implements ContainerAwareInterface
{
/**
* @var ContainerInterface|null
*/
private $container;
/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
use ContainerAwareTrait;
/**
* {@inheritdoc}

View File

@ -1,37 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection;
/**
* A simple implementation of ContainerAwareInterface.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead.
*/
abstract class ContainerAware implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* Sets the Container associated with this Controller.
*
* @param ContainerInterface $container A ContainerInterface instance
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}

View File

@ -11,7 +11,7 @@
namespace Symfony\Component\HttpKernel\Bundle;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Console\Application;
@ -24,8 +24,10 @@ use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Bundle extends ContainerAware implements BundleInterface
abstract class Bundle implements BundleInterface
{
use ContainerAwareTrait;
protected $name;
protected $extension;
protected $path;

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Test;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @deprecated since version 2.8, to be removed in 3.0. Use the VarDumperTestTrait instead.
*/
abstract class VarDumperTestCase extends \PHPUnit_Framework_TestCase
{
public function assertDumpEquals($dump, $data, $message = '')
{
$this->assertSame(rtrim($dump), $this->getDump($data), $message);
}
public function assertDumpMatchesFormat($dump, $data, $message = '')
{
$this->assertStringMatchesFormat(rtrim($dump), $this->getDump($data), $message);
}
protected function getDump($data)
{
$h = fopen('php://memory', 'r+b');
$cloner = new VarCloner();
$dumper = new CliDumper($h);
$dumper->setColors(false);
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
$data = stream_get_contents($h, -1, 0);
fclose($h);
return rtrim($data);
}
}

View File

@ -12,13 +12,15 @@
namespace Symfony\Component\VarDumper\Tests\Caster;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class CasterTest extends VarDumperTestCase
class CasterTest extends \PHPUnit_Framework_TestCase
{
use VarDumperTestTrait;
private $referenceArray = array(
'null' => null,
'empty' => false,

View File

@ -11,14 +11,16 @@
namespace Symfony\Component\VarDumper\Tests\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ReflectionCasterTest extends VarDumperTestCase
class ReflectionCasterTest extends \PHPUnit_Framework_TestCase
{
use VarDumperTestTrait;
public function testReflectionCaster()
{
$var = new \ReflectionClass('ReflectionClass');
@ -73,7 +75,7 @@ Closure {
\$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "63 to 63"
line: "65 to 65"
}
EOTXT
, $var
@ -93,7 +95,7 @@ Closure {
returnType: "int"
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { }
file: "%sReflectionCasterTest.php(88) : eval()'d code"
file: "%sReflectionCasterTest.php(90) : eval()'d code"
line: "1 to 1"
}
EOTXT

View File

@ -11,13 +11,15 @@
namespace Symfony\Component\VarDumper\Tests\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class SplCasterTest extends VarDumperTestCase
class SplCasterTest extends \PHPUnit_Framework_TestCase
{
use VarDumperTestTrait;
public function getCastFileInfoTests()
{
return array(

View File

@ -13,13 +13,15 @@ namespace Symfony\Component\VarDumper\Tests;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class CliDumperTest extends VarDumperTestCase
class CliDumperTest extends \PHPUnit_Framework_TestCase
{
use VarDumperTestTrait;
public function testGet()
{
require __DIR__.'/Fixtures/dumb-var.php';

View File

@ -1,16 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// Skipping trait tests for PHP < 5.4
if (version_compare(PHP_VERSION, '5.4.0-dev', '>=')) {
require 'VarDumpTestTraitRequire54.php';
}

View File

@ -11,10 +11,9 @@
namespace Symfony\Component\VarDumper\Tests\Test;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
class VarDumperTestTraitTest extends VarDumperTestCase
class VarDumperTestTraitTest extends \PHPUnit_Framework_TestCase
{
use VarDumperTestTrait;