Commit 2c22ebbd authored by alexpott's avatar alexpott

Issue #2380293 revert by dawehner, willzyx, mpdonadio, jibran, alexpott,...

Issue #2380293 revert by dawehner, willzyx, mpdonadio, jibran, alexpott, damiankloip, cilefen, Berdir: Properly inject services into ModuleInstaller
parent 60d994a2
......@@ -396,9 +396,6 @@ services:
logger.channel.form:
parent: logger.channel_base
arguments: ['form']
logger.channel.system:
parent: logger.channel_base
arguments: ['system']
logger.log_message_parser:
class: Drupal\Core\Logger\LogMessageParser
......@@ -479,9 +476,9 @@ services:
arguments: ['@app.root', '%container.modules%', '@cache.bootstrap']
module_installer:
class: Drupal\Core\Extension\ModuleInstaller
arguments: ['@app.root', '@module_handler', '@kernel', '@config.factory', '@config.installer', '@plugin.cache_clearer', '@router.builder', '@stream_wrapper_manager', '@theme_handler', '@logger.channel.system', '@config.manager', '@keyvalue']
tags:
- { name: service_collector, tag: 'module_install.uninstall_validator', call: addUninstallValidator }
arguments: ['@app.root', '@module_handler', '@kernel', '@router.builder']
lazy: true
content_uninstall_validator:
class: Drupal\Core\Entity\ContentUninstallValidator
......
<?php
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a trait which automatically updates dependencies from the container.
*
* Classes which trigger a container rebuild should point to the instances of
* the services with the new container. Calling the ::updateDependencies()
* method takes care of that.
*
* If the service depends on container parameters and if they can possibly
* change then the service will need to handle this itself.
*/
trait UpdateDependenciesTrait {
/**
* Updates an object's external dependencies from the container.
*
* This method depends on \Drupal\Core\DependencyInjection\Container::get()
* adding the _serviceId property to all services.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
*
* @see \Drupal\Core\DependencyInjection\Container
*/
protected function updateDependencies(ContainerInterface $container) {
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if (is_object($value) && isset($value->_serviceId)) {
$this->$key = $container->get($value->_serviceId);
}
// Special case the container, which might not have a service ID.
elseif ($value instanceof ContainerInterface) {
$this->$key = $container;
}
}
}
}
<?php
namespace Drupal\Tests\Core\DependencyInjection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\UpdateDependenciesTrait;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @coversDefaultClass \Drupal\Core\DependencyInjection\UpdateDependenciesTrait
* @group DependencyInjection
*/
class UpdateDependenciesTraitTest extends UnitTestCase {
/**
* @covers ::updateDependencies
*/
public function testUpdateDependencies() {
$service = new \StdClass();
$service->_serviceId = 'test_service';
$container = new ContainerBuilder();
$container->set('test_service', $service);
$trait_test = new UpdateDependenciesTraitTestClass();
$trait_test->dependency = clone $service;
$trait_test->container = $container;
$object = new \StdClass();
$trait_test->object = $object;
// Ensure that the service and container are updated but other objects are
// untouched.
$trait_test->updateDependenciesPublic($container);
$this->assertSame($service, $trait_test->dependency);
$this->assertSame($container, $trait_test->container);
$this->assertSame($object, $trait_test->object);
}
}
class UpdateDependenciesTraitTestClass {
use UpdateDependenciesTrait;
/**
* Updates dependencies.
*
* A wrapper around the protected UpdateDependenciesTrait::updateDependencies
* method.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
*/
public function updateDependenciesPublic(ContainerInterface $container) {
$this->updateDependencies($container);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment