5fdbd2f5eca2c782a015b7d035122dbf672598c8
[yaffs-website] / web / core / modules / image / src / PathProcessor / PathProcessorImageStyles.php
1 <?php
2
3 namespace Drupal\image\PathProcessor;
4
5 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Defines a path processor to rewrite image styles URLs.
11  *
12  * As the route system does not allow arbitrary amount of parameters convert
13  * the file path to a query parameter on the request.
14  *
15  * This processor handles two different cases:
16  * - public image styles: In order to allow the webserver to serve these files
17  *   directly, the route is registered under the same path as the image style so
18  *   it took over the first generation. Therefore the path processor converts
19  *   the file path to a query parameter.
20  * - private image styles: In contrast to public image styles, private
21  *   derivatives are already using system/files/styles. Similar to public image
22  *   styles, it also converts the file path to a query parameter.
23  */
24 class PathProcessorImageStyles implements InboundPathProcessorInterface {
25
26   /**
27    * The stream wrapper manager service.
28    *
29    * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
30    */
31   protected $streamWrapperManager;
32
33   /**
34    * Constructs a new PathProcessorImageStyles object.
35    *
36    * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
37    *   The stream wrapper manager service.
38    */
39   public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
40     $this->streamWrapperManager = $stream_wrapper_manager;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function processInbound($path, Request $request) {
47     $directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath();
48     if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
49       $path_prefix = '/' . $directory_path . '/styles/';
50     }
51     elseif (strpos($path, '/system/files/styles/') === 0) {
52       $path_prefix = '/system/files/styles/';
53     }
54     else {
55       return $path;
56     }
57
58     // Strip out path prefix.
59     $rest = preg_replace('|^' . preg_quote($path_prefix, '|') . '|', '', $path);
60
61     // Get the image style, scheme and path.
62     if (substr_count($rest, '/') >= 2) {
63       list($image_style, $scheme, $file) = explode('/', $rest, 3);
64
65       // Set the file as query parameter.
66       $request->query->set('file', $file);
67
68       return $path_prefix . $image_style . '/' . $scheme;
69     }
70     else {
71       return $path;
72     }
73   }
74
75 }