Ejemplo n.º 1
0
 /**
  * 缓存管理API
  *
  * @param Request $request
  * @param Setting $settingModel
  * @param SmpJsonView $view
  *
  * @return \Focus\MVC\View
  */
 public function cacheAction(Request $request, Setting $settingModel, SmpJsonView $view)
 {
     // __tags__, __nav__, post_[id], list_[current], list_[cat]_[current], tag_[tagName]_[current]
     $key = $request->get('key');
     $clear = $request->get('clear', 'false');
     $token = $request->get('token', '');
     $byprefix = $request->get('byprefix', false);
     if (empty($key)) {
         return $view->assign('status', false)->assign('info', 'key_required');
     }
     $tokenEntity = $settingModel->getSetting('cache_token', 'system');
     if (empty($tokenEntity) || !isset($tokenEntity['setting_value']) || $token !== $tokenEntity['setting_value']) {
         return $view->assign('status', false)->assign('info', 'token_invalid');
     }
     // 通过前缀处理
     if ($byprefix === 'true') {
         $res = $this->getCache()->all($key);
         $view->assign('original', $res);
         if ($clear == 'true') {
             foreach ($res as $_key => $_val) {
                 $this->getCache()->delete($_key);
             }
         }
         return $view->assign('status', true)->assign('info', 'clear ' + count($res) + ' items');
     }
     $res = $this->getCache()->get($key);
     if ($res !== false) {
         $this->assign('original', $res);
         if ($clear == 'true') {
             return $view->assign('status', $this->getCache()->delete($key));
         }
     }
     return $view->assign('status', $res);
 }
Ejemplo n.º 2
0
 /**
  * 根据标签名称获取相关文章
  *
  * @param Request $request
  * @param Tags $tagModel
  * @param Post $postModel
  * @param SmpJsonView $jsonView
  *
  * @return SmpJsonView
  */
 public function relatedPostsAction(Request $request, Tags $tagModel, Post $postModel, SmpJsonView $jsonView)
 {
     $tags = $tagModel->getTagsByNames(explode(',', $request->get('tags', '')));
     $tag_ids = [];
     foreach ($tags as $tag) {
         $tag_ids[] = $tag['id'];
     }
     $posts = $postModel->getPostsInfoByTags($tag_ids, 10);
     $jsonView->assign('posts', $posts);
     $jsonView->assign('status', true);
     return $jsonView;
 }
Ejemplo n.º 3
0
 /**
  * 文章列表页 (标签)
  *
  * @param Request $request
  * @param \Demo\Models\Post $postModel
  * @param \Demo\Models\Tags $tagModel
  *
  * @return string
  */
 public function tagAction(Request $request, \Demo\Models\Post $postModel, \Demo\Models\Tags $tagModel)
 {
     $tagName = $request->get('tag');
     $current = intval($request->get('page', 1));
     $key = "tag_{$tagName}_{$current}";
     $html = $this->getCache()->get($key);
     if (false === $html) {
         $tag = $tagModel->getTagByName($tagName);
         $posts = $postModel->getPostsByTag($tag['id'], $current);
         $this->assign('posts', $posts['data']);
         $this->assign('page', $posts['page']);
         $this->assign('__tag__', $tag);
         $this->assign('__navcur__', '技不压身');
         $this->assign('parsedown', new \Parsedown());
         $this->assign('catModel', new Category());
         $html = $this->view('index')->render();
         $this->getCache()->set($key, $html);
     }
     return $html;
 }
Ejemplo n.º 4
0
 /**
  * Processing request
  *
  * @param Request $request Request Object
  * @param Response $response Response Object
  * @param mixed $params
  *
  * @return void
  */
 public function execute(Request $request, Response $response, ...$params)
 {
     try {
         $className = "{$this->_controllerNs}\\{$this->_controllerName}";
         $methodName = $this->_actionName . "Action";
         $classRefl = new \ReflectionClass($className);
         $instance = $classRefl->newInstance();
         if (method_exists($instance, '__init__')) {
             $instance->__init__();
             $this->getLogger()->debug("exec: {$className}->__init__");
         }
         if (!method_exists($instance, $methodName)) {
             $this->getLogger()->debug('the request method not exist');
             throw new HttpNotFoundException('The request method not exist!');
         }
         $methodRefl = $classRefl->getMethod($methodName);
         $paramsRefl = $methodRefl->getParameters();
         $params = [];
         foreach ($paramsRefl as $index => $param) {
             $paramClass = $param->getClass();
             if ($paramClass->implementsInterface('Focus\\Request\\Request')) {
                 $params[$index] = $request;
             } else {
                 if ($paramClass->implementsInterface('Focus\\Response\\Response')) {
                     $params[$index] = $response;
                 } else {
                     if ($paramClass->implementsInterface('Focus\\Request\\Session')) {
                         $params[$index] = $request->session();
                     } else {
                         if ($paramClass->implementsInterface('Focus\\Config\\Config')) {
                             $params[$index] = $request->config();
                         } else {
                             if ($paramClass->implementsInterface('Focus\\MVC\\Model') || $paramClass->implementsInterface('Focus\\MVC\\Service')) {
                                 if ($request->container()->has($paramClass->getName())) {
                                     $object = $request->container()->get($paramClass->getName());
                                 } else {
                                     $object = $paramClass->newInstance();
                                 }
                                 $params[$index] = $object;
                                 $params[$index]->init();
                             } else {
                                 if ($paramClass->implementsInterface('Focus\\MVC\\View')) {
                                     $params[$index] = $paramClass->newInstance();
                                 } else {
                                     if (!empty($this->_pathParams)) {
                                         $params[$index] = array_shift($this->_pathParams);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $res = $instance->{$methodName}(...$params);
         if ($res instanceof View) {
             $this->getLogger()->debug('use view object for response');
             $res->output($response);
         } else {
             if (is_string($res) || is_numeric($res)) {
                 $this->getLogger()->debug('use scalar type for response');
                 $response->write($res);
             }
         }
         return $res;
     } catch (\ReflectionException $exception) {
         $this->getLogger()->warning(sprintf('reflection exception: [%s] %s', $exception->getCode(), $exception->getMessage()));
         throw new \RuntimeException($exception->getMessage(), $exception->getCode(), $exception);
     }
 }