public function testTheMockUp()
 {
     // Tests registered aliases
     $this->assertSame('alias-en', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'en'));
     $this->assertSame('alias-fr', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'fr'));
     $this->assertSame('alias-und', $this->aliasStorage->lookupPathAlias('duplicate-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED));
     // Non existing language returns the undefined language alias
     $this->assertSame('alias-und', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'martian'));
     // More basic tests
     $this->assertSame('duplicate-langcode', $this->aliasStorage->lookupPathSource('alias-fr', 'fr'));
     $this->assertSame('duplicate-langcode', $this->aliasStorage->lookupPathSource('alias-en', 'en'));
     $this->assertFalse($this->aliasStorage->lookupPathSource('alias-fr', LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $this->assertFalse($this->aliasStorage->lookupPathSource('alias-en', LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $this->assertFalse($this->aliasStorage->lookupPathSource('alias-fr', 'en'));
     $this->assertFalse($this->aliasStorage->lookupPathSource('alias-en', 'fr'));
 }
Beispiel #2
0
 /**
  * {@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;
 }
 protected function lookup($type, $lookup, $langcode = null)
 {
     if (self::SOURCE !== $type) {
         // Exclude all admin paths
         if ($this->excludeAdminPath && path_is_admin($lookup)) {
             return $lookup;
         }
         // Also check for whitelist
         if (false !== $this->whitelist && !isset($this->whitelist[strtok($lookup, '/')])) {
             return $lookup;
         }
     }
     if (!$langcode) {
         $langcode = $GLOBALS['language']->getId();
     }
     // Compute cache key lookup
     if (self::SOURCE === $type) {
         $dest = self::ALIAS;
     } else {
         $type = self::ALIAS;
         $dest = self::SOURCE;
         if ($this->doCache && !$this->isPreloaded) {
             if (empty($this->data[self::ALIAS][$langcode])) {
                 $this->data[self::ALIAS][$langcode] = [];
             }
             $this->data[self::ALIAS][$langcode] += $this->preloadSourceItems($langcode);
             $this->isPreloaded = true;
         }
     }
     if (isset($this->data[$type][$langcode][$lookup])) {
         $found = $this->data[$type][$langcode][$lookup];
         return $found ? $found : $lookup;
     }
     $this->dataIsUpdated = true;
     if (self::SOURCE === $type) {
         $found = $this->storage->lookupPathSource($lookup, $langcode);
     } else {
         $found = $this->storage->lookupPathAlias($lookup, $langcode);
     }
     if ($found) {
         $this->data[$type][$langcode][$lookup] = $found;
         $this->data[$dest][$langcode][$found] = $lookup;
     } else {
         $this->data[$type][$langcode][$lookup] = false;
     }
     return $found ? $found : $lookup;
 }