/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { // The following services are needed only on multilingual sites. if ($this->isMultilingual()) { $container->register('language_request_subscriber', 'Drupal\\language\\EventSubscriber\\LanguageRequestSubscriber')->addTag('event_subscriber')->addArgument(new Reference('language_manager'))->addArgument(new Reference('language_negotiator'))->addArgument(new Reference('string_translation'))->addArgument(new Reference('current_user')); $container->register('path_processor_language', 'Drupal\\language\\HttpKernel\\PathProcessorLanguage')->addTag('path_processor_inbound', array('priority' => 300))->addTag('path_processor_outbound', array('priority' => 100))->addArgument(new Reference('config.factory'))->addArgument(new Reference('language_manager'))->addArgument(new Reference('language_negotiator'))->addArgument(new Reference('current_user'))->addArgument(new Reference('language.config_subscriber'))->addMethodCall('initConfigSubscriber'); } }
/** * @inheritdoc */ public function alter(ContainerBuilder $container) { drush_log(dt("service modifier alter"), LogLevel::DEBUG); // http://symfony.com/doc/2.7/components/dependency_injection/tags.html#register-the-pass-with-the-container $container->register('drush.service.consolecommands', 'Drush\\Command\\ServiceCommandlist'); $container->addCompilerPass(new FindCommandsCompilerPass('drush.service.consolecommands', 'console.command')); $container->register('drush.service.consolidationcommands', 'Drush\\Command\\ServiceCommandlist'); $container->addCompilerPass(new FindCommandsCompilerPass('drush.service.consolidationcommands', 'consolidation.commandhandler')); }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { if (!empty($GLOBALS['conf']['update_service_provider_overrides'])) { // Disable the Lock service. $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend'); // Prevent config from being accessed via a cache wrapper by removing // any existing definition and setting an alias to the actual storage. $container->removeDefinition('config.storage'); $container->setAlias('config.storage', 'config.storage.active'); $container->register('cache_factory', 'Drupal\\Core\\Cache\\MemoryBackendFactory'); $container->register('router.builder', 'Drupal\\Core\\Routing\\RouteBuilderStatic'); $container->register('theme_handler', 'Drupal\\Core\\Extension\\ThemeHandler')->addArgument(new Reference('config.factory'))->addArgument(new Reference('module_handler'))->addArgument(new Reference('state'))->addArgument(new Reference('info_parser')); } }
public function setMinimalContainerPreKernel() { // Create a minimal mocked container to support calls to t() in the pre-kernel // base system verification code paths below. The strings are not actually // used or output for these calls. $container = new ContainerBuilder(); $container->setParameter('language.default_values', Language::$defaultValues); $container->register('language.default', 'Drupal\\Core\\Language\\LanguageDefault')->addArgument('%language.default_values%'); $container->register('string_translation', 'Drupal\\Core\\StringTranslation\\TranslationManager')->addArgument(new Reference('language.default')); // Register the stream wrapper manager. $container->register('stream_wrapper_manager', 'Drupal\\Core\\StreamWrapper\\StreamWrapperManager')->addMethodCall('setContainer', array(new Reference('service_container'))); $container->register('file_system', 'Drupal\\Core\\File\\FileSystem')->addArgument(new Reference('stream_wrapper_manager'))->addArgument(Settings::getInstance())->addArgument((new LoggerChannelFactory())->get('file')); \Drupal::setContainer($container); }
/** * Tests the get method. * * @see \Drupal\Core\DependencyInjection\Container::get() */ public function testGet() { $container = new ContainerBuilder(); $container->register('bar', 'BarClass'); $result = $container->get('bar'); $this->assertTrue($result instanceof \BarClass); }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { $container->addCompilerPass(new StoragePass()); if (FALSE !== $container->hasDefinition('profiler')) { $container->register('webprofiler.xhprof', 'Drupal\\xhprof\\DataCollector\\XHProfDataCollector')->addArgument(new Reference('xhprof.profiler'))->addTag('data_collector', array('template' => '@xhprof/Collector/xhprof.html.twig', 'id' => 'xhprof', 'title' => 'XHProf', 'priority' => 50)); } }
/** * @covers ::get */ public function testGet() { $container = new ContainerBuilder(); $container->register('bar', 'Drupal\\Tests\\Core\\DependencyInjection\\Fixture\\BarClass'); $result = $container->get('bar'); $this->assertTrue($result instanceof BarClass); }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { // Inject the special configuration storage for the installer. // This special implementation MUST NOT be used anywhere else than the early // installer environment. $container->register('config.storage', 'Drupal\\Core\\Config\\InstallStorage'); // Replace services with in-memory implementations. $definition = $container->getDefinition('cache_factory'); $definition->setClass('Drupal\\Core\\Cache\\MemoryBackendFactory'); $definition->setArguments(array()); $definition->setMethodCalls(array()); $container->register('keyvalue', 'Drupal\\Core\\KeyValueStore\\KeyValueMemoryFactory'); $container->register('keyvalue.expirable', 'Drupal\\Core\\KeyValueStore\\KeyValueNullExpirableFactory'); // Replace services with no-op implementations. $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend'); $container->register('url_generator', 'Drupal\\Core\\Routing\\NullGenerator')->addArgument(new Reference('request_stack')); $container->register('path_processor_manager', 'Drupal\\Core\\PathProcessor\\NullPathProcessorManager'); $container->register('router.dumper', 'Drupal\\Core\\Routing\\NullMatcherDumper'); // Remove the cache tags invalidator tag from the cache tags storage, so // that we don't call it when cache tags are invalidated very early in the // installer. $container->getDefinition('cache_tags.invalidator.checksum')->clearTag('cache_tags_invalidator'); // Replace the route builder with an empty implementation. // @todo Convert installer steps into routes; add an installer.routing.yml. $definition = $container->getDefinition('router.builder'); $definition->setClass('Drupal\\Core\\Installer\\InstallerRouteBuilder')->setLazy(FALSE); }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { // Add a compiler pass to discover all data collector services. $container->addCompilerPass(new ProfilerPass()); // Add a compiler pass to discover all available storage backend. $container->addCompilerPass(new StoragePass()); $container->addCompilerPass(new ServicePass(), PassConfig::TYPE_AFTER_REMOVING); $container->addCompilerPass(new EventPass(), PassConfig::TYPE_AFTER_REMOVING); $modules = $container->getParameter('container.modules'); // Add ViewsDataCollector only if Views module is enabled. if (isset($modules['views'])) { $container->register('webprofiler.views', 'Drupal\\webprofiler\\DataCollector\\ViewsDataCollector')->addArgument(new Reference('views.executable'))->addArgument(new Reference('entity.manager'))->addTag('data_collector', ['template' => '@webprofiler/Collector/views.html.twig', 'id' => 'views', 'title' => 'Views', 'priority' => 75]); } // Add BlockDataCollector only if Block module is enabled. if (isset($modules['block'])) { $container->register('webprofiler.blocks', 'Drupal\\webprofiler\\DataCollector\\BlocksDataCollector')->addArgument(new Reference('entity.manager'))->addTag('data_collector', ['template' => '@webprofiler/Collector/blocks.html.twig', 'id' => 'blocks', 'title' => 'Blocks', 'priority' => 78]); } // Add TranslationsDataCollector only if Locale module is enabled. if (isset($modules['locale'])) { $container->register('webprofiler.translations', 'Drupal\\webprofiler\\DataCollector\\TranslationsDataCollector')->addArgument(new Reference('string_translation'))->addArgument(new Reference('url_generator'))->addTag('data_collector', ['template' => '@webprofiler/Collector/translations.html.twig', 'id' => 'translations', 'title' => 'Translations', 'priority' => 210]); } }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { // Inject the special configuration storage for the installer. // This special implementation MUST NOT be used anywhere else than the early // installer environment. $container->register('config.storage', 'Drupal\\Core\\Config\\InstallStorage'); // Replace services with in-memory implementations. $definition = $container->getDefinition('cache_factory'); $definition->setClass('Drupal\\Core\\Cache\\MemoryBackendFactory'); $definition->setArguments(array()); $definition->setMethodCalls(array()); $container->register('keyvalue', 'Drupal\\Core\\KeyValueStore\\KeyValueMemoryFactory'); $container->register('keyvalue.expirable', 'Drupal\\Core\\KeyValueStore\\KeyValueNullExpirableFactory'); // Replace services with no-op implementations. $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend'); $container->register('url_generator', 'Drupal\\Core\\Routing\\NullGenerator')->addArgument(new Reference('request_stack')); $container->register('router.dumper', 'Drupal\\Core\\Routing\\NullMatcherDumper'); // Replace the route builder with an empty implementation. // @todo Convert installer steps into routes; add an installer.routing.yml. $definition = $container->getDefinition('router.builder'); $definition->setClass('Drupal\\Core\\Installer\\InstallerRouteBuilder'); // Remove dependencies on Drupal's default session handling. $container->removeDefinition('authentication.cookie'); }
/** * Registers services and event subscribers for a site under test. */ protected function registerTest(ContainerBuilder $container) { // Do nothing if we are not in a test environment. if (!drupal_valid_test_ua()) { return; } // Add the HTTP request subscriber to Guzzle. $container->register('test.http_client.request_subscriber', 'Drupal\\Core\\Test\\EventSubscriber\\HttpRequestSubscriber')->addTag('http_client_subscriber'); }
/** * Sets up the base service container for this test. * * Extend this method in your test to register additional service overrides * that need to persist a DrupalKernel reboot. This method is called whenever * the kernel is rebuilt. * * @see \Drupal\simpletest\KernelTestBase::setUp() * @see \Drupal\simpletest\KernelTestBase::enableModules() * @see \Drupal\simpletest\KernelTestBase::disableModules() */ public function containerBuild(ContainerBuilder $container) { // Keep the container object around for tests. $this->container = $container; // Set the default language on the minimal container. $this->container->setParameter('language.default_values', $this->defaultLanguageData()); $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend'); $container->register('cache_factory', 'Drupal\\Core\\Cache\\MemoryBackendFactory'); $container->register('config.storage', 'Drupal\\Core\\Config\\DatabaseStorage')->addArgument(Database::getConnection())->addArgument('config'); if ($this->strictConfigSchema) { $container->register('simpletest.config_schema_checker', 'Drupal\\Core\\Config\\Testing\\ConfigSchemaChecker')->addArgument(new Reference('config.typed'))->addArgument($this->getConfigSchemaExclusions())->addTag('event_subscriber'); } $keyvalue_options = $container->getParameter('factory.keyvalue') ?: array(); $keyvalue_options['default'] = 'keyvalue.memory'; $container->setParameter('factory.keyvalue', $keyvalue_options); $container->set('keyvalue.memory', $this->keyValueFactory); if (!$container->has('keyvalue')) { // TestBase::setUp puts a completely empty container in // $this->container which is somewhat the mirror of the empty // environment being set up. Unit tests need not to waste time with // getting a container set up for them. Drupal Unit Tests might just get // away with a simple container holding the absolute bare minimum. When // a kernel is overridden then there's no need to re-register the keyvalue // service but when a test is happy with the superminimal container put // together here, it still might a keyvalue storage for anything using // \Drupal::state() -- that's why a memory service was added in the first // place. $container->register('settings', 'Drupal\\Core\\Site\\Settings')->setFactoryClass('Drupal\\Core\\Site\\Settings')->setFactoryMethod('getInstance'); $container->register('keyvalue', 'Drupal\\Core\\KeyValueStore\\KeyValueFactory')->addArgument(new Reference('service_container'))->addArgument(new Parameter('factory.keyvalue')); $container->register('state', 'Drupal\\Core\\State\\State')->addArgument(new Reference('keyvalue')); } if ($container->hasDefinition('path_processor_alias')) { // Prevent the alias-based path processor, which requires a url_alias db // table, from being registered to the path processor manager. We do this // by removing the tags that the compiler pass looks for. This means the // url generator can safely be used within tests. $definition = $container->getDefinition('path_processor_alias'); $definition->clearTag('path_processor_inbound')->clearTag('path_processor_outbound'); } if ($container->hasDefinition('password')) { $container->getDefinition('password')->setArguments(array(1)); } // Register the stream wrapper manager. $container->register('stream_wrapper_manager', 'Drupal\\Core\\StreamWrapper\\StreamWrapperManager')->addArgument(new Reference('module_handler'))->addMethodCall('setContainer', array(new Reference('service_container'))); $request = Request::create('/'); $container->get('request_stack')->push($request); }
/** * Registers test-specific services. * * Extend this method in your test to register additional services. This * method is called whenever the kernel is rebuilt. * * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container * The service container to enhance. * * @see \Drupal\Tests\KernelTestBase::bootKernel() */ public function register(ContainerBuilder $container) { // Keep the container object around for tests. $this->container = $container; $container->register('flood', 'Drupal\\Core\\Flood\\MemoryBackend')->addArgument(new Reference('request_stack')); $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend'); $container->register('cache_factory', 'Drupal\\Core\\Cache\\MemoryBackendFactory'); $container->register('keyvalue.memory', 'Drupal\\Core\\KeyValueStore\\KeyValueMemoryFactory')->addTag('persist'); $container->setAlias('keyvalue', 'keyvalue.memory'); // Set the default language on the minimal container. $container->setParameter('language.default_values', Language::$defaultValues); if ($this->strictConfigSchema) { $container->register('simpletest.config_schema_checker', 'Drupal\\Core\\Config\\Testing\\ConfigSchemaChecker')->addArgument(new Reference('config.typed'))->addArgument($this->getConfigSchemaExclusions())->addTag('event_subscriber'); } if ($container->hasDefinition('path_processor_alias')) { // Prevent the alias-based path processor, which requires a url_alias db // table, from being registered to the path processor manager. We do this // by removing the tags that the compiler pass looks for. This means the // url generator can safely be used within tests. $container->getDefinition('path_processor_alias')->clearTag('path_processor_inbound')->clearTag('path_processor_outbound'); } if ($container->hasDefinition('password')) { $container->getDefinition('password')->setArguments(array(1)); } TestServiceProvider::addRouteProvider($container); }
/** * Runs the install database tasks for the driver used by the test runner. */ protected function runDbTasks() { // Create a minimal container so that t() works. // @see install_begin_request() $container = new ContainerBuilder(); $container->setParameter('language.default_values', Language::$defaultValues); $container->register('language.default', 'Drupal\\Core\\Language\\LanguageDefault')->addArgument('%language.default_values%'); $container->register('language_manager', 'Drupal\\Core\\Language\\LanguageManager')->addArgument(new Reference('language.default')); $container->register('string_translation', 'Drupal\\Core\\StringTranslation\\TranslationManager')->addArgument(new Reference('language_manager')); \Drupal::setContainer($container); require_once __DIR__ . '/../../../../../includes/install.inc'; $connection = Database::getConnection(); $errors = db_installer_object($connection->driver())->runTasks(); if (!empty($errors)) { $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors)); } }
/** * Registers test-specific services. * * Extend this method in your test to register additional services. This * method is called whenever the kernel is rebuilt. * * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container * The service container to enhance. * * @see \Drupal\Tests\KernelTestBase::bootKernel() */ public function register(ContainerBuilder $container) { // Keep the container object around for tests. $this->container = $container; $container ->register('flood', 'Drupal\Core\Flood\MemoryBackend') ->addArgument(new Reference('request_stack')); $container ->register('lock', 'Drupal\Core\Lock\NullLockBackend'); $container ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory'); $container ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory') // Must persist container rebuilds, or all data would vanish otherwise. ->addTag('persist'); $container ->setAlias('keyvalue', 'keyvalue.memory'); if ($this->strictConfigSchema) { $container ->register('simpletest.config_schema_checker', 'Drupal\Core\Config\Testing\ConfigSchemaChecker') ->addArgument(new Reference('config.typed')) ->addTag('event_subscriber'); } if ($container->hasDefinition('path_processor_alias')) { // Prevent the alias-based path processor, which requires a url_alias db // table, from being registered to the path processor manager. We do this // by removing the tags that the compiler pass looks for. This means the // url generator can safely be used within tests. $container->getDefinition('path_processor_alias') ->clearTag('path_processor_inbound') ->clearTag('path_processor_outbound'); } if ($container->hasDefinition('password')) { $container->getDefinition('password') ->setArguments(array(1)); } }
/** * Registers services and event subscribers for a site under test. */ protected function registerTest(ContainerBuilder $container) { // Do nothing if we are not in a test environment. if (!drupal_valid_test_ua()) { return; } // Add the HTTP request middleware to Guzzle. $container->register('test.http_client.middleware', 'Drupal\\Core\\Test\\HttpClientMiddleware\\TestHttpClientMiddleware')->addTag('http_client_middleware'); }
/** * {@inheritdoc} */ public function register(ContainerBuilder $container) { $container->register('router_test.subscriber', 'Drupal\\router_test\\RouteTestSubscriber')->addTag('event_subscriber'); $container->register('access_check.router_test', 'Drupal\\router_test\\Access\\TestAccessCheck')->addTag('access_check', array('applies_to' => '_access_router_test')); }
/** * @covers ::process * * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException */ public function testContainerWithLazyServicesWithoutProxyClass() { $container = new ContainerBuilder(); $container->register('alias_whitelist', 'Drupal\\Core\\Path\\AliasWhitelist')->setLazy(TRUE); $this->proxyServicesPass->process($container); }
/** * Test that the source count is correct. */ public function testCount() { $container = new ContainerBuilder(); $container->register('cache.migrate', 'Drupal\\Core\\Cache\\NullBackend')->setArguments(['migrate']); \Drupal::setContainer($container); // Test that the basic count works. $source = $this->getSource(); $this->assertEquals(1, $source->count()); // Test caching the count works. $source = $this->getSource(['cache_counts' => TRUE]); $this->assertEquals(1, $source->count()); // Test the skip argument. $source = $this->getSource(['skip_count' => TRUE]); $this->assertEquals(-1, $source->count()); }
/** * {@inheritdoc} */ public function alter(ContainerBuilder $container) { // Remove the basic content negotation middleware and replace it with a // basic header based one. $container->register('http_middleware.negotiation', 'Drupal\\accept_header_routing_test\\AcceptHeaderMiddleware')->addTag('http_middleware', ['priority' => 400]); }
/** * @covers ::register * @expectedException \InvalidArgumentException * @expectedExceptionMessage Service ID names must be lowercase: Bar */ public function testRegisterException() { $container = new ContainerBuilder(); $container->register('Bar'); }