Verified Commit 365dfc2e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3049525 by longwave, fougere, larowlan, kim.pepper, AaronBauman, Wim...

Issue #3049525 by longwave, fougere, larowlan, kim.pepper, AaronBauman, Wim Leers, Charlie ChX Negyesi, alexpott, geek-merlin: Enable service autowiring by adding interface aliases to core service definitions
parent 55274275
Loading
Loading
Loading
Loading
+183 −0

File changed.

Preview size limit exceeded, changes collapsed.

+4 −0
Original line number Diff line number Diff line
@@ -1282,6 +1282,10 @@ protected function compileContainer() {
    $container->register('kernel', 'Symfony\Component\HttpKernel\KernelInterface')->setSynthetic(TRUE);
    $container->register('service_container', 'Symfony\Component\DependencyInjection\ContainerInterface')->setSynthetic(TRUE);

    // Register aliases of synthetic services for autowiring.
    $container->setAlias(DrupalKernelInterface::class, 'kernel');
    $container->setAlias(ContainerInterface::class, 'service_container');

    // Register application services.
    $yaml_loader = new YamlFileLoader($container);
    foreach ($this->serviceYamls['app'] as $filename) {
+3 −1
Original line number Diff line number Diff line
@@ -15,8 +15,10 @@ services:
    alias: 'Drupal\autowire_test\TestInjection'
    public: false

  # A service that tests autowiring for two constructor arguments:
  # A service that tests autowiring for four constructor arguments:
  # - One type-hinted to TestInjectionInterface.
  # - One type-hinted to TestInjection2.
  # - One type-hinted to \Drupal\Core\Database\Connection.
  # - One type-hinted to \Symfony\Component\HttpKernel\KernelInterface.
  Drupal\autowire_test\TestService:
    autowire: true
+24 −1
Original line number Diff line number Diff line
@@ -2,6 +2,9 @@

namespace Drupal\autowire_test;

use Drupal\Core\Database\Connection;
use Drupal\Core\DrupalKernelInterface;

class TestService {

  /**
@@ -14,9 +17,21 @@ class TestService {
   */
  protected $testInjection2;

  public function __construct(TestInjectionInterface $test_injection, TestInjection2 $test_injection2) {
  /**
   * The database connection.
   */
  protected $database;

  /**
   * The Drupal kernel.
   */
  protected $kernel;

  public function __construct(TestInjectionInterface $test_injection, TestInjection2 $test_injection2, Connection $database, DrupalKernelInterface $kernel) {
    $this->testInjection = $test_injection;
    $this->testInjection2 = $test_injection2;
    $this->database = $database;
    $this->kernel = $kernel;
  }

  public function getTestInjection(): TestInjectionInterface {
@@ -27,4 +42,12 @@ public function getTestInjection2(): TestInjection2 {
    return $this->testInjection2;
  }

  public function getDatabase(): Connection {
    return $this->database;
  }

  public function getKernel(): DrupalKernelInterface {
    return $this->kernel;
  }

}
+2 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
@@ -723,6 +724,7 @@ protected function setTestFeedResponses(array $responses): void {
    $this->container = $this->container->get('kernel')->getContainer();
    $this->container->get('logger.factory')->addLogger($this);
    $this->container->set('http_client', new Client(['handler' => $handler_stack]));
    $this->container->setAlias(ClientInterface::class, 'http_client');
  }

  /**
Loading