|  |  |  | <?php
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace frontend\urls;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use common\modules\pages\entities\Page;
 | 
					
						
							|  |  |  | use common\modules\pages\repositories\read\PageReadRepository;
 | 
					
						
							|  |  |  | use yii\base\BaseObject;
 | 
					
						
							|  |  |  | use yii\base\InvalidArgumentException;
 | 
					
						
							|  |  |  | use yii\caching\Cache;
 | 
					
						
							|  |  |  | use yii\helpers\ArrayHelper;
 | 
					
						
							|  |  |  | use yii\web\Request;
 | 
					
						
							|  |  |  | use yii\web\UrlManager;
 | 
					
						
							|  |  |  | use yii\web\UrlNormalizerRedirectException;
 | 
					
						
							|  |  |  | use yii\web\UrlRuleInterface;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PageUrlRule extends BaseObject implements UrlRuleInterface
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  |     private PageReadRepository $repository;
 | 
					
						
							|  |  |  |     private Cache $cache;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(PageReadRepository $repository, Cache $cache, $config = [])
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         parent::__construct($config);
 | 
					
						
							|  |  |  |         $this->repository = $repository;
 | 
					
						
							|  |  |  |         $this->cache = $cache;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @param UrlManager $manager
 | 
					
						
							|  |  |  |      * @param Request $request
 | 
					
						
							|  |  |  |      * @return array|false
 | 
					
						
							|  |  |  |      * @throws UrlNormalizerRedirectException
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function parseRequest($manager, $request): bool|array
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $path = $request->pathInfo;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $this->cache->getOrSet(['page_route', 'path' => $path], function () use ($path) {
 | 
					
						
							|  |  |  |             if (!$page = $this->repository->findBySlug($this->getPathSlug($path))) {
 | 
					
						
							|  |  |  |                 return ['id' => null, 'path' => null];
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             return ['id' => $page->id, 'path' => $this->getPagePath($page)];
 | 
					
						
							|  |  |  |         }, 1000);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($result['id'])) {
 | 
					
						
							|  |  |  |             return false;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($path != $result['path']) {
 | 
					
						
							|  |  |  |             throw new UrlNormalizerRedirectException(['page/view', 'id' => $result['id']], 301);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return ['page/view', ['id' => $result['id']]];
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @param UrlManager $manager
 | 
					
						
							|  |  |  |      * @param string $route
 | 
					
						
							|  |  |  |      * @param array $params
 | 
					
						
							|  |  |  |      * @return mixed
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function createUrl($manager, $route, $params): mixed
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         if ($route == 'page/view') {
 | 
					
						
							|  |  |  |             if (empty($params['id'])) {
 | 
					
						
							|  |  |  |                 throw new InvalidArgumentException('Empty id.');
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             $id = $params['id'];
 | 
					
						
							|  |  |  |             $url = $this->cache->getOrSet(['page_route', 'id' => $id], function () use ($id) {
 | 
					
						
							|  |  |  |                 if (!$page = $this->repository->find($id)) {
 | 
					
						
							|  |  |  |                     return null;
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 return $this->getPagePath($page);
 | 
					
						
							|  |  |  |             });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$url) {
 | 
					
						
							|  |  |  |             	return 'error404';
 | 
					
						
							|  |  |  |                 throw new InvalidArgumentException('Undefined id.');
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             unset($params['id']);
 | 
					
						
							|  |  |  |             if (!empty($params) && ($query = http_build_query($params)) !== '') {
 | 
					
						
							|  |  |  |                 $url .= '?' . $query;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $url;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         return false;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getPathSlug($path): string
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $chunks = explode('/', $path);
 | 
					
						
							|  |  |  |         return end($chunks);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getPagePath(Page $page): string
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $chunks = ArrayHelper::getColumn($page->getParents()->andWhere(['>', 'depth', 0])->all(), 'slug');
 | 
					
						
							|  |  |  |         $chunks[] = $page->slug;
 | 
					
						
							|  |  |  |         return implode('/', $chunks);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |