Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / Plugin / libraries / VersionDetector / LinePatternDetector.php
1 <?php
2
3 namespace Drupal\libraries\Plugin\libraries\VersionDetector;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
7 use Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException;
8 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
9 use Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface;
10 use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Detects the version by matching lines in a file against a specified pattern.
15  *
16  * This version detector can be used if the library version is denoted in a
17  * particular format in a changelog or readme file, for example.
18  *
19  * @VersionDetector("line_pattern")
20  *
21  * @ingroup libraries
22  */
23 class LinePatternDetector extends PluginBase implements VersionDetectorInterface, ContainerFactoryPluginInterface {
24
25   /**
26    * The app root.
27    *
28    * @var string
29    */
30   protected $appRoot;
31
32   /**
33    * Constructs a line pattern version detector.
34    *
35    * @param array $configuration
36    * @param string $plugin_id
37    * @param array $plugin_definition
38    * @param string $app_root
39    */
40   public function __construct(array $configuration, $plugin_id, array $plugin_definition, $app_root) {
41     $configuration += [
42       'file' => '',
43       'pattern' => '',
44       'lines' => 20,
45       'columns' => 200,
46     ];
47     parent::__construct($configuration, $plugin_id, $plugin_definition);
48     $this->appRoot = $app_root;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55     return new static(
56       $configuration,
57       $plugin_id,
58       $plugin_definition,
59       $container->get('app.root')
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function detectVersion(VersionedLibraryInterface $library) {
67     if (!($library instanceof LocalLibraryInterface)) {
68       throw new UnknownLibraryVersionException($library);
69     }
70
71     $filepath = $this->appRoot . '/' . $library->getLocalPath() . '/' . $this->configuration['file'];
72     if (!file_exists($filepath)) {
73       throw new UnknownLibraryVersionException($library);
74     }
75
76     $file = fopen($filepath, 'r');
77     $lines = $this->configuration['lines'];
78     while ($lines && $line = fgets($file, $this->configuration['columns'])) {
79       if (preg_match($this->configuration['pattern'], $line, $version)) {
80         fclose($file);
81         $library->setVersion($version[1]);
82         return;
83       }
84       $lines--;
85     }
86     fclose($file);
87   }
88
89 }