actionFactory = $actionFactory; $this->helper = $helper; } /** * @param RequestInterface $request * * @return ActionInterface|null */ public function match(RequestInterface $request) { if (!$this->helper->isEnabled()) { return null; } $rssAction = "rss.xml"; $identifier = trim($request->getPathInfo(), '/'); $urlSuffix = $this->helper->getUrlSuffix(); if ($length = strlen($urlSuffix)) { if (substr($identifier, -$length) === $urlSuffix && !$this->isRss($identifier)) { $identifier = substr($identifier, 0, strlen($identifier) - $length); } else { $identifier = $this->checkRssIdentifier($identifier); } } elseif (strpos($identifier, $rssAction) !== false) { $identifier = $this->checkRssIdentifier($identifier); } $routePath = explode('/', (string)$identifier); $routeSize = count($routePath); $route = array_shift($routePath); $blogRoute = $this->helper->getRoute(); $isCategoryRoute = $this->helper->getCategoryRoute() !== $blogRoute && $route === $this->helper->getCategoryRoute(); if (!$routeSize || ($routeSize > 3) || (!$isCategoryRoute && $route !== $blogRoute) ) { return null; } $request->setModuleName('mpblog') ->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $identifier . $urlSuffix); $controller = $isCategoryRoute ? 'category' : array_shift($routePath); if (!$isCategoryRoute && $controller === $this->helper->getPostUrlPrefix()) { $controller = 'post'; } if (!$controller) { $request->setControllerName('post') ->setActionName('index') ->setPathInfo('/mpblog/post/index'); return $this->actionFactory->create(Forward::class); } $action = array_shift($routePath) ?: 'index'; [$controller, $request, $action] = $this->switchController($controller, $request, $action); $request->setControllerName($controller) ->setActionName($action) ->setPathInfo('/mpblog/' . $controller . '/' . $action); return $this->actionFactory->create(Forward::class); } /** * @param $controller * @param $request * @param $action */ public function switchController($controller, $request, $action) { switch ($controller) { case 'post': if (!in_array($action, ['index', 'rss'])) { $post = $this->helper->getObjectByParam($action, 'url_key'); $request->setParam('id', $post->getId()); $action = 'view'; } break; case 'category': if (!in_array($action, ['index', 'rss'])) { $category = $this->helper->getObjectByParam($action, 'url_key', Data::TYPE_CATEGORY); $request->setParam('id', $category->getId()); $action = 'view'; } break; case 'tag': $tag = $this->helper->getObjectByParam($action, 'url_key', Data::TYPE_TAG); $request->setParam('id', $tag->getId()); $action = 'view'; break; case 'topic': $topic = $this->helper->getObjectByParam($action, 'url_key', Data::TYPE_TOPIC); $request->setParam('id', $topic->getId()); $action = 'view'; break; case 'author': $author = $this->helper->getObjectByParam($action, 'url_key', Data::TYPE_AUTHOR); $request->setParam('id', $author->getId()); $action = 'view'; break; case 'month': $request->setParam('month_key', $action); $action = 'view'; break; default: $post = $this->helper->getObjectByParam($controller, 'url_key'); $request->setParam('id', $post->getId()); $controller = 'post'; $action = 'view'; } return [$controller, $request, $action]; } /** * check if action = rss * * @param $identifier * * @return bool */ public function isRss($identifier) { $routePath = explode('/', $identifier ?? ''); $routePath = array_pop($routePath); $routePath = explode('.', $routePath ?? ''); $action = array_shift($routePath); return $action === 'rss'; } /** * @param $identifier * * @return bool|null|string */ public function checkRssIdentifier($identifier) { $length = strlen(self::URL_SUFFIX_RSS_XML); if (substr($identifier, -$length) == self::URL_SUFFIX_RSS_XML && $this->isRss($identifier)) { $identifier = substr($identifier, 0, strlen($identifier) - $length); return $identifier; } return null; } }