Issue #3431362 by Spokje, longwave: Remove support for ContainerAwareInterface

merge-requests/6009/merge
Alex Pott 2024-03-21 08:41:16 +00:00
parent 5aaf92bb93
commit a058be438b
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
7 changed files with 11 additions and 130 deletions

View File

@ -37,8 +37,3 @@
%Since symfony/dependency-injection 6.4: "Symfony\\Component\\DependencyInjection\\ContainerAwareTrait" is deprecated, use dependency injection instead.%
%The "Drupal\\Core\\Logger\\LoggerChannelFactory" class implements "Symfony\\Component\\DependencyInjection\\ContainerAwareInterface" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Core\\Logger\\LoggerChannelFactory" class uses "Symfony\\Component\\DependencyInjection\\ContainerAwareTrait" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Core\\Queue\\QueueFactory" class implements "Symfony\\Component\\DependencyInjection\\ContainerAwareInterface" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Core\\Queue\\QueueFactory" class uses "Symfony\\Component\\DependencyInjection\\ContainerAwareTrait" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Tests\\Core\\Controller\\MockContainerAware" class implements "Symfony\\Component\\DependencyInjection\\ContainerAwareInterface" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Tests\\Core\\DependencyInjection\\DependencySerializationTestDummy" class implements "Symfony\\Component\\DependencyInjection\\ContainerAwareInterface" that is deprecated since Symfony 6.4, use dependency injection instead.%
%The "Drupal\\Tests\\Core\\Utility\\MockContainerAware" class implements "Symfony\\Component\\DependencyInjection\\ContainerAwareInterface" that is deprecated since Symfony 6.4, use dependency injection instead.%

View File

