LinePatternDetector.php in Libraries API 8.3
File
src/Plugin/libraries/VersionDetector/LinePatternDetector.php
View source
<?php
namespace Drupal\libraries\Plugin\libraries\VersionDetector;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException;
use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
use Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface;
use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LinePatternDetector extends PluginBase implements VersionDetectorInterface, ContainerFactoryPluginInterface {
protected $appRoot;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, $app_root) {
$configuration += [
'file' => '',
'pattern' => '',
'lines' => 20,
'columns' => 200,
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->appRoot = $app_root;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('app.root'));
}
public function detectVersion(VersionedLibraryInterface $library) {
if (!$library instanceof LocalLibraryInterface) {
throw new UnknownLibraryVersionException($library);
}
$filepath = $this->appRoot . '/' . $library
->getLocalPath() . '/' . $this->configuration['file'];
if (!file_exists($filepath)) {
throw new UnknownLibraryVersionException($library);
}
$file = fopen($filepath, 'r');
$lines = $this->configuration['lines'];
while ($lines && ($line = fgets($file, $this->configuration['columns']))) {
if (preg_match($this->configuration['pattern'], $line, $version)) {
fclose($file);
$library
->setVersion($version[1]);
return;
}
$lines--;
}
fclose($file);
}
}