You are here

class FlysystemPathProcessor in Flysystem 8

Same name and namespace in other branches
  1. 3.x src/PathProcessor/FlysystemPathProcessor.php \Drupal\flysystem\PathProcessor\FlysystemPathProcessor
  2. 2.0.x src/PathProcessor/FlysystemPathProcessor.php \Drupal\flysystem\PathProcessor\FlysystemPathProcessor
  3. 3.0.x src/PathProcessor/FlysystemPathProcessor.php \Drupal\flysystem\PathProcessor\FlysystemPathProcessor

Defines a path processor to rewrite Flysystem URLs.

As the route system does not allow arbitrary amount of parameters convert the file path to a query parameter on the request.

Hierarchy

Expanded class hierarchy of FlysystemPathProcessor

1 file declares its use of FlysystemPathProcessor
FlysystemPathProcessorTest.php in tests/src/Unit/PathProcessor/FlysystemPathProcessorTest.php
1 string reference to 'FlysystemPathProcessor'
flysystem.services.yml in ./flysystem.services.yml
flysystem.services.yml
1 service uses FlysystemPathProcessor
path_processor.flysystem in ./flysystem.services.yml
Drupal\flysystem\PathProcessor\FlysystemPathProcessor

File

src/PathProcessor/FlysystemPathProcessor.php, line 14

Namespace

Drupal\flysystem\PathProcessor
View source
class FlysystemPathProcessor implements InboundPathProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function processInbound($path, Request $request) {

    // Quick exit.
    if (strpos($path, '/_flysystem/') !== 0) {
      return $path;
    }

    // Stream wrapper protocols must conform to /^[a-zA-Z0-9+.-]+$/
    // Via php_stream_wrapper_scheme_validate() in the PHP source.
    if (!preg_match('|^/_flysystem/([a-zA-Z0-9+.-]+)/|', $path, $matches)) {
      return $path;
    }
    $rest = substr($path, strlen($matches[0]));

    // Support image styles.
    if (strpos($rest, 'styles/') === 0 && substr_count($rest, '/') >= 3) {
      list(, $image_style, $scheme, $file) = explode('/', $rest, 4);

      // Set the file as query parameter.
      $request->query
        ->set('file', $file);
      return '/_flysystem/styles/' . $image_style . '/' . $scheme;
    }

    // Routes to FileDownloadController::download().
    $request->query
      ->set('file', $rest);
    return '/_flysystem/' . $matches[1];
  }

}

Members