@ -2264,24 +2264,12 @@ https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/Config/ConfigTest.php',
];
$ignoreErrors[] = [
'message' => '#^Class Drupal\\\\Tests\\\\Core\\\\Controller\\\\MockContainerAware implements deprecated interface Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface\\:
since Symfony 6\\.4, use dependency injection instead$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated method expectError\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/Database/ConditionTest.php',
];
$ignoreErrors[] = [
'message' => '#^Class Drupal\\\\Tests\\\\Core\\\\DependencyInjection\\\\DependencySerializationTestDummy implements deprecated interface Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface\\:
since Symfony 6\\.4, use dependency injection instead$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php',
];
$ignoreErrors[] = [
'message' => '#^Trying to mock an undefined method getRevisionId\\(\\) on class Drupal\\\\Tests\\\\Core\\\\Entity\\\\UrlTestEntity\\.$#',
'count' => 1,
@ -2374,12 +2362,6 @@ $ignoreErrors[] = [
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/Test/AssertContentTraitTest.php',
];
$ignoreErrors[] = [
'message' => '#^Class Drupal\\\\Tests\\\\Core\\\\Utility\\\\MockContainerAware implements deprecated interface Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface\\:
since Symfony 6\\.4, use dependency injection instead$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated method getConfig\\(\\) of class GuzzleHttp\\\\ClientInterface\\:
ClientInterface\\:\\:getConfig will be removed in guzzlehttp/guzzle\\:8\\.0\\.$#',

View File

@ -10,12 +10,7 @@ use Symfony\Component\HttpFoundation\Request;
/**
* ControllerResolver to enhance controllers beyond Symfony's basic handling.
*
* It adds two behaviors:
*
* - When creating a new object-based controller that implements
* ContainerAwareInterface, inject the container into it. While not always
* necessary, that allows a controller to vary the services it needs at
* runtime.
* It adds one behavior:
*
* - By default, a controller name follows the class::method notation. This
* class adds the possibility to use a service from the container as a

View File

@ -2,7 +2,6 @@
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -15,14 +14,10 @@ class ClassResolver implements ClassResolverInterface {
/**
* Constructs a new ClassResolver object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
*/
public function __construct(protected ?ContainerInterface $container = NULL) {
if ($this->container === NULL) {
@trigger_error('Calling ' . __METHOD__ . ' without the $container argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3419963', E_USER_DEPRECATED);
$this->container = \Drupal::getContainer();
}
public function __construct(protected ContainerInterface $container) {
}
/**
@ -45,26 +40,7 @@ class ClassResolver implements ClassResolverInterface {
}
}
if ($instance instanceof ContainerAwareInterface) {
@trigger_error('Implementing \Symfony\Component\DependencyInjection\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \Drupal\Core\DependencyInjection\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661', E_USER_DEPRECATED);
$instance->setContainer($this->container);
}
return $instance;
}
/**
* Sets the service container.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0.
* Instead, you should pass the container as an argument in the
* __construct() method.
*
* @see https://www.drupal.org/node/3419963
*/
public function setContainer(?ContainerInterface $container): void {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Instead, you should pass the container as an argument in the __construct() method. See https://www.drupal.org/node/3419963', E_USER_DEPRECATED);
$this->container = $container;
}
}

View File

@ -11,7 +11,6 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Utility\CallableResolver;
use Drupal\Tests\UnitTestCase;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@ -52,12 +51,8 @@ class ControllerResolverTest extends UnitTestCase {
* Tests createController().
*
* @dataProvider providerTestCreateController
* @group legacy
*/
public function testCreateController($controller, $class, $output, string $deprecation = NULL) {
if ($deprecation) {
$this->expectDeprecation($deprecation);
}
public function testCreateController($controller, $class, $output) {
$this->container->set('some_service', new MockController());
$result = $this->controllerResolver->getControllerFromDefinition($controller);
$this->assertCallableController($result, $class, $output);
@ -74,13 +69,6 @@ class ControllerResolverTest extends UnitTestCase {
['some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
// Tests a class with injection.
['Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'],
// Tests a ContainerAware class.
[
'Drupal\Tests\Core\Controller\MockContainerAware::getResult',
'Drupal\Tests\Core\Controller\MockContainerAware',
'This is container aware.',
'Implementing \Symfony\Component\DependencyInjection\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \Drupal\Core\DependencyInjection\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661',
],
];
}
@ -104,12 +92,8 @@ class ControllerResolverTest extends UnitTestCase {
* Tests getController().
*
* @dataProvider providerTestGetController
* @group legacy
*/
public function testGetController($attributes, $class, $output = NULL) {
if ($class) {
$this->expectDeprecation('Implementing \Symfony\Component\DependencyInjection\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \Drupal\Core\DependencyInjection\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661');
}
$request = new Request([], [], $attributes);
$result = $this->controllerResolver->getController($request);
if ($class) {
@ -126,7 +110,11 @@ class ControllerResolverTest extends UnitTestCase {
public static function providerTestGetController() {
return [
// Tests passing a controller via the request.
[['_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'], 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
[
['_controller' => MockContainerInjection::class . '::getResult'],
MockContainerInjection::class,
'This used injection.',
],
// Tests a request with no controller specified.
[[], FALSE],
];
@ -232,28 +220,7 @@ class MockContainerInjection implements ContainerInjectionInterface {
}
}
class MockContainerAware implements ContainerAwareInterface {
/**
* The service container.
*/
protected ContainerInterface $container;
/**
* Sets the service container.
*/
public function setContainer(?ContainerInterface $container): void {
$this->container = $container;
}
public function getResult() {
if (empty($this->container)) {
throw new \Exception('Container was not injected.');
}
return 'This is container aware.';
}
}
class MockInvokeController {
public function __invoke() {

View File

@ -8,7 +8,6 @@ use Drupal\Component\DependencyInjection\ReverseContainer;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Test\TestKernel;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -46,7 +45,7 @@ class DependencySerializationTest extends UnitTestCase {
/**
* Defines a test class which has a single service as dependency.
*/
class DependencySerializationTestDummy implements ContainerAwareInterface {
class DependencySerializationTestDummy {
use DependencySerializationTrait;

View File

@ -9,7 +9,6 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Utility\CallableResolver;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -42,12 +41,8 @@ class CallableResolverTest extends UnitTestCase {
/**
* @dataProvider callableResolverTestCases
* @covers ::getCallableFromDefinition
* @group legacy
*/
public function testCallbackResolver($definition, $result, string $deprecation = NULL) {
if ($deprecation) {
$this->expectDeprecation($deprecation);
}
public function testCallbackResolver($definition, $result) {
$argument = 'bar';
$this->assertEquals($result . '+' . $argument, $this->resolver->getCallableFromDefinition($definition)($argument));
}
@ -99,11 +94,6 @@ class CallableResolverTest extends UnitTestCase {
'\Drupal\Tests\Core\Utility\MockContainerInjection::getResult',
'Drupal\Tests\Core\Utility\MockContainerInjection::getResult-foo',
],
'Non-static function, instantiated by class resolver, container aware' => [
'\Drupal\Tests\Core\Utility\MockContainerAware::getResult',
'Drupal\Tests\Core\Utility\MockContainerAware::getResult',
'Implementing \Symfony\Component\DependencyInjection\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \Drupal\Core\DependencyInjection\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661',
],
'Service notation' => [
'test_service:method',
__CLASS__ . '::method',
@ -253,26 +243,3 @@ class NoInstantiationMockStaticCallable {
class NoMethodCallable {
}
class MockContainerAware implements ContainerAwareInterface {
/**
* The service container.
*/
protected ContainerInterface $container;
/**
* Sets the service container.
*/
public function setContainer(?ContainerInterface $container): void {
$this->container = $container;
}
public function getResult($suffix) {
if (empty($this->container)) {
throw new \Exception('Container was not injected.');
}
return __METHOD__ . '+' . $suffix;
}
}