You are here

public function PathProcessor::processInbound in Sub-pathauto (Sub-path URL Aliases) 8

Processes the inbound path.

Implementations may make changes to the request object passed in but should avoid all other side effects. This method can be called to process requests other than the current request.

Parameters

string $path: The path to process, with a leading slash.

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the request to process. Note, if this method is being called via the path_processor_manager service and is not part of routing, the current request object must be cloned before being passed in.

Return value

string The processed path.

Overrides InboundPathProcessorInterface::processInbound

File

src/PathProcessor.php, line 73

Class

PathProcessor
Processes the inbound path using path alias lookups.

Namespace

Drupal\subpathauto

Code

public function processInbound($path, Request $request) {
  $request_path = $this
    ->getPath($request
    ->getPathInfo());

  // The path won't be processed if the path has been already modified by
  // a path processor (including this one), or if this is a recursive call
  // caused by ::isValidPath.
  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));

      // Ensure that the path generated is valid. Call ::isValidPath to
      // trigger path processors one more time to ensure proposed new path is
      // valid. Since this method has generated the path, it should ignore all
      // recursive calls made for this method.
      $valid_path = $this
        ->isValidPath($path);

      // Use generated path if it's valid, otherwise give up and return
      // original path to give other path processors chance to make their
      // modifications for the path.
      if ($valid_path) {
        return $path;
      }
      break;
    }
  }
  return $original_path;
}