/** * * @param string $result * @param Context $context * * @return array Rule array or false if not found */ protected function find($result, Context $context) { $actionName = $context->getActionName(); if ($context->getRequest()->isXmlHttpRequest() && isset($this->rules[$actionName]['ajax:' . $result])) { return $this->rules[$actionName]['ajax:' . $result]; } elseif ($context->hasParent() && isset($this->rules[$actionName]['embed:' . $result])) { return $this->rules[$actionName]['embed:' . $result]; } return isset($this->rules[$actionName][$result]) ? $this->rules[$actionName][$result] : false; }
protected function calculateTemplate($pageName, Context $context, array $config) { $cfg = $this->getConfig($pageName, $config); // widget if ($context->hasParent()) { $tpl = $cfg['template_widget'] != null ? $cfg['template_widget'] . self::PAGES_EXTENSION : $pageName . self::PAGES_EXTENSION; } elseif ($context->getRequest()->isXmlHttpRequest()) { $tpl = $cfg['template_ajax'] != null ? $cfg['template_ajax'] . self::PAGES_EXTENSION : $pageName . self::PAGES_EXTENSION; } else { $tpl = $pageName . self::PAGES_EXTENSION; } $template = $this->path->calculate(array($config['directories']['pages'], $tpl)); return $template; }
/** * * @param array $actionData * @param array $params * * @return Response */ public function getResponse(array $actionData = array(), array $params = array()) { $actionName = isset($params['actionName']) ? $params['actionName'] : null; unset($params['actionName']); if (null === $actionName) { throw new Exception('Missing ResultType parameter "actionName"'); } $this->context->setActionName($actionName); foreach ($params as $key => $value) { if (!empty($value) && strpos($value, ':', 0) !== false && array_key_exists(substr($value, 1), $actionData)) { $value = $actionData[substr($value, 1)]; } $this->context->getRequest()->query->set($key, $value); } return $this->application->runAction($this->context); }
/** * Executes the callable and returns the result * * @param Application $app The running Application * @param Context $context Actual context * * @return mixed The controller's result */ public function execute(Application $app, Context $context) { if ($this->callable instanceof \Closure) { $refFunc = new \ReflectionFunction($this->callable); $params = array(); $request = $context->getRequest(); foreach ($refFunc->getParameters() as $param) { if ($param->getName() == self::PARAM_CONTEXT_NAME) { $params[] = $context; } elseif ($param->getName() == self::PARAM_SERVICES_NAME) { $params[] = $app->getServices(); } else { $params[] = $request->get($param->getName(), null); } } $result = call_user_func_array($this->callable, $params); } else { $result = call_user_func($this->callable); } if (is_array($result)) { $this->actionData = array_merge($result, $this->actionData); } return $result; }
/** * Executes the controller's defined method * * @param Application $app The running Application * @param Context $context Actual context * * @return mixed The controller's result */ public function execute(Application $app, Context $context) { $instance = $this->instantiate($app); $this->populate($instance, $context->getRequest()); $this->populateCoreInterfaces($instance, $app, $context); $return = call_user_func(array($instance, $this->method)); $accessor = new Accessor($instance); $data = $accessor->toArray(); // unset services and context from action data // to avoid having them in the view if ($instance instanceof ServicesAware) { unset($data['services']); } if ($instance instanceof ContextAware) { unset($data['context']); } $this->actionData = array_merge($data, $this->actionData); return $return; }
/** * * @param Context $context * * @return mixed */ public function runAction(Context $context) { if (!$context->isReady()) { throw new Exception('Context is not ready (i.e. no action defined)'); } if (!$this->exists($context->getActionName())) { throw new Exception('Unregistered action "' . $context->getActionName() . '"'); } $proxy = $this->get($context->getActionName()); $this->notify(new BeforeActionEvent($proxy, $this, $context)); if ($context->isDone()) { return $context->getResult(); } $result = $proxy->execute($this, $context); $context->setResult($result); $this->notify(new AfterActionEvent($proxy, $this, $context)); return $result; }