public function afterRenderView(Event $event, View $view) { if ('afterRenderView' == $event->getType()) { $this->levels[] = $view->getCurrentRenderLevel(); } return true; }
/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $di = $this->getDI(); $cookie = $di->getCookie(); $session = $di->getSession(); $config = $di->getConfig(); $languageCode = ''; if ($di->get('app')->isConsole()) { return; } // Detect language from cookie if ($cookie->has('languageCode')) { $languageCode = $cookie->get('languageCode')->getValue(); } else { // Get default language from language model $languageCode = LanguageModel::findFirst(['default = :isdefault: AND status = :enable:', 'bind' => ['isdefault' => LanguageModel::IS_DEFAULT, 'enable' => LanguageModel::STATUS_ENABLE]])->code; } // Set language code to session if ($session->has('languageCode') && $session->get('languageCode') != $languageCode || !$session->has('languageCode')) { $session->set('languageCode', $languageCode); } $messages = []; $directory = $di->get('registry')->directories->modules . ucfirst($dispatcher->getModuleName()) . '/Lang/' . $languageCode . '/' . strtolower($dispatcher->getControllerName()); $extension = '.php'; if (file_exists($directory . $extension)) { require $directory . $extension; } // add default core lang package require $di->get('registry')->directories->modules . self::DEFAULT_LANG_PACK . '/Lang/' . $languageCode . '/default.php'; $translate = new PhTranslateArray(['content' => array_merge($messages, $default)]); $di->set('lang', $translate); return !$event->isStopped(); }
public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { $view = $dispatcher->getDI()->getService('view')->resolve(); $controllerData = $event->getData(); if (is_array($controllerData)) { foreach ($controllerData as $key => $value) { $view->setVar($key, $value); } } }
protected function acceptable(Event $evt, App $app) { //Can we generate this content type? if (($this->contentType = self::match($this->getHeader('Accept'), array_keys($this->acceptable))) === null) { $evt->stop(); $msg = "Can't generate a '.{$this->contentType}.' response"; $this->contentType = static::DEF; $this->accept = implode(', ', array_keys($this->acceptable)); throw new Exception\NotAcceptable($msg); } $app->di->set('responseWriter', $this->acceptable[$this->contentType], true); }
/** * This action is executed before execute any action in the application * * @param Event $event * @param Dispatcher $dispatcher * @param DispatcherException $exception */ public function beforeException(Event $event, MvcDispatcher $dispatcher, $exception) { $object = $event->getData(); $this->view->setVar('message', $object->getMessage()); switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'error', 'action' => 'show404')); return false; case Dispatcher::EXCEPTION_CYCLIC_ROUTING: $dispatcher->forward(['controller' => 'errors', 'action' => 'reports']); return false; } }
/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $me = null; $config = $this->getDI()->get('config'); $cookie = $this->getDI()->get('cookie'); $session = $this->getDI()->get('session'); // check exsited cookie if ($cookie->has('remember-me')) { $rememberMe = $cookie->get('remember-me'); $userId = $rememberMe->getValue(); $myUser = User::findFirst(['id = :id: AND status = :status:', 'bind' => ['id' => $userId, 'status' => User::STATUS_ENABLE]]); if ($myUser) { } $this->session->set('me', $me); $role = $myUser->role; } else { //Get role name from session if ($session->has('me')) { $me = $session->get('me'); $role = $me->role; } else { $role = ROLE_GUEST; } } $current_resource = $dispatcher->getModuleName() . '/' . strtolower($dispatcher->getControllerName()); $current_action = $dispatcher->getActionName(); $acl = $this->getAcl($config); $allowed = $acl->isAllowed($role, $current_resource, $current_action); if ($allowed != PhAcl::ALLOW) { $this->getDI()->getEventsManager()->fire('dispatch:beforeException', $dispatcher, new Dispatcher\Exception()); } return !$event->isStopped(); }
/** * Before exception is happening. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * @param PhalconException $exception Exception object. * * @throws \Phalcon\Exception * @return bool */ public function beforeException($event, $dispatcher, $exception) { // Handle 404 exceptions. if ($exception instanceof DispatchException) { $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show404']); return false; } if (APPLICATION_STAGE == APPLICATION_STAGE_DEVELOPMENT) { throw $exception; } else { EngineException::logException($exception); } // Handle other exceptions. $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show500']); return $event->isStopped(); }
/** * Start the query benchmark * * @param Event $event * @param Adapter $database */ public function beforeQuery(Event $event, Adapter $database) { $metadata = ['query' => $database->getSQLStatement()]; $params = $database->getSQLVariables(); if (isset($params)) { $metadata['params'] = $params; } $bindtypes = $database->getSQLBindTypes(); if (isset($bindtypes)) { $metadata['bindTypes'] = $bindtypes; } $desc = $database->getDescriptor(); if (isset($desc['dbname'])) { $metadata['database'] = $desc['dbname']; } $this->benchmark = $this->getProfiler()->start(get_class($event->getSource()) . '::query', $metadata, 'Database'); }
/** * Triggered before the dispatcher throws any exception */ public function beforeException(Event $event, Dispatcher $dispatcher, $exception) { echo 'testing'; // Handle 404 exceptions if ($exception instanceof DispatchException) { $dispatcher->forward(array('controller' => 'quick-response', 'action' => 'sendNotFoundStatus')); return false; } // Alternative way, controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'quick-response', 'action' => 'sendNotFoundStatus')); return false; } } return true; }
public function beforeDispatch(Event $event, Dispatcher $dispatcher) { $user = $this->session->get('auth'); $acl = $this->getAcl(); $role = $user && $user->role->role ? $user->role->role : 'guest'; $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); $allowed = $acl->isAllowed($role, $controller, $action); if ($allowed != Acl::ALLOW) { //Flash feedback message if in dev environment if (strpos(APPLICATION_ENV, 'development') !== false) { $this->flash->error("Route failing in security plugin - Controller: {$controller}, Action: {$action}, Role: {$role}, Url:" . $this->router->getRewriteUri()); } //stop the event $event->stop(); $this->response->redirect(); return false; } }
/** * This is called after initialize the model. * * @param Event $event Event object. * @param ModelsManager $manager Model manager * @param AbstractModel $model Model object. * * @return string */ public function afterInitialize(Event $event, ModelsManager $manager, $model) { //Reflector $reflector = $this->annotations->get($model); /** * Read the annotations in the class' docblock */ $annotations = $reflector->getClassAnnotations(); if ($annotations) { /** * Traverse the annotations */ foreach ($annotations as $annotation) { switch ($annotation->getName()) { /** * Initializes the model's source */ case 'Source': $arguments = $annotation->getArguments(); $manager->setModelSource($model, $arguments[0]); break; /** * Initializes Has-Many relations */ /** * Initializes Has-Many relations */ case 'HasMany': $arguments = $annotation->getArguments(); if (isset($arguments[3])) { $manager->addHasMany($model, $arguments[0], $arguments[1], $arguments[2], $arguments[3]); } else { $manager->addHasMany($model, $arguments[0], $arguments[1], $arguments[2]); } break; /** * Initializes BelongsTo relations */ /** * Initializes BelongsTo relations */ case 'BelongsTo': $arguments = $annotation->getArguments(); if (isset($arguments[3])) { $manager->addBelongsTo($model, $arguments[0], $arguments[1], $arguments[2], $arguments[3]); } else { $manager->addBelongsTo($model, $arguments[0], $arguments[1], $arguments[2]); } break; } } } return $event->getType(); }
/** * This event is executed before every route is executed in the dispatcher. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return bool */ public function beforeExecuteRoute($event, $dispatcher) { // Parse the annotations in the method currently executed. $annotations = $this->annotations->getMethod($dispatcher->getActiveController(), $dispatcher->getActiveMethod()); // Check if the method has an annotation 'Cache'. if ($annotations->has('Cache')) { // The method has the annotation 'Cache'. /** @var \Phalcon\Annotations\Annotation $annotation */ $annotation = $annotations->get('Cache'); // Get the lifetime. $lifetime = $annotation->getNamedArgument('lifetime'); $options = ['lifetime' => $lifetime]; // Check if there is a user defined cache key. if ($annotation->hasNamedArgument('key')) { $options['key'] = $annotation->getNamedArgument('key'); } // Enable the cache for the current method. $this->view->cache($options); } return !$event->isStopped(); }
/** * This action is executed before execute any action in the application * * @param Event $event * @param MvcDispatcher $dispatcher * @param \Exception $exception * @return boolean * @throws \Exception */ public function beforeException(Event $event, MvcDispatcher $dispatcher, $exception) { if ($exception instanceof DispatcherException) { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_INVALID_HANDLER: case Dispatcher::EXCEPTION_CYCLIC_ROUTING: $action = 'show500'; break; case Dispatcher::EXCEPTION_INVALID_PARAMS: $action = 'show400'; break; default: $action = 'show404'; } $dispatcher->forward(['controller' => 'errors', 'action' => $action]); return false; } if (APP_PRODUCTION !== APPLICATION_ENV && $exception instanceof \Exception) { throw $exception; } $dispatcher->forward(['controller' => 'errors', 'action' => 'route500']); return $event->isStopped(); }
/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $me = null; $config = $this->getDI()->get('config'); $cookie = $this->getDI()->get('cookie'); $session = $this->getDI()->get('session'); // check exsited cookie if ($cookie->has('remember-me')) { $rememberMe = $cookie->get('remember-me'); $userId = $rememberMe->getValue(); $myUser = UserModel::findFirst(['id = :id: AND status = :status:', 'bind' => ['id' => $userId, 'status' => UserModel::STATUS_ENABLE]]); if ($myUser) { $me = new \stdClass(); $me->id = $myUser->id; $me->email = $myUser->email; $me->name = $myUser->name; $me->role = $myUser->role; $me->roleName = $myUser->getRoleName(); $me->avatar = $myUser->avatar; } $this->session->set('me', $me); $role = $myUser->role; } else { //Get role name from session if ($session->has('me')) { $me = $session->get('me'); $role = $me->role; } else { $role = ROLE_GUEST; } } $current_resource = $dispatcher->getModuleName() . '/' . strtolower($dispatcher->getControllerName()); $current_action = $dispatcher->getActionName(); $acl = $this->getAcl($config); $allowed = $acl->isAllowed($role, $current_resource, $current_action); // var_dump($current_resource, $current_action, $allowed);die; if ($allowed === false && $me == null) { echo '<script type="text/javascript">self.location.href = "' . $this->getDI()->get('config')->global->baseUrl . 'login?redirect=' . base64_encode($this->getCurrentUrl()) . '"; </script>'; exit; } elseif ($allowed === false && $me->id > 0) { // khong co quyen + dang nhap roi echo '<script type="text/javascript">self.location.href = "' . $this->getDI()->get('config')->global->baseUrl . 'notfound' . '"; </script>'; exit; } return !$event->isStopped(); }
/** * Start the query benchmark * * @param Event $event * @param Adapter $database */ public function beforeQuery(Event $event, Adapter $database) { $metadata = ['query' => $database->getSQLStatement()]; $this->benchmark = $this->getProfiler()->start(get_class($event->getSource()) . '::query', $metadata, 'Database'); }
/** * Start execute route benchmark * * @param Event $event * @param DispatcherInterface $dispatcher */ public function beforeExecuteRoute(Event $event, DispatcherInterface $dispatcher) { $name = get_class($event->getSource()) . '::executeRoute'; $metadata = ['executed' => sprintf('%s::%sAction', get_class($dispatcher->getActiveController()), $dispatcher->getActionName()), 'controller' => $dispatcher->getControllerName(), 'action' => $dispatcher->getActionName(), 'params' => $dispatcher->getParams()]; $this->benchmarkRoute = $this->getProfiler()->start($name, $metadata, 'Dispatcher'); }
public function handle(\Phalcon\Events\Event $oEvent, \Phalcon\Mvc\Dispatcher $oDispatcher) { $oLogger = $this->di->get('fileLogger'); $oLogger->info($oEvent->getType() . ' happened in ' . $oDispatcher->getControllerClass() . '::' . $oDispatcher->getActionName()); }
/** * Start view benchmark * * @param Event $event * @param ViewInterface $view */ public function beforeRenderView(Event $event, ViewInterface $view) { $name = get_class($event->getSource()) . '::render: ' . basename($view->getActiveRenderPath()); $metadata = ['view' => realpath($view->getActiveRenderPath()) ?: $view->getActiveRenderPath(), 'level' => $this->getRenderLevel($view->getCurrentRenderLevel())]; $this->setBenchmark($view, $this->getProfiler()->start($name, $metadata, 'View')); }