/** * Attach scripts and links to the view helpers based on the configuration file. * @todo Handle priority * @todo Add extended specification for scripts and links */ protected function attachConfigEvents() { $config = $this->serviceLocator->get('Configuration'); if (isset($config['dots']['view']['events']) && is_array($config['dots']['view']['events'])) { foreach ($config['dots']['view']['events'] as $name => $options) { if (!empty($options)) { StaticEventManager::getInstance()->attach('dots', $name, function (Event $event) use($options) { $view = $event->getTarget(); if (isset($options['scripts']) && is_array($options['scripts'])) { $scripts = array_filter($options['scripts'], function ($script) { return $script != null; }); foreach ($scripts as $script) { $view->plugin('headScript')->appendFile($script); } } if (isset($options['links']) && is_array($options['links'])) { $links = array_filter($options['links'], function ($link) { return $link != null; }); foreach ($links as $script) { $view->plugin('headLink')->appendStylesheet($script); } } }, 200); } } } }
public function __construct() { $event = $this->getEvent(); $this->viewModel = $event->getViewModel(); $sharedEvents = StaticEventManager::getInstance(); $sharedEvents->attach('Zend\\Mvc\\Controller\\AbstractActionController', MvcEvent::EVENT_DISPATCH, function (Event $event) { // get view model for layout $view = $event->getViewModel(); // assign locales information $sm = $this->getServiceLocator(); $config = $sm->get('Configuration'); $view->setVariable('locale', $sm->get('translator')->getLocale()); $view->setVariable('locales', $config['locales']['list']); // assign flashmessanger messages $messages = $this->flashmessenger()->getSuccessMessages(); $view->setVariable('messages', $messages); $info = $this->flashmessenger()->getInfoMessages(); $view->setVariable('info', $info); $warnings = $this->flashmessenger()->getMessages(); $view->setVariable('warnings', $warnings); $errors = $this->flashmessenger()->getErrorMessages(); $view->setVariable('errors', $errors); // assign variables to action view $this->viewModel->setVariables($view->getVariables()); }); }
/** * Initialize the module * * @param ModuleManager $m Module manager * * @return void */ public function init(ModuleManager $m) { if (!Console::isConsole()) { $em = StaticEventManager::getInstance(); $em->attach('Zend\\Mvc\\Application', 'bootstrap', [$this, 'registerBaseUrl'], 100000); } }
public function init() { // Attach Event to EventManager $events = StaticEventManager::getInstance(); // Add event of authentication before dispatch $events->attach('Zend\\View\\View', ViewEvent::EVENT_RENDERER_POST, array($this, 'addPiwikCode'), 110); }
public static function setEventCollection(EventManagerInterface $events = null) { parent::setEventCollection($events); $sharedManager = StaticEventManager::getInstance(); static::$events->setSharedManager($sharedManager); static::$events->setIdentifiers(array(__CLASS__, get_called_class(), 'dots')); }
public function init(Manager $moduleManager) { $events = StaticEventManager::getInstance(); # post configuration merge $events->attach('Zend\\Module\\Manager', 'loadModules.post', array($this, 'postModulesLoadsListener'), 8); # pre bootstrap $events->attach('bootstrap', 'bootstrap', array($this, 'preBootstrapListener'), 20); }
public function setUp() { StaticEventManager::resetInstance(); if (isset($this->message)) { unset($this->message); } $this->events = new EventManager(); }
private function _afterLogin($e) { $em = \Zend\EventManager\StaticEventManager::getInstance(); $em->attach('ZfcUser\\Authentication\\Adapter\\AdapterChain', 'authenticate.success', function ($e) { $userId = $e->getIdentity(); $this->_logService->log(LogService::NOTICE, "Connexion de l'utilisateur: {$userId}", LogService::USER); $this->_userTable->updateLastConnection($userId); }); }
public function setUp() { StaticEventManager::resetInstance(); $this->controller = new TestAsset\SampleController(); $this->request = new Request(); $this->response = null; $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample')); $this->event = new MvcEvent(); $this->event->setRouteMatch($this->routeMatch); $this->controller->setEvent($this->event); }
public function setUp() { StaticEventManager::resetInstance(); $this->controller = new IndexController(); $this->request = new Request(); $this->response = null; $this->routeMatch = new RouteMatch(array('controller' => 'php-unit-controller-index')); $this->event = new MvcEvent(); $this->event->setRouteMatch($this->routeMatch); $this->controller->setEvent($this->event); }
public function initializeView(Event $e) { $app = $e->getParam('application'); $locator = $app->getLocator(); $config = $e->getParam('modules')->getMergedConfig(); $view = $this->getView($app, $config); $viewListener = $this->getViewListener($view, $config); $app->events()->attachAggregate($viewListener); $events = StaticEventManager::getInstance(); $viewListener->registerStaticListeners($events, $locator); }
public function onBootstrap($e) { $app = $e->getTarget(); $this->sm = $app->getServiceManager(); StaticEventManager::getInstance()->attach('ZF\\Rest\\RestController', 'getList.post', function ($e) { $halCollection = $e->getParam('collection'); $parameters = $e->getTarget()->queryParams(); unset($parameters['page']); $halCollection->setCollectionRouteOptions(array('query' => $parameters)); }); }
public function onBootstrap(MvcEvent $e) { $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); $em = $e->getApplication()->getServiceManager()->get('Doctrine\\ORM\\EntityManager'); $platform = $em->getConnection()->getDatabasePlatform(); $platform->registerDoctrineTypeMapping('enum', 'string'); $this->initializeSessions($e); $sharedEventManager = StaticEventManager::getInstance(); $sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, array($this, 'onLoad'), 200); }
public function _after(\Codeception\TestCase $test) { $_SESSION = array(); $_GET = array(); $_POST = array(); $_COOKIE = array(); // reset singleton StaticEventManager::resetInstance(); Placeholder\Registry::unsetRegistry(); $this->queries = 0; $this->time = 0; }
public function defineEventHandlers() { $events = StaticEventManager::getInstance(); $view = $this->di->get('view'); $events->attach('Zf2\\Mvc\\FrontController', 'dispatch.post', function ($e) use($view) { $response = $e->getParam('response'); if ($response->getHeaders()->getStatusCode() != 302) { $content = $view->render($e->getParam('request'), $e->getParam('__RESULT__')); $response->setContent($content); } return $response; }); }
public function onBootstrap(MvcEvent $event) { $serviceManager = $event->getApplication()->getServiceManager(); $events = StaticEventManager::getInstance(); $events->attach('dots', 'blocks.admin.menu', function () use($serviceManager) { $view = $serviceManager->get('DotsTwigViewRenderer'); //render admin navigation $viewModel = new ViewModel(); $viewModel->setTemplate('dots-slideshow/admin/menu'); $viewModel->setTerminal(true); return $view->render($viewModel); }); }
public function onBootstrap($e) { $em = StaticEventManager::getInstance(); $em->attach('*', '*', function ($e) { $event = $e->getName(); $target = get_class($e->getTarget()); $params = $e->getParams(); $output = sprintf('Event "%s" was triggered on target "%s", with parameters %s', $event, $target, json_encode($params)); error_log($output); // Return true so this listener doesn't break the validator // chain triggering session.validate listeners return true; }); }
/** * Attach default events listeners for the dispatcher */ protected function attachDefaultListeners() { $events = StaticEventManager::getInstance(); foreach ($this->handlers as $class => $handlers) { foreach ($handlers as $options) { $event = explode(':', $options['event']); if (array_key_exists('priority', $options)) { $events->attach($event[0], $event[1], array($class, $options['handler']), $options['priority']); } else { $events->attach($event[0], $event[1], array($class, $options['handler'])); } } } }
public function testAuthenticate() { $authService = $this->prophesize(AuthenticationService::class); $adapter = $this->prophesize(AdapterInterface::class); $result = $this->prophesize(Result::class); $authenticator = new Authenticator($authService->reveal(), $adapter->reveal()); $eventManager = StaticEventManager::getInstance(); // event will be raised $eventManager->attach(Authenticator::class, AuthenticationEvent::EVENT_AUTH, function ($e) use($result) { $this->assertInstanceOf(AuthenticationEvent::class, $e); $e->setResult($result->reveal()); }); $returnedResult = $authenticator->authenticate(); $this->assertSame($result->reveal(), $returnedResult); }
public function _after(\Codeception\TestCase $test) { $_SESSION = array(); $_GET = array(); $_POST = array(); $_COOKIE = array(); // reset singleton StaticEventManager::resetInstance(); // Reset singleton placeholder if version < 2.2.0, no longer required in 2.2.0+ if (Version::compareVersion('2.2.0') >= 0) { Placeholder\Registry::unsetRegistry(); } $this->queries = 0; $this->time = 0; }
public function onBootstrap(MvcEvent $e) { $events = StaticEventManager::getInstance(); $serviceManager = $e->getApplication()->getServiceManager(); $appConfig = $serviceManager->get('Config'); $logger = new Logger(); if (!isset($appConfig['operation_logger'])) { throw new \RuntimeException("Logger not properly configured"); } if (!isset($appConfig['operation_logger']['priority_filter'])) { throw new \RuntimeException("You must specify a 'priority_filter' config param"); } $logFilter = new PriorityFilter($appConfig['operation_logger']['priority_filter']); if (!is_null($appConfig['operation_logger']['db_adapter'])) { if (empty($appConfig['operation_logger']['logger_table'])) { throw new \RuntimeException("You must specify a 'logger_table' config param"); } $dbAdapter = $serviceManager->get($appConfig['operation_logger']['db_adapter']); if (!$dbAdapter instanceof \Zend\Db\Adapter\Adapter) { throw new \RuntimeException("Failed to load database adapter for logger"); } $tableMapping = array('timestamp' => 'event_date', 'priorityName' => 'priority', 'message' => 'event', 'extra' => array('id_operation_log' => 'id_operation_log', 'note' => 'note', 'table' => 'table_name', 'id_user' => 'id_user', 'username' => 'username', 'id_row' => 'id_row', 'field' => 'field', 'value_old' => 'value_old', 'value_new' => 'value_new', 'source' => 'source', 'uri' => 'uri', 'ip' => 'ip', 'session_id' => 'session_id')); $logWriter = new DbWriter($dbAdapter, $appConfig['operation_logger']['logger_table'], $tableMapping); $logWriter->addFilter($logFilter); $logger->addWriter($logWriter); } // nel caso si volgia fare un file LOG if (isset($appConfig['operation_logger']['log_file']) && !is_null($appConfig['operation_logger']['log_file'])) { $streamWriter = new StreamWriter($appConfig['operation_logger']['log_file']); $streamWriter->addFilter($logFilter); $logger->addWriter($streamWriter); } $request = $e->getApplication()->getRequest(); $remoteAddress = new RemoteAddress(); Logger::registerErrorHandler($logger, true); Logger::registerExceptionHandler($logger); // Attacco evento per trigger LOG! (evento: operation-log) $events->attach("*", 'operation-log', function (\Zend\EventManager\Event $e) use($logger, $request, $remoteAddress, $serviceManager) { $targetClass = get_class($e->getTarget()); $message = $e->getParam('message'); $priority = $e->getParam('priority', Logger::INFO); $zfcAuthEvents = $serviceManager->get('zfcuser_auth_service'); $idUser = $zfcAuthEvents->hasIdentity() ? $zfcAuthEvents->getIdentity()->getId() : (array_key_exists('id_user', $message) ? $message['id_user'] : '******'); $displayName = $zfcAuthEvents->hasIdentity() ? $zfcAuthEvents->getIdentity()->getDisplayName() : (array_key_exists('username', $message) ? $message['username'] : '******'); $extras = array('id_operation_log' => null, 'note' => array_key_exists('note', $message) ? $message['note'] : null, 'table_name' => array_key_exists('table', $message) ? $message['table'] : null, 'operation' => array_key_exists('operation', $message) ? $message['operation'] : null, 'id_user' => $idUser, 'username' => $displayName, 'id_row' => array_key_exists('id_row', $message) ? $message['id_row'] : null, 'field' => array_key_exists('field', $message) ? $message['field'] : null, 'value_old' => array_key_exists('value_old', $message) ? $message['value_old'] : null, 'value_new' => array_key_exists('value_new', $message) ? $message['value_new'] : null, 'source' => $targetClass, 'uri' => $request->getUriString(), 'ip' => $remoteAddress->getIpAddress(), 'session_id' => session_id()); $logger->log($priority, $message['message'], $extras); }); }
public function onBootstrap(MvcEvent $event) { $app = $event->getApplication(); $serviceManager = $app->getServiceManager(); $blockManager = $serviceManager->get('DotsBlockManager'); Registry::set('block_manager', $blockManager); $events = StaticEventManager::getInstance(); $events->attach('dots', 'admin.menu', function () use($serviceManager) { $view = $serviceManager->get('DotsTwigViewRenderer'); //render admin navigation $viewModel = new ViewModel(); $viewModel->setTemplate('dots-block/admin/nav'); $viewModel->setTerminal(true); return $view->render($viewModel); }, 100); }
public function onBootstrap($e) { $app = $e->getApplication(); $app->getServiceManager()->get('translator'); $eventManager = $app->getEventManager(); $serviceManager = $app->getServiceManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); /** * Assign 'user' role when registering */ $em = \Zend\EventManager\StaticEventManager::getInstance(); $em->attach('ZfcUser\\Service\\User', 'register.post', function ($e) use($serviceManager) { $user_id = $e->getParam('user')->getId(); $linker = $serviceManager->get('UserRolerLinkerTable'); $linker->saveLink(array('user_id' => $user_id, 'role_id' => 'user')); }); }
/** * loadModulesPost * * Once all the modules are loaded, loop * * @param Event $e * @return void */ public function loadModulesPost(Event $e) { $events = StaticEventManager::getInstance(); $moduleManager = $e->getTarget(); // Shared instance for module manager $events->attach('bootstrap', 'bootstrap', function ($e) use($moduleManager) { $moduleClassName = get_class($moduleManager); $im = $e->getParam('application')->getLocator()->instanceManager(); if (!$im->hasSharedInstance($moduleClassName)) { $im->addSharedInstance($moduleManager, $moduleClassName); } }, 1000); if (0 === count($this->modules)) { return; } // Attach to the bootstrap event if there are modules we need to process $events = StaticEventManager::getInstance(); $events->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'), 1000); }
/** * @param EventInterface|\Zend\Mvc\MvcEvent $e * @return array|void */ public function onBootstrap(EventInterface $e) { $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); /** * attach an event to setDbConfigsFromEvent. add this code to other module if you need to add some dynamic db config. */ $events = StaticEventManager::getInstance(); $events->attach("YcheukfMigration\\Controller\\MigrateController", array('setDbConfigsFromEvent'), function ($e) { // var_dump($e->getParams()); $aReturn = array(); $aReturn['db'] = array('driver' => 'Pdo', 'dsn' => 'mysql:dbname=zf2test2;host=localhost', 'username' => 'root', 'password' => 'root', 'driver_options' => array('SET NAMES \'UTF8\'')); $aReturn['db2'] = array('driver' => 'Pdo', 'dsn' => 'mysql:dbname=zf2test3;host=localhost', 'username' => 'root', 'password' => 'root', 'driver_options' => array('SET NAMES \'UTF8\'')); return $aReturn; }); // */ }
public function _after(\Codeception\TestCase $test) { $_SESSION = []; $_GET = []; $_POST = []; $_COOKIE = []; // reset singleton StaticEventManager::resetInstance(); // Reset singleton placeholder if version < 2.2.0, no longer required in 2.2.0+ if (Version::compareVersion('2.2.0') >= 0) { Placeholder\Registry::unsetRegistry(); } //Close the session, if any are open if (session_status() == PHP_SESSION_ACTIVE) { session_write_close(); } $this->queries = 0; $this->time = 0; }
public function onBootstrap(MvcEvent $e) { $events = StaticEventManager::getInstance(); $serviceManager = $e->getApplication()->getServiceManager(); $appConfig = $serviceManager->get('Config'); $logger = new Logger(); if (!isset($appConfig['logger'])) { throw new \RuntimeException("Logger not properly configured"); } if (!isset($appConfig['logger']['priority_filter'])) { throw new \RuntimeException("You must specify a 'priority_filter' config param"); } $logFilter = new PriorityFilter($appConfig['logger']['priority_filter']); if (!is_null($appConfig['logger']['db_adapter'])) { if (empty($appConfig['logger']['logger_table'])) { throw new \RuntimeException("You must specify a 'logger_table' config param"); } $dbAdapter = $serviceManager->get($appConfig['logger']['db_adapter']); if (!$dbAdapter instanceof \Zend\Db\Adapter\Adapter) { throw new \RuntimeException("Failed to load database adapter for logger"); } $tableMapping = array('timestamp' => 'event_date', 'priorityName' => 'priority', 'message' => 'event', 'extra' => array('source' => 'source', 'uri' => 'uri', 'ip' => 'ip', 'session_id' => 'session_id')); $logWriter = new DbWriter($dbAdapter, $appConfig['logger']['logger_table'], $tableMapping); $logWriter->addFilter($logFilter); $logger->addWriter($logWriter); } if (isset($appConfig['logger']['log_file']) && !is_null($appConfig['logger']['log_file'])) { $streamWriter = new StreamWriter($appConfig['logger']['log_file']); $streamWriter->addFilter($logFilter); $logger->addWriter($streamWriter); } $request = $e->getApplication()->getRequest(); $remoteAddress = new RemoteAddress(); Logger::registerErrorHandler($logger, true); Logger::registerExceptionHandler($logger); $events->attach("*", 'log', function (\Zend\EventManager\Event $e) use($logger, $request, $remoteAddress) { $targetClass = get_class($e->getTarget()); $message = $e->getParam('message', "[No Message Provided]"); $priority = $e->getParam('priority', Logger::INFO); $extras = array('source' => $targetClass, 'uri' => $request->getUriString(), 'ip' => $remoteAddress->getIpAddress(), 'session_id' => session_id()); $logger->log($priority, $message, $extras); }); }
public function bootstrap() { $events = StaticEventManager::getInstance(); $acl = $this->getAclService(); $defMapper = $this->getEventGuardDefMapper(); $defs = $defMapper->findByRoleId($acl->getRole()->getRoleId()); foreach ($defs as $def) { $events->attach($def->getEventId(), $def->getEvent(), function ($e) use($acl, $def) { if ($def instanceof EventGuardDefTriggeredEventAware) { $def->setTriggeredEvent($e); } $resource = $def->getResource(); $privilege = $def->getPrivilege(); if (!$acl->isAllowed($resource, $privilege)) { $roleId = $acl->getRole()->getRoleId(); if ($resource instanceof Resource) { $resource = $resource->getResourceId(); } throw new UnauthorizedException("You ({$roleId}) are not allowed to perform '{$privilege}' on '{$resource}'"); } }, 1000); } }
protected function setUp() { StaticEventManager::resetInstance(); $this->serviceManager = new ServiceManager(); $this->request = new Request(); $this->routeMatch = new RouteMatch(array('controller' => 'HtUserRegistration')); $this->event = new MvcEvent(); $this->event->setRouteMatch($this->routeMatch); $this->zfcUserMapper = $this->getMock('ZfcUser\\Mapper\\User'); $this->zfcUserEntity = $this->getMock('ZfcUser\\Entity\\UserInterface'); $this->userRegistrationService = $this->getMock('HtUserRegistration\\Service\\UserRegistrationServiceInterface'); $this->userRegistrationMapper = $this->getMock('HtUserRegistration\\Mapper\\UserRegistrationMapperInterface'); $this->userRegistrationEntity = $this->getMock('HtUserRegistration\\Entity\\UserRegistrationInterface'); $this->moduleOptions = $this->getMock('HtUserRegistration\\Options\\ModuleOptions'); $this->moduleOptions->expects($this->any())->method('getPostVerificationRoute')->willReturn($this->redirectRoute); $routeStack = new SimpleRouteStack(); $route = new Segment($this->redirectRoute); $routeStack->addRoute($this->redirectRoute, $route); $this->event->setRouter($routeStack); $this->serviceManager->setService('HtUserRegistration\\ModuleOptions', $this->moduleOptions); $this->controller = new UserRegistrationController($this->userRegistrationService); $this->controller->setServiceLocator($this->serviceManager); $this->controller->setEvent($this->event); }
public function __construct() { $manager = StaticEventManager::getInstance(); $manager->attach('Zend\\Mvc\\Application', MvcEvent::EVENT_BOOTSTRAP, array($this, 'registerAspects'), PHP_INT_MAX); }