Jarves\PageStack::getCachedUrlToPage PHP Method

getCachedUrlToPage() public method

Returns a array map url -> nodeId
public getCachedUrlToPage ( integer $domainId ) : array
$domainId integer
return array
    public function getCachedUrlToPage($domainId)
    {
        $cacheKey = 'core/urls/' . $domainId;
        $urls = $this->cacher->getDistributedCache($cacheKey);
        if (!$urls) {
            $nodes = NodeQuery::create()->select(array('id', 'urn', 'lvl', 'type'))->filterByDomainId($domainId)->orderByBranch()->find();
            //build urls array
            $urls = array();
            $level = array();
            foreach ($nodes as $node) {
                if ($node['lvl'] == 0) {
                    continue;
                }
                //root
                if ($node['type'] == 3) {
                    continue;
                }
                //deposit
                if ($node['type'] == 2 || $node['urn'] == '') {
                    //folder or empty url
                    $level[$node['lvl'] + 0] = isset($level[$node['lvl'] - 1]) ? $level[$node['lvl'] - 1] : '';
                    continue;
                }
                $url = isset($level[$node['lvl'] - 1]) ? $level[$node['lvl'] - 1] : '';
                $url .= '/' . $node['urn'];
                $level[$node['lvl'] + 0] = $url;
                $urls[$url] = $node['id'];
            }
            $this->cacher->setDistributedCache($cacheKey, $urls);
        }
        return $urls;
    }

Usage Example

Example #1
0
 /**
  * Returns the id of given path-info. Null if not existent.
  *
  * @return Node|null
  */
 public function searchPage()
 {
     $url = $this->getRequest()->getPathInfo();
     $page = null;
     $title = sprintf('Searching Page [%s]', $url);
     $this->stopwatch->start($title);
     if (!$page) {
         $domain = $this->pageStack->getCurrentDomain();
         $urls = $this->pageStack->getCachedUrlToPage($domain->getId());
         $possibleUrl = $url;
         $id = false;
         while (1) {
             if (isset($urls[$possibleUrl])) {
                 $id = $urls[$possibleUrl];
                 break;
             }
             if (false !== ($pos = strrpos($possibleUrl, '/'))) {
                 $possibleUrl = substr($possibleUrl, 0, $pos);
             } else {
                 break;
             }
         }
         if (!$id) {
             //set to startpage
             $id = $domain->getStartnodeId();
             $possibleUrl = '/';
         }
         $url = $possibleUrl;
         if ($url == '/') {
             $pageId = $this->pageStack->getCurrentDomain()->getStartnodeId();
             if (!$pageId > 0) {
                 $this->eventDispatcher->dispatch('core/domain-no-start-page');
             }
         } else {
             $pageId = $id;
         }
         /** @var \Jarves\Model\Node $page */
         $page = $this->pageStack->getPage($pageId);
     }
     $this->stopwatch->stop($title);
     return $page;
 }