PathProcessor.php in Sub-pathauto (Sub-path URL Aliases) 8
File
src/PathProcessor.php
View source
<?php
namespace Drupal\subpathauto;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;
class PathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
protected $pathProcessor;
protected $languageManager;
protected $configFactory;
protected $pathValidator;
protected $recursiveCall;
public function __construct(InboundPathProcessorInterface $path_processor, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory) {
$this->pathProcessor = $path_processor;
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
}
public function processInbound($path, Request $request) {
$request_path = $this
->getPath($request
->getPathInfo());
if ($request_path !== $path || $this->recursiveCall) {
return $path;
}
$original_path = $path;
$max_depth = $this
->getMaxDepth();
$subpath = [];
$i = 0;
while (($path_array = explode('/', ltrim($path, '/'))) && ($max_depth === 0 || $i < $max_depth)) {
$i++;
$subpath[] = array_pop($path_array);
if (empty($path_array)) {
break;
}
$path = '/' . implode('/', $path_array);
$processed_path = $this->pathProcessor
->processInbound($path, $request);
if ($processed_path !== $path) {
$path = $processed_path . '/' . implode('/', array_reverse($subpath));
$valid_path = $this
->isValidPath($path);
if ($valid_path) {
return $path;
}
break;
}
}
return $original_path;
}
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleableMetadata = NULL) {
$original_path = $path;
$subpath = [];
$max_depth = $this
->getMaxDepth();
$i = 0;
while (($path_array = explode('/', ltrim($path, '/'))) && ($max_depth === 0 || $i < $max_depth)) {
$i++;
$subpath[] = array_pop($path_array);
if (empty($path_array)) {
break;
}
$path = '/' . implode('/', $path_array);
$processed_path = $this->pathProcessor
->processOutbound($path, $options, $request);
if ($processed_path !== $path) {
$path = $processed_path . '/' . implode('/', array_reverse($subpath));
return $path;
}
}
return $original_path;
}
protected function getPath($path_info) {
$language_prefix = '/' . $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_URL)
->getId() . '/';
if (substr($path_info, 0, strlen($language_prefix)) == $language_prefix) {
$path_info = '/' . substr($path_info, strlen($language_prefix));
}
return $path_info;
}
protected function isValidPath($path) {
$this->recursiveCall = TRUE;
$is_valid = (bool) $this
->getPathValidator()
->getUrlIfValidWithoutAccessCheck($path);
$this->recursiveCall = FALSE;
return $is_valid;
}
protected function getPathValidator() {
if (!$this->pathValidator) {
$this
->setPathValidator(\Drupal::service('path.validator'));
}
return $this->pathValidator;
}
public function setPathValidator(PathValidatorInterface $path_validator) {
$this->pathValidator = $path_validator;
return $this;
}
protected function getMaxDepth() {
return $this->configFactory
->get('subpathauto.settings')
->get('depth');
}
}