FlysystemRoutes.php in Flysystem 3.x
File
src/Routing/FlysystemRoutes.php
View source
<?php
namespace Drupal\flysystem\Routing;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\flysystem\FlysystemFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class FlysystemRoutes implements ContainerInjectionInterface {
protected $factory;
protected $moduleHandler;
protected $streamWrapperManager;
public function __construct(FlysystemFactory $factory, StreamWrapperManagerInterface $stream_wrapper_manager, ModuleHandlerInterface $module_handler) {
$this->factory = $factory;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('flysystem_factory'), $container
->get('stream_wrapper_manager'), $container
->get('module_handler'));
}
public function routes() {
$public_directory_path = $this->streamWrapperManager
->getViaScheme('public')
->getDirectoryPath();
$routes = [];
$all_settings = Settings::get('flysystem', []);
foreach ($this->factory
->getSchemes() as $scheme) {
$settings = $all_settings[$scheme];
if ($settings['driver'] !== 'local' || empty($settings['config']['public'])) {
continue;
}
if ($settings['config']['root'] === $public_directory_path) {
continue;
}
$routes['flysystem.' . $scheme . '.serve'] = new Route('/' . $settings['config']['root'], [
'_controller' => 'Drupal\\system\\FileDownloadController::download',
'_disable_route_normalizer' => TRUE,
'scheme' => $scheme,
], [
'_access' => 'TRUE',
]);
if ($this->moduleHandler
->moduleExists('image')) {
$routes['flysystem.' . $scheme . '.style_public'] = new Route('/' . $settings['config']['root'] . '/styles/{image_style}/' . $scheme, [
'_controller' => 'Drupal\\image\\Controller\\ImageStyleDownloadController::deliver',
'_disable_route_normalizer' => TRUE,
'scheme' => $scheme,
], [
'_access' => 'TRUE',
]);
}
}
if ($this->moduleHandler
->moduleExists('image')) {
$routes['flysystem.image_style'] = new Route('/_flysystem/styles/{image_style}/{scheme}', [
'_controller' => 'Drupal\\image\\Controller\\ImageStyleDownloadController::deliver',
'_disable_route_normalizer' => TRUE,
], [
'_access' => 'TRUE',
'scheme' => '^[a-zA-Z0-9+.-]+$',
]);
}
return $routes;
}
}
Classes
Name |
Description |
FlysystemRoutes |
Defines a route subscriber to register a url for serving image styles. |