"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.1.0-rc1/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php" (18 Nov 2020, 2005 Bytes) of package /linux/www/drupal-9.1.0-rc1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AliasPathProcessor.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\path_alias\PathProcessor;
    4 
    5 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
    6 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
    7 use Drupal\Core\Render\BubbleableMetadata;
    8 use Drupal\path_alias\AliasManagerInterface;
    9 use Symfony\Component\HttpFoundation\Request;
   10 
   11 /**
   12  * Processes the inbound path using path alias lookups.
   13  */
   14 class AliasPathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
   15 
   16   /**
   17    * An alias manager for looking up the system path.
   18    *
   19    * @var \Drupal\path_alias\AliasManagerInterface
   20    */
   21   protected $aliasManager;
   22 
   23   /**
   24    * Constructs a AliasPathProcessor object.
   25    *
   26    * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
   27    *   An alias manager for looking up the system path.
   28    */
   29   public function __construct(AliasManagerInterface $alias_manager) {
   30     $this->aliasManager = $alias_manager;
   31   }
   32 
   33   /**
   34    * {@inheritdoc}
   35    */
   36   public function processInbound($path, Request $request) {
   37     $path = $this->aliasManager->getPathByAlias($path);
   38     return $path;
   39   }
   40 
   41   /**
   42    * {@inheritdoc}
   43    */
   44   public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
   45     if (empty($options['alias'])) {
   46       $langcode = isset($options['language']) ? $options['language']->getId() : NULL;
   47       $path = $this->aliasManager->getAliasByPath($path, $langcode);
   48       // Ensure the resulting path has at most one leading slash, to prevent it
   49       // becoming an external URL without a protocol like //example.com. This
   50       // is done in \Drupal\Core\Routing\UrlGenerator::generateFromRoute()
   51       // also, to protect against this problem in arbitrary path processors,
   52       // but it is duplicated here to protect any other URL generation code
   53       // that might call this method separately.
   54       if (strpos($path, '//') === 0) {
   55         $path = '/' . ltrim($path, '/');
   56       }
   57     }
   58     return $path;
   59   }
   60 
   61 }