本文整理汇总了PHP中Symfony\Component\EventDispatcher\EventDispatcher类的典型用法代码示例。如果您正苦于以下问题:PHP EventDispatcher类的具体用法?PHP EventDispatcher怎么用?PHP EventDispatcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventDispatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testRecursiveMessage
public function testRecursiveMessage()
{
$dispatcher = new EventDispatcher();
$backend = new PostponeRuntimeBackend($dispatcher, true);
$dispatcher->addListener('kernel.terminate', array($backend, 'onEvent'));
$message1 = $backend->create('notification.demo1', array());
$message2 = $backend->create('notification.demo2', array());
$backend->publish($message1);
$phpunit = $this;
$phpunit->passed1 = false;
$phpunit->passed2 = false;
$dispatcher->addListener('notification.demo1', function (ConsumerEventInterface $event) use($phpunit, $message1, $message2, $backend, $dispatcher) {
$phpunit->assertSame($message1, $event->getMessage());
$phpunit->passed1 = true;
$backend->publish($message2);
});
$dispatcher->addListener('notification.demo2', function (ConsumerEventInterface $event) use($phpunit, $message2) {
$phpunit->assertSame($message2, $event->getMessage());
$phpunit->passed2 = true;
});
$dispatcher->dispatch('kernel.terminate');
$this->assertTrue($phpunit->passed1);
$this->assertTrue($phpunit->passed2);
$this->assertEquals(MessageInterface::STATE_DONE, $message1->getState());
$this->assertEquals(MessageInterface::STATE_DONE, $message2->getState());
}
示例2: shouldSortSimpleDoctrineQuery
/**
* @test
*/
function shouldSortSimpleDoctrineQuery()
{
$em = $this->getMockSqliteEntityManager();
$this->populate($em);
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new PaginationSubscriber());
$dispatcher->addSubscriber(new Sortable());
$p = new Paginator($dispatcher);
$_GET['sort'] = 'a.title';
$_GET['direction'] = 'asc';
$this->startQueryLog();
$query = $this->em->createQuery('SELECT a FROM Test\\Fixture\\Entity\\Article a');
$view = $p->paginate($query, 1, 10);
$items = $view->getItems();
$this->assertEquals(4, count($items));
$this->assertEquals('autumn', $items[0]->getTitle());
$this->assertEquals('spring', $items[1]->getTitle());
$this->assertEquals('summer', $items[2]->getTitle());
$this->assertEquals('winter', $items[3]->getTitle());
$_GET['direction'] = 'desc';
$view = $p->paginate($query, 1, 10);
$items = $view->getItems();
$this->assertEquals(4, count($items));
$this->assertEquals('winter', $items[0]->getTitle());
$this->assertEquals('summer', $items[1]->getTitle());
$this->assertEquals('spring', $items[2]->getTitle());
$this->assertEquals('autumn', $items[3]->getTitle());
$this->assertEquals(6, $this->queryAnalyzer->getNumExecutedQueries());
$executed = $this->queryAnalyzer->getExecutedQueries();
$this->assertEquals('SELECT DISTINCT a0_.id AS id0, a0_.title AS title1 FROM Article a0_ ORDER BY a0_.title ASC LIMIT 10 OFFSET 0', $executed[1]);
$this->assertEquals('SELECT DISTINCT a0_.id AS id0, a0_.title AS title1 FROM Article a0_ ORDER BY a0_.title DESC LIMIT 10 OFFSET 0', $executed[4]);
}
示例3: testListen
/**
* Verifies that all events are logged.
*/
public function testListen()
{
// create the test sources
mkdir($this->dir . '/src/a', 0755, true);
mkdir($this->dir . '/src/b', 0755, true);
mkdir($this->dir . '/src/c', 0755, true);
file_put_contents($this->dir . '/src/a/a', 'a');
file_put_contents($this->dir . '/src/b/b', 'b');
file_put_contents($this->dir . '/src/c/c', 'c');
// create the logger
$handler = new TestHandler();
$logger = new Logger('test');
$logger->pushHandler($handler);
// create the event dispatcher and register the subscriber
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new LoggerSubscriber($logger));
// register the dispatcher with the builder
$this->builder->setEventDispatcher($dispatcher);
// build the archive
$this->builder->addEmptyDir('path/to/d');
$this->builder->addFile($this->dir . '/src/a/a', 'path/to/e');
$this->builder->addFromString('path/to/f', 'f');
$this->builder->buildFromDirectory($this->dir . '/src/b', '/(^test)/');
$this->builder->buildFromIterator(new ArrayIterator(array('c/c' => $this->dir . '/src/c/c')), $this->dir . '/src/');
$this->builder->setStub('<?php __HALT_COMPILER();');
// make sure the log is what we expected
$records = $handler->getRecords();
$expected = array(array('message' => 'The empty directory "d" is about to be added.', 'context' => array('local' => 'path/to/d')), array('message' => 'The empty directory "d" has been added.', 'context' => array('local' => 'path/to/d')), array('message' => 'The file "a" is about to be added as "e".', 'context' => array('file' => $this->dir . '/src/a/a', 'local' => 'path/to/e')), array('message' => 'The string is about to be added as "e".', 'context' => array('local' => 'path/to/e')), array('message' => 'The string has been added as "e".', 'context' => array('local' => 'path/to/e')), array('message' => 'The file "a" has been added as "e".', 'context' => array('file' => $this->dir . '/src/a/a', 'local' => 'path/to/e')), array('message' => 'The string is about to be added as "f".', 'context' => array('local' => 'path/to/f')), array('message' => 'The string has been added as "f".', 'context' => array('local' => 'path/to/f')), array('message' => 'The directory "b" is about to be added.', 'context' => array('filter' => '/(^test)/', 'path' => $this->dir . '/src/b')), array('message' => 'The items from the "RegexIterator" iterator are about to be added.', 'context' => array('base' => $this->dir . '/src/b', 'class' => 'Box\\Component\\Builder\\Iterator\\RegexIterator')), array('message' => 'The items from the "RegexIterator" iterator have been added.', 'context' => array('base' => $this->dir . '/src/b', 'class' => 'Box\\Component\\Builder\\Iterator\\RegexIterator')), array('message' => 'The directory "b" has been added.', 'context' => array('filter' => '/(^test)/', 'path' => $this->dir . '/src/b')), array('message' => 'The items from the "ArrayIterator" iterator are about to be added.', 'context' => array('base' => $this->dir . '/src/', 'class' => 'ArrayIterator')), array('message' => 'The path "c" is about to be added as "c".', 'context' => array('path' => $this->dir . '/src/c/c', 'local' => 'c/c')), array('message' => 'The items from the "ArrayIterator" iterator have been added.', 'context' => array('base' => $this->dir . '/src/', 'class' => 'ArrayIterator')), array('message' => 'The custom stub is about to be set.', 'context' => array()), array('message' => 'The custom stub has been set.', 'context' => array()));
foreach ($expected as $i => $e) {
self::assertEquals($e['message'], $records[$i]['message']);
self::assertEquals($e['context'], $records[$i]['context']);
}
}
示例4: setUp
public function setUp()
{
$dispatcher = new EventDispatcher();
$this->wsdl = new DefinitionsReader(null, $dispatcher);
$this->soap = new SoapReader();
$dispatcher->addSubscriber($this->soap);
}
示例5: getEventDispatcher
/**
* @return EventDispatcher
*/
protected function getEventDispatcher()
{
$dispatcher = new EventDispatcher();
$redirect_subsciber = new RedirectSubscriber();
$dispatcher->addSubscriber($redirect_subsciber);
return $dispatcher;
}
示例6: setUp
public function setUp()
{
$this->requestStack = new RequestStack();
$request = new Request();
$this->requestStack->push($request);
$this->session = new Session(new MockArraySessionStorage());
$request->setSession($this->session);
$this->dispatcher = new EventDispatcher();
$translator = new Translator($this->getMock('\\Symfony\\Component\\DependencyInjection\\ContainerInterface'));
$token = new TokenProvider($this->requestStack, $translator, 'test');
$this->dispatcher->addSubscriber(new \Thelia\Action\Cart($this->requestStack, $token));
$this->session->setSessionCart(null);
$request->setSession($this->session);
/** @var \Thelia\Action\Cart cartAction */
$this->cartAction = new \Thelia\Action\Cart($this->requestStack, new TokenProvider($this->requestStack, $translator, 'baba au rhum'));
$this->dispatcherNull = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
$this->dispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface', array(), array(), '', true, true, true, false);
$this->dispatcher->expects($this->any())->method('dispatch')->will($this->returnCallback(function ($type, $event) {
if ($type == TheliaEvents::CART_RESTORE_CURRENT) {
$this->cartAction->restoreCurrentCart($event, null, $this->dispatcher);
} elseif ($type == TheliaEvents::CART_CREATE_NEW) {
$this->cartAction->createEmptyCart($event, null, $this->dispatcher);
}
}));
}
示例7: __construct
public function __construct($name, array $params = [])
{
$app = $this;
$locator = new FileLocator();
$this->routesFileLoader = new YamlFileLoader($locator);
$this['context'] = function () {
return new RequestContext();
};
$this['matcher'] = function () {
return new UrlMatcher($this['routes'], $this['context']);
};
$this['resolver'] = function () {
return new ControllerResolver($this);
};
$this['dispatcher'] = function () use($app) {
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new RouterListener($app['matcher']));
$dispatcher->addSubscriber(new ResponseListener($app['charset']));
return $dispatcher;
};
$this['kernel'] = function () use($app) {
return new HttpKernel($app['dispatcher'], $app['resolver']);
};
// $this['request.http_port'] = 80;
// $this['request.https_port'] = 443;
$this['charset'] = 'UTF-8';
parent::__construct($name, null, $params);
$this->setApp($this);
}
示例8: execute
/**
* {@inheritdoc}
*/
public function execute(EventDispatcher $dispatcher)
{
$this->ticksRemaining = $this->units;
$dispatcher->dispatch(Events::STEP_BEFORE_EXECUTE, new StepEvent($this));
call_user_func($this->executer, $this);
$dispatcher->dispatch(Events::STEP_AFTER_EXECUTE, new StepEvent($this));
}
示例9: testGlobalScore
public function testGlobalScore()
{
$dispatcher = new EventDispatcher();
$testers = array('Activity', 'Composer', 'Followers', 'KnpBundles', 'Readme', 'Travis');
foreach ($testers as $testerClass) {
$fqcn = sprintf('\\Knp\\Bundle\\KnpBundlesBundle\\EventListener\\Scoring\\%sListener', $testerClass);
$tester = new $fqcn();
$dispatcher->addListener(BundleEvent::UPDATE_SCORE, array($tester, 'onScoreUpdate'));
}
$bundle = new Bundle();
// activity (+4)
$bundle->setLastCommitAt(new \DateTime('-10days'));
// composer (+5)
$bundle->setComposerName('bundle-composer-name');
// followers (+10)
$bundle->setNbFollowers(10);
// recommendation (+5)
$bundle->addRecommender(new Developer());
// readme (+5)
$bundle->setReadme(str_repeat('-', 500));
// travis (+10)
$bundle->setUsesTravisCi(true);
$bundle->setTravisCiBuildStatus(true);
$dispatcher->dispatch(BundleEvent::UPDATE_SCORE, new BundleEvent($bundle));
$bundle->recalculateScore();
$this->assertEquals(39, $bundle->getScore());
}
示例10: shouldSortSimpleDoctrineQuery
/**
* @test
*/
function shouldSortSimpleDoctrineQuery()
{
$this->populate();
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new PaginationSubscriber());
$dispatcher->addSubscriber(new Sortable());
$p = new Paginator($dispatcher);
$_GET['sort'] = 'title';
$_GET['direction'] = 'asc';
$qb = $this->dm->createQueryBuilder('Test\\Fixture\\Document\\Article');
$query = $qb->getQuery();
$view = $p->paginate($query, 1, 10);
$items = array_values($view->getItems());
$this->assertEquals(4, count($items));
$this->assertEquals('autumn', $items[0]->getTitle());
$this->assertEquals('spring', $items[1]->getTitle());
$this->assertEquals('summer', $items[2]->getTitle());
$this->assertEquals('winter', $items[3]->getTitle());
$_GET['direction'] = 'desc';
$view = $p->paginate($query, 1, 10);
$items = array_values($view->getItems());
$this->assertEquals(4, count($items));
$this->assertEquals('winter', $items[0]->getTitle());
$this->assertEquals('summer', $items[1]->getTitle());
$this->assertEquals('spring', $items[2]->getTitle());
$this->assertEquals('autumn', $items[3]->getTitle());
}
示例11: buildContainer
/**
* Use this method to build the container with the services that you need.
* @param ContainerBuilder $container
*/
protected function buildContainer(ContainerBuilder $container)
{
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new Cache(new ArrayAdapter()));
$container->set("event_dispatcher", $eventDispatcher);
$container->setParameter("kernel.cache_dir", $this->cache_dir);
}
示例12: getDispatcher
protected function getDispatcher()
{
$dispatcher = new EventDispatcher();
$listener = new ResponseListener('UTF-8');
$dispatcher->connect('core.response', array($listener, 'filter'));
return $dispatcher;
}
示例13: testSwitchParameters
public function testSwitchParameters()
{
$input = array('where' => array('foo' => 'bar'));
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener(OperatorEvent::EVENT_NAME, function (OperatorEvent $event) {
if ($event->getField() === 'foo') {
$event->setField('pew-pew');
if ($event->getValue() === 'bar') {
$value = [1, 2, 3, 4];
} else {
$value = [5, 6, 7, 8];
}
$event->setValue($value);
}
});
$builder = new Builder($eventDispatcher);
$builder->build($input);
$this->assertCount(1, $builder->getFilters());
$filters = $builder->getFilters();
$this->assertEquals('and', $filters[0]->getGroup());
/* @var $expected ConditionInterface[] */
$expected = array(FilterConditionBuilder::create(FilterCondition::CONDITION_IN, 'pew-pew', [1, 2, 3, 4], $eventDispatcher));
foreach ($filters[0]->getConditions() as $key => $condition) {
$this->assertEquals($expected[$key]->getValue(), $condition->getValue());
$this->assertEquals($expected[$key]->getField(), $condition->getField());
$this->assertEquals($expected[$key]->getOperator(), $condition->getOperator());
}
}
示例14: __construct
public function __construct()
{
parent::__construct(self::NAME, self::VERSION);
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, array($this, 'onCommand'));
$this->setDispatcher($dispatcher);
$this->loadConfig();
$this->add(new Command\InitCommand());
$this->add(new Command\ClearStashCacheCommand());
$this->add(new Command\ClearCeCacheCommand());
$this->add(new Command\ClearEECacheCommand());
$this->add(new Command\GithubAddonInstallerCommand());
$this->add(new Command\ReplCommand());
$this->add(new Command\ShowConfigCommand());
$this->add(new Command\UpdateAddonsCommand());
$this->add(new Command\CreateChannelCommand());
$this->add(new Command\CreateTemplateCommand());
$this->add(new Command\CreateTemplateGroupCommand());
$this->add(new Command\CreateSnippetCommand());
$this->add(new Command\CreateGlobalVariableCommand());
$this->add(new Command\GenerateCommandCommand());
$this->add(new Command\GenerateAddonCommand());
$this->add(new Command\GenerateHtaccessCommand());
$this->add(new Command\DbDumpCommand());
$this->add(new Command\DeleteTemplateCommand());
$this->add(new Command\DeleteTemplateGroupCommand());
$this->add(new Command\DeleteSnippetCommand());
$this->add(new Command\DeleteGlobalVariableCommand());
$this->add(new Command\SyncTemplatesCommand());
$this->add(new Command\ShowTemplatesCommand());
}
示例15: createPrimaryMenu
/**
* Navbar Menu
*
* @param array $options
* @return ItemInterface
*/
public function createPrimaryMenu(array $options)
{
$menu = $this->factory->createItem('root');
$this->eventDispatcher->dispatch(WebsiteEvents::PRIMARY_MENU_INIT, new PrimaryMenuEvent($menu, $this->factory));
$this->reorderMenuItems($menu);
return $menu;
}