Skip to content
Snippets Groups Projects
Commit 42ec6c8d authored by Tobias Zimmermann's avatar Tobias Zimmermann
Browse files

Issue #2770983 by tstoeckler: Allow libraries to be symlinked to their

right place.
parent 548f3c2c
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
namespace Drupal\libraries\Plugin\libraries\Locator; namespace Drupal\libraries\Plugin\libraries\Locator;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface; use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
use Drupal\libraries\ExternalLibrary\Local\LocatorInterface; use Drupal\libraries\ExternalLibrary\Local\LocatorInterface;
use Drupal\libraries\Plugin\MissingPluginConfigurationException; use Drupal\libraries\Plugin\MissingPluginConfigurationException;
...@@ -14,8 +14,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -14,8 +14,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* *
* It makes the following assumptions: * It makes the following assumptions:
* - The library files can be accessed using a specified stream. * - The library files can be accessed using a specified stream.
* - The stream wrapper is local (i.e. it returns a proper value in its * - The stream wrapper is local (i.e. it is a subclass of
* realpath() method). * \Drupal\Core\StreamWrapper\LocalStream).
* - The first component of the file URIs are the library IDs (i.e. file URIs * - 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). * are of the form: scheme://library-id/path/to/file/filename).
* *
...@@ -26,18 +26,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -26,18 +26,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface { 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; protected $streamWrapperManager;
/**
* The app root.
*
* @var string
*/
protected $appRoot;
/** /**
* The scheme of the stream wrapper. * The scheme of the stream wrapper.
...@@ -49,16 +42,13 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface ...@@ -49,16 +42,13 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
/** /**
* Constructs a stream locator. * Constructs a stream locator.
* *
* @param \Drupal\Core\File\FileSystemInterface $file_system_helper * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The file system helper. * The stream wrapper manager.
* @param string $app_root
* The app root.
* @param string $scheme * @param string $scheme
* The scheme of the stream wrapper. * The scheme of the stream wrapper.
*/ */
public function __construct(FileSystemInterface $file_system_helper, $app_root, $scheme) { public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, $scheme) {
$this->fileSystemHelper = $file_system_helper; $this->streamWrapperManager = $stream_wrapper_manager;
$this->appRoot = (string) $app_root;
$this->scheme = (string) $scheme; $this->scheme = (string) $scheme;
} }
...@@ -69,7 +59,7 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface ...@@ -69,7 +59,7 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
if (!isset($configuration['scheme'])) { if (!isset($configuration['scheme'])) {
throw new MissingPluginConfigurationException($plugin_id, $plugin_definition, $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 ...@@ -81,14 +71,15 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
* @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate() * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate()
*/ */
public function locate(LocalLibraryInterface $library) { public function locate(LocalLibraryInterface $library) {
$path = $this->fileSystemHelper->realpath($this->getUri($library)); /** @var \Drupal\Core\StreamWrapper\LocalStream $stream_wrapper */
$stream_wrapper = $this->streamWrapperManager->getViaScheme($this->scheme);
if ($path && is_dir($path) && is_readable($path)) { assert('$stream_wrapper instanceof \Drupal\Core\StreamWrapper\LocalStream');
// Set a path relative to the app root. // Calling LocalStream::getDirectoryPath() explicitly avoids the realpath()
assert('strpos($path, $this->appRoot . "/") === 0', "Path: $path"); // usage in LocalStream::getLocalPath(), which breaks if Libraries API is
$path = str_replace($this->appRoot . '/', '', $path); // symbolically linked into the Drupal installation.
assert('$path[0] !== "/"'); $path = $stream_wrapper->getDirectoryPath() . '/' . $library->getId();
if (is_dir($path) && is_readable($path)) {
$library->setLocalPath($path); $library->setLocalPath($path);
} }
else { else {
...@@ -96,17 +87,4 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface ...@@ -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();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment