You are here

public function RemoteImageStyleRoutes::routes in Remote Stream Wrapper 8

Returns an array of route objects.

Return value

\Symfony\Component\Routing\Route[] An array of route objects.

1 string reference to 'RemoteImageStyleRoutes::routes'
remote_stream_wrapper.routing.yml in ./remote_stream_wrapper.routing.yml
remote_stream_wrapper.routing.yml

File

src/Routing/RemoteImageStyleRoutes.php, line 59

Class

RemoteImageStyleRoutes
Defines a route subscriber to register a url for serving image styles.

Namespace

Drupal\remote_stream_wrapper\Routing

Code

public function routes() {
  $routes = [];

  // This functionality relies on image module being installed.
  if (!$this->moduleHandler
    ->moduleExists('image')) {
    return $routes;
  }

  // Calling getWrappers() here returns an empty array here. Calling the
  // register method seems to resolve it.
  if (method_exists($this->streamWrapperManager, 'register')) {
    $this->streamWrapperManager
      ->register();
  }

  // @todo Use the default scheme instead of hard-coding to public.
  $public_directory_path = $this->streamWrapperManager
    ->getViaScheme('public')
    ->getDirectoryPath();

  // Find all remote stream wrappers.
  $wrappers = $this->streamWrapperManager
    ->getWrappers();
  $remote_wrappers = array_filter($wrappers, function ($wrapper) {
    return file_is_wrapper_remote($wrapper['class']);
  });
  $remote_schemes = array_keys($remote_wrappers);
  foreach ($remote_schemes as $scheme) {

    // Manually specify the scheme so that the route is preferred over the
    // image.style_public route.
    $routes['image.style_' . $scheme] = new Route('/' . $public_directory_path . '/styles/{image_style}/' . $scheme, [
      '_controller' => 'Drupal\\remote_stream_wrapper\\Controller\\RemoteImageStyleDownloadController::deliver',
      'scheme' => $scheme,
      '_disable_route_normalizer' => TRUE,
    ], [
      '_access' => 'TRUE',
    ]);
  }
  return $routes;
}