本文整理汇总了PHP中Drupal\Core\DependencyInjection\ContainerBuilder类的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder类的具体用法?PHP ContainerBuilder怎么用?PHP ContainerBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ContainerBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
/**
* {@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));
}
}
示例2: testConstructor
/**
* Tests the constructor assignment of actions.
*/
public function testConstructor()
{
$actions = array();
for ($i = 1; $i <= 2; $i++) {
$action = $this->getMock('\\Drupal\\system\\ActionConfigEntityInterface');
$action->expects($this->any())->method('getType')->will($this->returnValue('user'));
$actions[$i] = $action;
}
$action = $this->getMock('\\Drupal\\system\\ActionConfigEntityInterface');
$action->expects($this->any())->method('getType')->will($this->returnValue('node'));
$actions[] = $action;
$entity_storage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage->expects($this->any())->method('loadMultiple')->will($this->returnValue($actions));
$views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')->disableOriginalConstructor()->getMock();
$views_data->expects($this->any())->method('get')->with('users')->will($this->returnValue(array('table' => array('entity type' => 'user'))));
$container = new ContainerBuilder();
$container->set('views.views_data', $views_data);
\Drupal::setContainer($container);
$storage = $this->getMock('Drupal\\views\\ViewStorageInterface');
$storage->expects($this->any())->method('get')->with('base_table')->will($this->returnValue('users'));
$executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->getMock();
$executable->storage = $storage;
$display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock();
$definition['title'] = '';
$options = array();
$user_bulk_form = new UserBulkForm(array(), 'user_bulk_form', $definition, $entity_storage);
$user_bulk_form->init($executable, $display, $options);
$this->assertAttributeEquals(array_slice($actions, 0, -1, TRUE), 'actions', $user_bulk_form);
}
示例3: testTitleQuery
/**
* Tests the title_query method.
*
* @see \Drupal\user\Plugin\views\argument\RolesRid::title_query()
*/
public function testTitleQuery()
{
$role1 = new Role(array('id' => 'test_rid_1', 'label' => 'test rid 1'), 'user_role');
$role2 = new Role(array('id' => 'test_rid_2', 'label' => 'test <strong>rid 2</strong>'), 'user_role');
// Creates a stub entity storage;
$role_storage = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityStorageInterface');
$role_storage->expects($this->any())->method('loadMultiple')->will($this->returnValueMap(array(array(array(), array()), array(array('test_rid_1'), array('test_rid_1' => $role1)), array(array('test_rid_1', 'test_rid_2'), array('test_rid_1' => $role1, 'test_rid_2' => $role2)))));
$entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type->expects($this->any())->method('getKey')->with('label')->will($this->returnValue('label'));
$entity_manager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$entity_manager->expects($this->any())->method('getDefinition')->with($this->equalTo('user_role'))->will($this->returnValue($entity_type));
$entity_manager->expects($this->once())->method('getStorage')->with($this->equalTo('user_role'))->will($this->returnValue($role_storage));
// @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
// entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
// the entity manager until this is fixed.
$container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager);
\Drupal::setContainer($container);
$roles_rid_argument = new RolesRid(array(), 'user__roles_rid', array(), $entity_manager);
$roles_rid_argument->value = array();
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array(), $titles);
$roles_rid_argument->value = array('test_rid_1');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1'), $titles);
$roles_rid_argument->value = array('test_rid_1', 'test_rid_2');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1', String::checkPlain('test <strong>rid 2</strong>')), $titles);
}
示例4: testSymfonyGuesserRegistration
/**
* @covers ::registerWithSymfonyGuesser
*
* @see Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
*/
public function testSymfonyGuesserRegistration()
{
// Make the guessers property accessible on Symfony's MimeTypeGuesser.
$symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
// Test that the Drupal mime type guess is not being used before the
// override method is called. It is possible that the test environment does
// not support the default guessers.
$guessers = $this->readAttribute($symfony_guesser, 'guessers');
if (count($guessers)) {
$this->assertNotInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
}
$container = new ContainerBuilder();
$container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager()));
MimeTypeGuesser::registerWithSymfonyGuesser($container);
$symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
$guessers = $this->readAttribute($symfony_guesser, 'guessers');
$this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]);
$this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
$count = count($guessers);
$container = new ContainerBuilder();
$container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager()));
MimeTypeGuesser::registerWithSymfonyGuesser($container);
$symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
$guessers = $this->readAttribute($symfony_guesser, 'guessers');
$this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]);
$this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
$new_count = count($guessers);
$this->assertEquals($count, $new_count, 'The count of mime type guessers remains the same after container re-init.');
}
示例5: testCreate
/**
* @covers ::create
* @covers ::__construct
*/
public function testCreate()
{
$container = new ContainerBuilder();
$container->set('keyvalue.expirable', $this->prophesize('Drupal\\Core\\KeyValueStore\\KeyValueExpirableFactoryInterface')->reveal());
$container->set('entity.manager', $this->entityManager->reveal());
$this->assertInstanceOf('Drupal\\feeds\\Controller\\SubscriptionController', SubscriptionController::create($container));
}
示例6: testSet
/**
* Tests the set() method.
*
* @covers ::set
*/
public function testSet()
{
$container = new ContainerBuilder();
$class = new BarClass();
$container->set('bar', $class);
$this->assertEquals('bar', $class->_serviceId);
}
示例7: register
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container)
{
// Add a compiler pass for adding Normalizers and Encoders to Serializer.
$container->addCompilerPass(new RegisterSerializationClassesCompilerPass());
// Add a compiler pass for adding concrete Resolvers to chain Resolver.
$container->addCompilerPass(new RegisterEntityResolversCompilerPass());
}
示例8: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
// Create a config mock which does not mock the clear(), set() and get() methods.
$methods = get_class_methods('Drupal\\Core\\Config\\Config');
unset($methods[array_search('set', $methods)]);
unset($methods[array_search('get', $methods)]);
unset($methods[array_search('clear', $methods)]);
$config_mock = $this->getMockBuilder('Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->setMethods($methods)->getMock();
// Create the config factory we use in the submitForm() function.
$this->configFactory = $this->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory->expects($this->any())->method('getEditable')->will($this->returnValue($config_mock));
// Create a MailsystemManager mock.
$this->mailManager = $this->getMock('\\Drupal\\mailsystem\\MailsystemManager', array(), array(), '', FALSE);
$this->mailManager->expects($this->any())->method('getDefinition')->will($this->returnValueMap(array(array('mailsystem_test', TRUE, array('label' => 'Test Mail-Plugin')), array('mailsystem_demo', TRUE, array('label' => 'Demo Mail-Plugin')))));
$this->mailManager->expects($this->any())->method('getDefinitions')->will($this->returnValue(array(array('id' => 'mailsystem_test', 'label' => 'Test Mail-Plugin'), array('id' => 'mailsystem_demo', 'label' => 'Demo Mail-Plugin'))));
// Create a module handler mock.
$this->moduleHandler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->moduleHandler->expects($this->any())->method('getImplementations')->with('mail')->will($this->returnValue(array('mailsystem_test', 'mailsystem_demo')));
$this->moduleHandler->expects($this->any())->method('moduleExists')->withAnyParameters()->will($this->returnValue(FALSE));
// Create a theme handler mock.
$this->themeHandler = $this->getMock('\\Drupal\\Core\\Extension\\ThemeHandlerInterface');
$this->themeHandler->expects($this->any())->method('listInfo')->will($this->returnValue(array('test_theme' => (object) array('status' => 1, 'info' => array('name' => 'test theme name')), 'demo_theme' => (object) array('status' => 1, 'info' => array('name' => 'test theme name demo')), 'inactive_theme' => (object) array('status' => 0, 'info' => array('name' => 'inactive test theme')))));
// Inject a language-manager into \Drupal.
$this->languageManager = $this->getMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$this->languageManager->expects($this->any())->method('translate')->withAnyParameters()->will($this->returnArgument(0));
$container = new ContainerBuilder();
$container->set('string_translation', $this->languageManager);
\Drupal::setContainer($container);
}
示例9: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// TODO: Change the autogenerated stub
$condition_plugin_manager = $this->getMock('Drupal\\Core\\Executable\\ExecutableManagerInterface');
$condition_plugin_manager->expects($this->any())->method('getDefinitions')->will($this->returnValue(array()));
$container = new ContainerBuilder();
$container->set('plugin.manager.condition', $condition_plugin_manager);
\Drupal::setContainer($container);
$this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])->getMock();
$this->executable->expects($this->any())->method('setDisplay')->with('block_1')->will($this->returnValue(TRUE));
$this->executable->expects($this->any())->method('getShowAdminLinks')->willReturn(FALSE);
$this->executable->display_handler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->setMethods(NULL)->getMock();
$this->view = $this->getMockBuilder('Drupal\\views\\Entity\\View')->disableOriginalConstructor()->getMock();
$this->view->expects($this->any())->method('id')->willReturn('test_view');
$this->executable->storage = $this->view;
$this->executableFactory = $this->getMockBuilder('Drupal\\views\\ViewExecutableFactory')->disableOriginalConstructor()->getMock();
$this->executableFactory->expects($this->any())->method('get')->with($this->view)->will($this->returnValue($this->executable));
$this->displayHandler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->getMock();
$this->displayHandler->expects($this->any())->method('blockSettings')->willReturn([]);
$this->displayHandler->expects($this->any())->method('getPluginId')->willReturn('block');
$this->executable->display_handler = $this->displayHandler;
$this->storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->any())->method('load')->with('test_view')->will($this->returnValue($this->view));
$this->account = $this->getMock('Drupal\\Core\\Session\\AccountInterface');
}
示例10: register
/**
* Registers services to the container.
*
* @param ContainerBuilder $container
* The ContainerBuilder to register services to.
*/
public function register(ContainerBuilder $container)
{
static::$container = $container;
$class_map = $this->AutoloaderInit();
$ns = [];
foreach ($class_map as $name => $path) {
$path = preg_replace("/\\.php\$/i", '', $path);
$path = explode(DIRECTORY_SEPARATOR, $path);
$name = explode('\\', $name);
while (end($path) === end($name)) {
array_pop($path);
array_pop($name);
}
$path = implode(DIRECTORY_SEPARATOR, $path);
$name = implode('\\', $name);
if (is_dir($path)) {
if (is_dir($path . '/Plugin') || is_dir($path . '/Entity') || is_dir($path . '/Element')) {
$ns[$name][] = $path;
}
}
}
foreach ($ns as $name => $path) {
$path = array_unique($path);
$ns[$name] = count($path) == 1 ? reset($path) : $path;
}
$ns += $container->getParameter('container.namespaces');
$container->setParameter('container.namespaces', $ns);
$yaml_loader = new YamlFileLoader($container);
foreach (static::getFiles('/^.+\\.services\\.yml$/i') as $file) {
$yaml_loader->load($file);
}
}
示例11: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->pathValidator = $this->getMock('Drupal\\Core\\Path\\PathValidatorInterface');
$container = new ContainerBuilder();
$container->set('path.validator', $this->pathValidator);
\Drupal::setContainer($container);
}
示例12: 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);
$this->assertEquals('bar', $result->_serviceId);
}
示例13: alter
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container)
{
if ($container->has('file.usage')) {
// Override the class used for the file.usage service.
$definition = $container->getDefinition('file.usage');
$definition->setClass('Drupal\\service_provider_test\\TestFileUsage');
}
}
示例14: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$container = new ContainerBuilder();
$this->manager = $this->prophesize(PluginManagerInterface::class);
$container->set('plugin.manager.display_variant', $this->manager->reveal());
\Drupal::setContainer($container);
}
示例15: test
public function test()
{
$container = new ContainerBuilder();
$container->set('event_dispatcher', $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'));
$mock = $this->getMockForAbstractClass('Drupal\\feeds\\FeedHandlerBase', [], '', FALSE);
$mock_class = get_class($mock);
$hander = $mock_class::createInstance($container, $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'));
}