本文整理汇总了PHP中Drupal\Core\DependencyInjection\ContainerBuilder::get方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::get方法的具体用法?PHP ContainerBuilder::get怎么用?PHP ContainerBuilder::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\DependencyInjection\ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder::get方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: runApp
function runApp(ContainerBuilder $container)
{
$container->get('stopwatch')->start('example1');
// Calculate something.
$factorial = 1;
for ($x = 100; $x >= 1; $x--) {
$factorial = $factorial * $x;
}
dump($factorial);
$event = $container->get('stopwatch')->stop('example1');
dump($event->getEndTime() . " ms");
}
示例2: runApp
function runApp(ContainerBuilder $container)
{
$container->get('stopwatch')->start('Acquia D8 Workshop');
// Calculate something.
$factorial = 1;
for ($x = 100; $x >= 1; $x--) {
$factorial = $factorial * $x;
}
dump($factorial);
$stop = $container->get('stopwatch')->stop('Acquia D8 Workshop');
print_r($stop->getMemory() . "\n");
}
示例3: testGet
/**
* @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);
}
示例4: testGet
/**
* 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);
}
示例5: alter
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container)
{
// Disable configuration overrides.
// ConfigFactory would to try to load language overrides and InstallStorage
// throws an exception upon trying to load a non-existing file.
$container->get('config.factory')->setOverrideState(FALSE);
// No service may persist when the early installer kernel is rebooted into
// the production environment.
// @todo The DrupalKernel reboot performed by drupal_install_system() is
// actually not a "regular" reboot (like ModuleHandler::install()), so
// services are not actually persisted.
foreach ($container->findTaggedServiceIds('persist') as $id => $tags) {
$definition = $container->getDefinition($id);
$definition->clearTag('persist');
}
}
示例6: alter
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container)
{
if ($container->has('config.factory')) {
// The configuration factory depends on the cache factory, but that
// depends on the 'cache_default_bin_backends' parameter that has not yet
// been set by \Drupal\Core\Cache\ListCacheBinsPass::process() at this
// point.
$parameter_name = 'cache_default_bin_backends';
if (!$container->hasParameter($parameter_name)) {
$container->setParameter($parameter_name, []);
}
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $container->get('config.factory');
$config = $config_factory->get('libraries.settings');
if (!$config->isNew()) {
// Set the local definition path.
$container->getDefinition('libraries.definition.discovery.local')->replaceArgument(1, $config->get('definition.local.path'));
// Set the remote definition URL. Note that this is set even if
// the remote discovery is not enabled below in case the
// 'libraries.definition.discovery.remote' service is used explicitly.
$container->getDefinition('libraries.definition.discovery.remote')->replaceArgument(2, $config->get('definition.remote.url'));
// Because it is less convenient to remove a method call than to add
// one, the remote discovery is not registered in libraries.services.yml
// and instead added here, even though the 'definition.remote.enable'
// configuration value is TRUE by default.
if ($config->get('definition.remote.enable')) {
// Add the remote discovery to the list of chained discoveries.
$container->getDefinition('libraries.definition.discovery')->addMethodCall('addDiscovery', [new Reference('libraries.definition.discovery.remote')]);
}
}
// At this point the event dispatcher has not yet been populated with
// event subscribers by RegisterEventSubscribersPass::process() but has
// already bin injected in the configuration factory. Reset those services
// accordingly.
$container->set('event_dispatcher', NULL);
$container->set('config.factory', NULL);
}
}
示例7: containerBuild
/**
* 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);
}
示例8: __get
/**
* BC: Automatically resolve former KernelTestBase class properties.
*
* Test authors should follow the provided instructions and adjust their tests
* accordingly.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.2.0.
*/
public function __get($name)
{
if (in_array($name, array('public_files_directory', 'private_files_directory', 'temp_files_directory', 'translation_files_directory'))) {
// @comment it in again.
trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
switch ($name) {
case 'public_files_directory':
return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
case 'private_files_directory':
return $this->container->get('config.factory')->get('system.file')->get('path.private');
case 'temp_files_directory':
return file_directory_temp();
case 'translation_files_directory':
return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
}
}
if ($name === 'configDirectories') {
trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
return array(CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY));
}
$denied = array('testId', 'timeLimit', 'results', 'assertions', 'skipClasses', 'verbose', 'verboseId', 'verboseClassName', 'verboseDirectory', 'verboseDirectoryUrl', 'dieOnFail', 'kernel', 'generatedTestFiles', 'keyValueFactory');
if (in_array($name, $denied) || strpos($name, 'original') === 0) {
throw new \RuntimeException(sprintf('TestBase::$%s property no longer exists', $name));
}
}
示例9: testRebuildWithProviderBasedRoutes
/**
* Tests the rebuild with routes provided by a callback.
*
* @see \Drupal\Core\Routing\RouteBuilder::rebuild()
*/
public function testRebuildWithProviderBasedRoutes()
{
$this->lock->expects($this->once())->method('acquire')->with('router_rebuild')->will($this->returnValue(TRUE));
$this->yamlDiscovery->expects($this->once())->method('findAll')->will($this->returnValue(array('test_module' => array('route_callbacks' => array('\\Drupal\\Tests\\Core\\Routing\\TestRouteSubscriber::routesFromArray', 'test_module.route_service:routesFromCollection')))));
$container = new ContainerBuilder();
$container->set('test_module.route_service', new TestRouteSubscriber());
$this->controllerResolver->expects($this->any())->method('getControllerFromDefinition')->will($this->returnCallback(function ($controller) use($container) {
$count = substr_count($controller, ':');
if ($count == 1) {
list($service, $method) = explode(':', $controller, 2);
$object = $container->get($service);
} else {
list($class, $method) = explode('::', $controller, 2);
$object = new $class();
}
return array($object, $method);
}));
$route_collection_filled = new RouteCollection();
$route_collection_filled->add('test_route.1', new Route('/test-route/1'));
$route_collection_filled->add('test_route.2', new Route('/test-route/2'));
$route_build_event = new RouteBuildEvent($route_collection_filled);
// Ensure that the alter routes events are fired.
$this->dispatcher->expects($this->at(0))->method('dispatch')->with(RoutingEvents::DYNAMIC, $route_build_event);
$this->dispatcher->expects($this->at(1))->method('dispatch')->with(RoutingEvents::ALTER, $route_build_event);
// Ensure that the routes are set to the dumper and dumped.
$this->dumper->expects($this->at(0))->method('addRoutes')->with($route_collection_filled);
$this->dumper->expects($this->at(1))->method('dump');
$this->assertTrue($this->routeBuilder->rebuild());
}
示例10: render
/**
* Renders a render array.
*
* @param array $elements
* The elements to render.
*
* @return string
* The rendered string output (typically HTML).
*/
protected function render(array &$elements)
{
// \Drupal\Core\Render\BareHtmlPageRenderer::renderBarePage calls out to
// system_page_attachments() directly.
if (!\Drupal::moduleHandler()->moduleExists('system')) {
throw new \Exception(__METHOD__ . ' requires system module to be installed.');
}
// Use the bare HTML page renderer to render our links.
$renderer = $this->container->get('bare_html_page_renderer');
$response = $renderer->renderBarePage($elements, '', 'maintenance_page');
// Glean the content from the response object.
$content = $response->getContent();
$this->setRawContent($content);
$this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
return $content;
}