You are here

public function LinePatternDetector::detectVersion in Libraries API 8.3

Detects the version of a library.

@todo Provide a mechanism for version detectors to provide a reason for failing.

Parameters

\Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface $library: The library whose version to detect.

Throws

\Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException

Overrides VersionDetectorInterface::detectVersion

File

src/Plugin/libraries/VersionDetector/LinePatternDetector.php, line 66

Class

LinePatternDetector
Detects the version by matching lines in a file against a specified pattern.

Namespace

Drupal\libraries\Plugin\libraries\VersionDetector

Code

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);
}