コード例 #1
0
ファイル: AliasManager.php プロジェクト: ddrozdik/dmaps
 /**
  * {@inheritdoc}
  */
 public function getAliasByPath($path, $langcode = NULL)
 {
     if ($path[0] !== '/') {
         throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
     }
     // If no language is explicitly specified we default to the current URL
     // language. If we used a language different from the one conveyed by the
     // requested URL, we might end up being unable to check if there is a path
     // alias matching the URL path.
     $langcode = $langcode ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId();
     // Check the path whitelist, if the top-level part before the first /
     // is not in the list, then there is no need to do anything further,
     // it is not in the database.
     if ($path === '/' || !$this->whitelist->get(strtok(trim($path, '/'), '/'))) {
         return $path;
     }
     // During the first call to this method per language, load the expected
     // paths for the page from cache.
     if (empty($this->langcodePreloaded[$langcode])) {
         $this->langcodePreloaded[$langcode] = TRUE;
         $this->lookupMap[$langcode] = array();
         // Load the cached paths that should be used for preloading. This only
         // happens if a cache key has been set.
         if ($this->preloadedPathLookups === FALSE) {
             $this->preloadedPathLookups = array();
             if ($this->cacheKey) {
                 if ($cached = $this->cache->get($this->cacheKey)) {
                     $this->preloadedPathLookups = $cached->data;
                 } else {
                     $this->cacheNeedsWriting = TRUE;
                 }
             }
         }
         // Load paths from cache.
         if (!empty($this->preloadedPathLookups[$langcode])) {
             $this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups[$langcode], $langcode);
             // Keep a record of paths with no alias to avoid querying twice.
             $this->noAlias[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups[$langcode], array_keys($this->lookupMap[$langcode])));
         }
     }
     // If we already know that there are no aliases for this path simply return.
     if (!empty($this->noAlias[$langcode][$path])) {
         return $path;
     }
     // If the alias has already been loaded, return it from static cache.
     if (isset($this->lookupMap[$langcode][$path])) {
         return $this->lookupMap[$langcode][$path];
     }
     // Try to load alias from storage.
     if ($alias = $this->storage->lookupPathAlias($path, $langcode)) {
         $this->lookupMap[$langcode][$path] = $alias;
         return $alias;
     }
     // We can't record anything into $this->lookupMap because we didn't find any
     // aliases for this path. Thus cache to $this->noAlias.
     $this->noAlias[$langcode][$path] = TRUE;
     return $path;
 }
コード例 #2
0
 protected function preloadSourceItems($langcode)
 {
     if ($item = cache_get($this->cacheKey, 'cache_path')) {
         // FIXME
         $sources = $item->data;
         if (!empty($sources)) {
             $aliases = $this->storage->preloadPathAlias($item->data, $langcode);
             // Also set to false anything that is not set.
             foreach ($sources as $source) {
                 if (!isset($aliases[$source])) {
                     $aliases[$source] = false;
                 }
             }
             return $aliases;
         }
     }
     return [];
 }