You are here

public function TextimagePathProcessor::processInbound in Textimage 8.4

Same name and namespace in other branches
  1. 8.3 src/PathProcessor/TextimagePathProcessor.php \Drupal\textimage\PathProcessor\TextimagePathProcessor::processInbound()

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/TextimagePathProcessor.php, line 38

Class

TextimagePathProcessor
Defines a path processor to rewrite Textimage URLs.

Namespace

Drupal\textimage\PathProcessor

Code

public function processInbound($path, Request $request) {
  $public_directory_path = $this->streamWrapperManager
    ->getViaScheme('public')
    ->getDirectoryPath();
  if (strpos($path, '/' . $public_directory_path . '/textimage_store/') === 0) {

    // Path is for deferred Textimage generation from public scheme.
    $path_prefix = '/' . $public_directory_path . '/textimage_store';

    // Strip out path prefix.
    $rest = preg_replace('|^' . preg_quote($path_prefix . '/', '|') . '|', '', $path);

    // Set the file as query parameter.
    $request->query
      ->set('file', $rest);
    return $path_prefix;
  }
  elseif (strpos($path, '/system/files/textimage_store/') === 0) {

    // Path is for deferred Textimage generation from private scheme.
    $path_prefix = '/system/files/textimage_store';

    // Strip out path prefix.
    $rest = preg_replace('|^' . preg_quote($path_prefix . '/', '|') . '|', '', $path);

    // Set the file as query parameter.
    $request->query
      ->set('file', $rest);
    return $path_prefix;
  }
  elseif (strpos($path, '/' . $public_directory_path . '/textimage/') === 0) {

    // Path is for direct URL Textimage generation.
    $path_prefix = '/' . $public_directory_path . '/textimage';

    // Strip out path prefix.
    $rest = preg_replace('|^' . preg_quote($path_prefix . '/', '|') . '|', '', $path);

    // Get the image style and text.
    if (substr_count($rest, '/') >= 1) {
      list($image_style, $text) = explode('/', $rest, 2);

      // Set the text as query parameter.
      $request->query
        ->set('text', $text);
      return $path_prefix . '/' . $image_style;
    }
    else {
      return $path;
    }
  }
  else {
    return $path;
  }
}