3 namespace Drupal\libraries\Plugin\libraries\Locator;
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
7 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
8 use Drupal\libraries\ExternalLibrary\Local\LocatorInterface;
9 use Drupal\libraries\Plugin\MissingPluginConfigurationException;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a locator utilizing a URI.
15 * It makes the following assumptions:
16 * - The library files can be accessed using a specified stream.
17 * - The stream wrapper is local (i.e. it is a subclass of
18 * \Drupal\Core\StreamWrapper\LocalStream).
19 * - The first component of the file URIs are the library IDs (i.e. file URIs
20 * are of the form: scheme://library-id/path/to/file/filename).
24 * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
26 class UriLocator implements LocatorInterface, ContainerFactoryPluginInterface {
29 * The stream wrapper manager.
31 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
33 protected $streamWrapperManager;
43 * Constructs a URI locator.
45 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
46 * The stream wrapper manager.
50 public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, $uri) {
51 $this->streamWrapperManager = $stream_wrapper_manager;
52 $this->uri = (string) $uri;
58 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59 if (!isset($configuration['uri'])) {
60 throw new MissingPluginConfigurationException($plugin_id, $plugin_definition, $configuration, 'uri');
62 return new static($container->get('stream_wrapper_manager'), $configuration['uri']);
68 * @param \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $library
69 * The library to locate.
71 * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate()
73 public function locate(LocalLibraryInterface $library) {
74 /** @var \Drupal\Core\StreamWrapper\LocalStream $stream_wrapper */
75 $stream_wrapper = $this->streamWrapperManager->getViaUri($this->uri);
76 assert('$stream_wrapper instanceof \Drupal\Core\StreamWrapper\LocalStream');
77 // Calling LocalStream::getDirectoryPath() explicitly avoids the realpath()
78 // usage in LocalStream::getLocalPath(), which breaks if Libraries API is
79 // symbolically linked into the Drupal installation.
80 list($scheme, $target) = explode('://', $this->uri, 2);
81 $base_path = str_replace('//', '/', $stream_wrapper->getDirectoryPath() . '/' . $target . '/' . $library->getId());
82 if (is_dir($base_path) && is_readable($base_path)) {
83 $library->setLocalPath($base_path);
86 $library->setUninstalled();