Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / Plugin / libraries / Locator / UriLocator.php
1 <?php
2
3 namespace Drupal\libraries\Plugin\libraries\Locator;
4
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;
11
12 /**
13  * Provides a locator utilizing a URI.
14  *
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).
21  *
22  * @Locator("uri")
23  *
24  * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
25  */
26 class UriLocator implements LocatorInterface, ContainerFactoryPluginInterface {
27
28   /**
29    * The stream wrapper manager.
30    *
31    * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
32    */
33   protected $streamWrapperManager;
34
35   /**
36    * The URI to check.
37    *
38    * @var string
39    */
40   protected $uri;
41
42   /**
43    * Constructs a URI locator.
44    *
45    * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
46    *   The stream wrapper manager.
47    * @param string $uri
48    *   The URI to check.
49    */
50   public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, $uri) {
51     $this->streamWrapperManager = $stream_wrapper_manager;
52     $this->uri = (string) $uri;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
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');
61     }
62     return new static($container->get('stream_wrapper_manager'), $configuration['uri']);
63   }
64
65   /**
66    * Locates a library.
67    *
68    * @param \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $library
69    *   The library to locate.
70    *
71    * @see \Drupal\libraries\ExternalLibrary\Local\LocatorInterface::locate()
72    */
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);
84       return;
85     }
86     $library->setUninstalled();
87   }
88
89 }