diff --git a/src/Plugin/libraries/Locator/StreamLocator.php b/src/Plugin/libraries/Locator/StreamLocator.php index 9b5ddd78e740c1c64240f35060027b77dfa0df07..18731763416d1c59bc33d87cd5adb4f2c965824c 100644 --- a/src/Plugin/libraries/Locator/StreamLocator.php +++ b/src/Plugin/libraries/Locator/StreamLocator.php @@ -2,8 +2,8 @@ namespace Drupal\libraries\Plugin\libraries\Locator; -use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface; use Drupal\libraries\ExternalLibrary\Local\LocatorInterface; use Drupal\libraries\Plugin\MissingPluginConfigurationException; @@ -14,8 +14,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * * It makes the following assumptions: * - The library files can be accessed using a specified stream. - * - The stream wrapper is local (i.e. it returns a proper value in its - * realpath() method). + * - The stream wrapper is local (i.e. it is a subclass of + * \Drupal\Core\StreamWrapper\LocalStream). * - The first component of the file URIs are the library IDs (i.e. file URIs * are of the form: scheme://library-id/path/to/file/filename). * @@ -26,18 +26,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface { /** - * The file system helper. + * The stream wrapper manager. * - * @var \Drupal\Core\File\FileSystemInterface + * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface */ - protected $fileSystemHelper; - - /** - * The app root. - * - * @var string - */ - protected $appRoot; + protected $streamWrapperManager; /** * The scheme of the stream wrapper. @@ -49,16 +42,13 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface /** * Constructs a stream locator. * - * @param \Drupal\Core\File\FileSystemInterface $file_system_helper - * The file system helper. - * @param string $app_root - * The app root. + * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager + * The stream wrapper manager. * @param string $scheme * The scheme of the stream wrapper. */ - public function __construct(FileSystemInterface $file_system_helper, $app_root, $scheme) { - $this->fileSystemHelper = $file_system_helper; - $this->appRoot = (string) $app_root; + public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, $scheme) { + $this->streamWrapperManager = $stream_wrapper_manager; $this->scheme = (string) $scheme; } @@ -69,7 +59,7 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface if (!isset($configuration['scheme'])) { throw new MissingPluginConfigurationException($plugin_id, $plugin_definition, $configuration, 'scheme'); } - return new static($container->get('file_system'), $container->get('app.root'), $configuration['scheme']); + return new static($container->get('stream_wrapper_manager'), $configuration['scheme']); } /** @@ -81,14 +71,15 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate() */ public function locate(LocalLibraryInterface $library) { - $path = $this->fileSystemHelper->realpath($this->getUri($library)); - - if ($path && is_dir($path) && is_readable($path)) { - // Set a path relative to the app root. - assert('strpos($path, $this->appRoot . "/") === 0', "Path: $path"); - $path = str_replace($this->appRoot . '/', '', $path); - assert('$path[0] !== "/"'); + /** @var \Drupal\Core\StreamWrapper\LocalStream $stream_wrapper */ + $stream_wrapper = $this->streamWrapperManager->getViaScheme($this->scheme); + assert('$stream_wrapper instanceof \Drupal\Core\StreamWrapper\LocalStream'); + // Calling LocalStream::getDirectoryPath() explicitly avoids the realpath() + // usage in LocalStream::getLocalPath(), which breaks if Libraries API is + // symbolically linked into the Drupal installation. + $path = $stream_wrapper->getDirectoryPath() . '/' . $library->getId(); + if (is_dir($path) && is_readable($path)) { $library->setLocalPath($path); } else { @@ -96,17 +87,4 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface } } - /** - * Gets the URI of a library. - * - * @param \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $library - * The library. - * - * @return string - * The URI of the library. - */ - protected function getUri(LocalLibraryInterface $library) { - return $this->scheme . '://' . $library->getId(); - } - }