Exemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function attach($eventName, callable $listener, $priority = 1)
 {
     $options = [];
     $options['priority'] = $priority;
     $this->eventManager->on($eventName, $options, $listener);
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Create the template loader.
  *
  * @return \Twig_LoaderInterface
  */
 protected function getLoader()
 {
     $event = new Event('TwigView.TwigView.loader', $this, ['loader' => new Loader()]);
     $this->eventManager->dispatch($event);
     if (isset($event->result['loader'])) {
         return $event->result['loader'];
     }
     return $event->data['loader'];
 }
 /**
  * beforeRender event.
  *
  * @param Event $event Event.
  * @return void
  */
 public function beforeRender(Event $event)
 {
     $this->set('authUser', $this->authUser);
     $this->set('title', $this->name);
     $event = new Event('CakeAdmin.Controller.beforeRender', $this);
     EventManager::instance()->dispatch($event);
 }
Exemplo n.º 4
0
 /**
  * Similar to "trigger()" but this method expects that data is given as an
  * associative array instead of function arguments.
  *
  * ### Usage:
  *
  * ```php
  * EventDispatcher::instance()->triggerArray('myEvent', [$data1, $data2]);
  * ```
  *
  * Which is equivalent to:
  *
  * ```php
  * EventDispatcher::instance()->trigger('myEvent', $data1, $data2);
  * ```
  *
  * @param array|string $eventName The event name to trigger
  * @param array $data Information to be passed to event listener
  * @return \Cake\Event\Event The event object that was triggered
  */
 public function triggerArray($eventName, array $data = [])
 {
     $event = $this->_prepareEvent($eventName, $data);
     $this->_log($event->name());
     $this->_eventManager->dispatch($event);
     return $event;
 }
 /**
  * Unloads all registered listeners that were attached using the
  * "_attachListeners()" method.
  *
  * @return void
  */
 protected function _detachListeners()
 {
     $EventManager = EventManager::instance();
     foreach ($this->_listeners as $listener) {
         $EventManager->detach($listener);
     }
 }
 /**
  * startTest
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->ImageStorage = TableRegistry::get('Burzum/FileStorage.ImageStorage');
     $listener = new LocalListener();
     EventManager::instance()->on($listener);
 }
Exemplo n.º 7
0
 public function _before(TestCase $test)
 {
     if (method_exists($test, 'getTestClass')) {
         $this->testCase = $test->getTestClass();
     } else {
         $this->testCase = $test;
     }
     if (!isset($this->testCase->autoFixtures)) {
         $this->testCase->autoFixtures = $this->config['autoFixtures'];
     }
     if (!isset($this->testCase->dropTables)) {
         $this->testCase->dropTables = $this->config['dropTables'];
     }
     if (!isset($this->testCase->cleanUpInsertedRecords)) {
         $this->testCase->cleanUpInsertedRecords = $this->config['cleanUpInsertedRecords'];
     }
     EventManager::instance(new EventManager());
     $this->fixtureManager = new FixtureManager();
     if ($this->testCase->autoFixtures) {
         if (!isset($this->testCase->fixtures)) {
             $this->testCase->fixtures = [];
         }
         $this->loadFixtures($this->testCase->fixtures);
     }
     $this->snapshotApplication();
 }
Exemplo n.º 8
0
 /**
  * {@inheritDoc}
  */
 public function bake($name)
 {
     EventManager::instance()->on('Bake.initialize', function (Event $event) {
         $event->subject->loadHelper('Migrations.Migration');
     });
     return parent::bake($name);
 }
Exemplo n.º 9
0
 /**
  * @return \Cake\Network\Response|void
  * @throws \League\OAuth2\Server\Exception\InvalidGrantException
  */
 public function authorize()
 {
     if (!($authParams = $this->OAuth->checkAuthParams('authorization_code'))) {
         return;
     }
     if (!$this->Auth->user()) {
         $query = $this->request->query;
         $query['redir'] = 'oauth';
         return $this->redirect(['plugin' => false, 'controller' => 'Users', 'action' => 'login', '?' => $query]);
     }
     $event = new Event('OAuthServer.beforeAuthorize', $this);
     EventManager::instance()->dispatch($event);
     if (is_array($event->result)) {
         $this->set($event->result);
     }
     if ($this->request->is('post') && $this->request->data['authorization'] === 'Approve') {
         $ownerModel = isset($this->request->data['owner_model']) ? $this->request->data['owner_model'] : 'Users';
         $ownerId = isset($this->request->data['owner_id']) ? $this->request->data['owner_id'] : $this->Auth->user('id');
         $redirectUri = $this->OAuth->Server->getGrantType('authorization_code')->newAuthorizeRequest($ownerModel, $ownerId, $authParams);
         $event = new Event('OAuthServer.afterAuthorize', $this);
         EventManager::instance()->dispatch($event);
         return $this->redirect($redirectUri);
     } elseif ($this->request->is('post')) {
         $event = new Event('OAuthServer.afterDeny', $this);
         EventManager::instance()->dispatch($event);
         $error = new AccessDeniedException();
         $redirectUri = new RedirectUri($authParams['redirect_uri'], ['error' => $error->errorType, 'message' => $error->getMessage()]);
         return $this->redirect($redirectUri);
     }
     $this->set('authParams', $authParams);
     $this->set('user', $this->Auth->user());
 }
 /**
  * testProcessVersion
  *
  * @return void
  */
 public function testProcessVersion()
 {
     $entity = $this->Image->newEntity(['foreign_key' => 'test-1', 'model' => 'Test', 'file' => ['name' => 'titus.jpg', 'size' => 332643, 'tmp_name' => Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg', 'error' => 0]], ['accessibleFields' => ['*' => true]]);
     $this->Image->save($entity);
     $result = $this->Image->find()->where(['id' => $entity->id])->first();
     $this->assertTrue(!empty($result) && is_a($result, '\\Cake\\ORM\\Entity'));
     $this->assertTrue(file_exists($this->testPath . $result['path']));
     $path = $this->testPath . $result['path'];
     $Folder = new Folder($path);
     $folderResult = $Folder->read();
     $this->assertEquals(count($folderResult[1]), 3);
     Configure::write('FileStorage.imageSizes.Test', array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))));
     StorageUtils::generateHashes();
     $Event = new Event('ImageVersion.createVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))));
     EventManager::instance()->dispatch($Event);
     $path = $this->testPath . $result['path'];
     $Folder = new Folder($path);
     $folderResult = $Folder->read();
     $this->assertEquals(count($folderResult[1]), 4);
     $Event = new Event('ImageVersion.removeVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))));
     EventManager::instance()->dispatch($Event);
     $path = $this->testPath . $result['path'];
     $Folder = new Folder($path);
     $folderResult = $Folder->read();
     $this->assertEquals(count($folderResult[1]), 3);
 }
Exemplo n.º 11
0
 /**
  * {@inheritDoc}
  */
 public function setUp()
 {
     $eventManagerInstance = EventManager::instance();
     parent::setUp();
     EventManager::instance($eventManagerInstance);
     $this->session(mockUserSession());
     include QUICKAPPS_CORE . '/config/routes_site.php';
 }
Exemplo n.º 12
0
 /**
  * Constructor
  *
  * @param Request|null       $request
  * @param Response|null      $response
  * @param EventManager|null  $eventManager
  * @param array              $viewOptions
  */
 public function __construct(Request $request = null, Response $response = null, EventManager $eventManager = null, array $viewOptions = [])
 {
     if ($eventManager === null) {
         $eventManager = EventManager::instance();
     }
     $this->_templatePath = Configure::read('App.paths.templates')[0];
     parent::__construct($request, $response, $eventManager, $viewOptions);
 }
 /**
  * Attaches to Event topics and attaches them to the corresponding event handlers
  *
  * @return void
  */
 public function handleEvents()
 {
     if (defined('PHPUNIT_TESTSUITE') == 1) {
         return;
     }
     $manager = EventManager::instance();
     $manager->attach([$this, 'onForgotPassword'], 'Users.forgot_password');
 }
Exemplo n.º 14
0
 /**
  * {@inheritDoc}
  */
 public function bake($name)
 {
     $collection = $this->getCollection($this->connection);
     EventManager::instance()->on('Bake.initialize', function (Event $event) use($collection) {
         $event->subject->loadHelper('Migrations.Migration', ['collection' => $collection]);
     });
     return parent::bake($name);
 }
Exemplo n.º 15
0
 /**
  * @param WebsocketStartEvent $event
  */
 public function start(WebsocketStartEvent $event)
 {
     $this->pcntl = new PCNTL($event->getLoop());
     foreach ($this->signals as $signal) {
         $this->pcntl->on($signal, function ($signal) use($event) {
             EventManager::instance()->dispatch(SignalEvent::create($event->getLoop(), $signal));
         });
     }
 }
Exemplo n.º 16
0
 /**
  * Get available link types via an Event trigger
  * This fetches avilable Links from all activated Plugins.
  *
  * @return array
  */
 public function getLinkTypes()
 {
     $event = new Event('Wasabi.Backend.MenuItems.getLinkTypes', $this);
     EventManager::instance()->dispatch($event);
     $typeExternal = ['type' => 'external'];
     $typeCustom = ['type' => 'custom'];
     $event->result[__d('wasabi_core', 'General')] = [json_encode($typeExternal) => __d('wasabi_core', 'External Link'), json_encode($typeCustom) => __d('wasabi_core', 'Custom Controller Action')];
     return $event->result;
 }
 /**
  * [setUp description]
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->resetReflectionCache();
     $this->_eventManager = EventManager::instance();
     $existing = Configure::read('App.paths.templates');
     $existing[] = Plugin::path('Crud') . 'tests/App/Template/';
     Configure::write('App.paths.templates', $existing);
 }
Exemplo n.º 18
0
 /**
  * Costum Auth finder method
  * 
  * @param array $config Table Configuration.
  *
  * @return void
  */
 public function findAuth(\Cake\ORM\Query $query, array $options)
 {
     $event = new Event('Lil.authFinder', $this, [$query, $options]);
     EventManager::instance()->dispatch($event);
     if (!empty($event->result)) {
         $query = $event->result;
     }
     return $query;
 }
 /**
  * Resolves a ResolvableAuthorizationInterface
  *
  * @param  ResolvableAuthorizationInterface|string $resolvable Resolvable to resolve
  * @param  array $params Params for resolution
  * @return null
  */
 public function resolve($resolvable, $params = [])
 {
     if (ResolvableAuthorizationRegistry::isRegistered($resolvable)) {
         $resolvable = ResolvableAuthorizationRegistry::get($resolvable);
     }
     $resolvable->resolve($params);
     $resolvable->afterResolve();
     EventManager::instance()->dispatch(new Event('AuthorizationInternalEvent.AuthorizationResolved', $this, [$resolvable, $params]));
     return $resolvable->value();
 }
 /**
  * @param ConstructEvent $event
  */
 public function construct(ConstructEvent $event)
 {
     $client = new Client();
     $client->setLoop($event->getLoop());
     $bot = new Bot();
     $bot->setConfig(Configure::read('WyriHaximus.Phergie.config'));
     $bot->setClient($client);
     $bot->run(false);
     EventManager::instance()->dispatch(StartEvent::create($event->getLoop(), $bot));
 }
Exemplo n.º 21
0
 /**
  * tests trying to assert data key=>value when an event is fired multiple times
  *
  * @return void
  * @expectedException \PHPUnit_Framework_AssertionFailedError
  */
 public function testMatchesInvalid()
 {
     $manager = EventManager::instance();
     $manager->setEventList(new EventList());
     $manager->trackEvents(true);
     $myEvent = new Event('my.event', $this, ['key' => 'value']);
     $manager->getEventList()->add($myEvent);
     $manager->getEventList()->add($myEvent);
     $constraint = new EventFiredWith($manager, 'key', 'value');
     $constraint->matches('my.event');
 }
Exemplo n.º 22
0
 /**
  * @param ConstructEvent $event
  */
 public function construct(ConstructEvent $event)
 {
     $router = new Router($event->getLoop());
     foreach (Configure::read('WyriHaximus.Ratchet.realms') as $realm => $config) {
         $router->addInternalClient(new InternalClient($realm, $event->getLoop()));
     }
     $router->addTransportProvider(new RatchetTransportProvider(Configure::read('WyriHaximus.Ratchet.internal.address'), Configure::read('WyriHaximus.Ratchet.internal.port')));
     //$router->getRealmManager()->setDefaultAuthorizationManager(new AllPermissiveAuthorizationManager());
     EventManager::instance()->dispatch(WebsocketStartEvent::create($event->getLoop()));
     $router->start(false);
 }
Exemplo n.º 23
0
 /**
  * Remove an object from the registry.
  *
  * If this registry has an event manager, the object will be detached from any events as well.
  *
  * @param string $objectName The name of the object to remove from the registry.
  * @return void
  */
 public function unload($objectName)
 {
     if (empty($this->_loaded[$objectName])) {
         return;
     }
     $object = $this->_loaded[$objectName];
     if (isset($this->_eventManager)) {
         $this->_eventManager->detach($object);
     }
     unset($this->_loaded[$objectName]);
 }
Exemplo n.º 24
0
 public function start()
 {
     $this->loop = \WyriHaximus\Ratchet\loopResolver();
     $router = new Router($this->loop);
     $router->addInternalClient(new InternalClient('first', $this->loop));
     $router->addTransportProvider(new RatchetTransportProvider(Configure::read('WyriHaximus.Ratchet.Connection.Websocket.address'), Configure::read('WyriHaximus.Ratchet.Connection.Websocket.port')));
     //$router->getRealmManager()->setDefaultAuthorizationManager(new AllPermissiveAuthorizationManager());
     EventManager::instance()->dispatch(WebsocketStartEvent::create($this->loop));
     $router->start(false);
     $this->loop->run();
 }
Exemplo n.º 25
0
 /**
  * Create the behavior instance.
  *
  * Part of the template method for Cake\Utility\ObjectRegistry::load()
  * Enabled behaviors will be registered with the event manager.
  *
  * @param string $class The classname that is missing.
  * @param string $alias The alias of the object.
  * @param array $config An array of config to use for the behavior.
  * @return Behavior The constructed behavior class.
  */
 protected function _create($class, $alias, $config)
 {
     $instance = new $class($this->_table, $config);
     $enable = isset($config['enabled']) ? $config['enabled'] : true;
     if ($enable) {
         $this->_eventManager->attach($instance);
     }
     $methods = $this->_getMethods($instance, $class, $alias);
     $this->_methodMap += $methods['methods'];
     $this->_finderMap += $methods['finders'];
     return $instance;
 }
Exemplo n.º 26
0
 /**
  * Get view instance
  *
  * @return \Cake\View\View
  * @triggers Bake.initialize $view
  */
 public function getView()
 {
     if ($this->View) {
         return $this->View;
     }
     $theme = isset($this->params['theme']) ? $this->params['theme'] : '';
     $viewOptions = ['helpers' => ['Bake.Bake', 'Bake.DocBlock'], 'theme' => $theme];
     $view = new BakeView(new Request(), new Response(), null, $viewOptions);
     $event = new Event('Bake.initialize', $view);
     EventManager::instance()->dispatch($event);
     $this->View = $event->subject;
     return $this->View;
 }
Exemplo n.º 27
0
 /**
  * [setUp description]
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->resetReflectionCache();
     $this->_eventManager = EventManager::instance();
     $existing = Configure::read('App.paths.templates');
     $existing[] = Plugin::path('Crud') . 'tests/App/Template/';
     Configure::write('App.paths.templates', $existing);
     Configure::write('App.namespace', 'Crud\\Test\\App');
     Router::extensions('json');
     Router::connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
     Router::connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);
     $this->useHttpServer(false);
 }
Exemplo n.º 28
0
 /**
  * tests EventFired constraint
  *
  * @return void
  */
 public function testMatches()
 {
     $manager = EventManager::instance();
     $manager->setEventList(new EventList());
     $manager->trackEvents(true);
     $myEvent = new Event('my.event', $this, []);
     $myOtherEvent = new Event('my.other.event', $this, []);
     $manager->getEventList()->add($myEvent);
     $manager->getEventList()->add($myOtherEvent);
     $constraint = new EventFired($manager);
     $this->assertTrue($constraint->matches('my.event'));
     $this->assertTrue($constraint->matches('my.other.event'));
     $this->assertFalse($constraint->matches('event.not.fired'));
 }
 /**
  * Initializer, must be called in beforeFilter()
  *
  * @return void
  */
 public function initAuthActions()
 {
     EventManager::instance()->attach(function (\Cake\Event\Event $event) {
         // Make the AuthComponent, AuthActions and UserRights available to the view.
         // FIXME - find a clean way to do this
         if (!$event->subject() instanceof \Cake\Controller\ErrorController) {
             $viewAuthActions = ['AuthActions' => $event->subject()->getAuthActions(), 'UserRights' => $event->subject()->getUserRights()];
             $event->subject()->set('viewAuthActions', $viewAuthActions);
         }
     }, 'Controller.beforeRender');
     if ($this->getAuthActions()->isPublicAction($this->request->params['plugin'], $this->request->params['controller'], $this->request->params['action'])) {
         $this->Auth->allow();
     }
 }
Exemplo n.º 30
0
 public function setUp()
 {
     parent::setUp();
     $this->Request = $this->getMockBuilder('Cake\\Network\\Request')->getMock();
     $this->Controller = $this->getMockBuilder('Cake\\Controller\\Controller')->setMethods(['set'])->setConstructorArgs([$this->Request, new \Cake\Network\Response(), 'CrudExamples', \Cake\Event\EventManager::instance()])->getMock();
     $this->Registry = $this->Controller->components();
     $this->Crud = $this->getMockBuilder('Crud\\Controller\\Component\\CrudComponent')->setConstructorArgs([$this->Registry])->setMethods(['foobar'])->getMock();
     $this->Controller->Crud = $this->Crud;
     $this->Controller->modelClass = 'CrudExamples';
     $this->Controller->CrudExamples = \Cake\ORM\TableRegistry::get('Crud.CrudExamples');
     $this->Controller->CrudExamples->alias('MyModel');
     $this->actionClassName = $this->getMockClass('Crud\\Action\\BaseAction', ['_handle']);
     $this->ActionClass = new $this->actionClassName($this->Controller);
     $this->_configureAction($this->ActionClass);
 }