[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/ocramius/package-versions/src/PackageVersions/ -> Installer.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace PackageVersions;
   6  
   7  use Composer\Composer;
   8  use Composer\Config;
   9  use Composer\EventDispatcher\EventSubscriberInterface;
  10  use Composer\IO\IOInterface;
  11  use Composer\Package\AliasPackage;
  12  use Composer\Package\Locker;
  13  use Composer\Package\PackageInterface;
  14  use Composer\Package\RootPackageInterface;
  15  use Composer\Plugin\PluginInterface;
  16  use Composer\Script\Event;
  17  use Composer\Script\ScriptEvents;
  18  use Generator;
  19  use RuntimeException;
  20  use function array_key_exists;
  21  use function array_merge;
  22  use function chmod;
  23  use function dirname;
  24  use function file_exists;
  25  use function file_put_contents;
  26  use function iterator_to_array;
  27  use function rename;
  28  use function sprintf;
  29  use function uniqid;
  30  use function var_export;
  31  
  32  final class Installer implements PluginInterface, EventSubscriberInterface
  33  {
  34      /** @var string */
  35      private static $generatedClassTemplate = <<<'PHP'
  36  <?php
  37  
  38  declare(strict_types=1);
  39  
  40  namespace PackageVersions;
  41  
  42  /**
  43   * This class is generated by ocramius/package-versions, specifically by
  44   * @see \PackageVersions\Installer
  45   *
  46   * This file is overwritten at every run of `composer install` or `composer update`.
  47   */
  48  %s
  49  {
  50      public const ROOT_PACKAGE_NAME = '%s';
  51      public const VERSIONS          = %s;
  52  
  53      private function __construct()
  54      {
  55      }
  56  
  57      /**
  58       * @throws \OutOfBoundsException If a version cannot be located.
  59       */
  60      public static function getVersion(string $packageName) : string
  61      {
  62          if (isset(self::VERSIONS[$packageName])) {
  63              return self::VERSIONS[$packageName];
  64          }
  65  
  66          throw new \OutOfBoundsException(
  67              'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
  68          );
  69      }
  70  }
  71  
  72  PHP;
  73  
  74      /**
  75       * {@inheritDoc}
  76       */
  77      public function activate(Composer $composer, IOInterface $io) : void
  78      {
  79          // Nothing to do here, as all features are provided through event listeners
  80      }
  81  
  82      /**
  83       * {@inheritDoc}
  84       */
  85      public static function getSubscribedEvents() : array
  86      {
  87          return [
  88              ScriptEvents::POST_INSTALL_CMD => 'dumpVersionsClass',
  89              ScriptEvents::POST_UPDATE_CMD  => 'dumpVersionsClass',
  90          ];
  91      }
  92  
  93      /**
  94       * @throws RuntimeException
  95       */
  96      public static function dumpVersionsClass(Event $composerEvent) : void
  97      {
  98          $composer    = $composerEvent->getComposer();
  99          $rootPackage = $composer->getPackage();
 100          $versions    = iterator_to_array(self::getVersions($composer->getLocker(), $rootPackage));
 101  
 102          if (! array_key_exists('ocramius/package-versions', $versions)) {
 103              //plugin must be globally installed - we only want to generate versions for projects which specifically
 104              //require ocramius/package-versions
 105              return;
 106          }
 107  
 108          $versionClass = self::generateVersionsClass($rootPackage->getName(), $versions);
 109  
 110          self::writeVersionClassToFile($versionClass, $composer, $composerEvent->getIO());
 111      }
 112  
 113      /**
 114       * @param string[] $versions
 115       */
 116      private static function generateVersionsClass(string $rootPackageName, array $versions) : string
 117      {
 118          return sprintf(
 119              self::$generatedClassTemplate,
 120              'fin' . 'al ' . 'cla' . 'ss ' . 'Versions', // note: workaround for regex-based code parsers :-(
 121              $rootPackageName,
 122              var_export($versions, true)
 123          );
 124      }
 125  
 126      /**
 127       * @throws RuntimeException
 128       */
 129      private static function writeVersionClassToFile(string $versionClassSource, Composer $composer, IOInterface $io) : void
 130      {
 131          $installPath = self::locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage())
 132              . '/src/PackageVersions/Versions.php';
 133  
 134          if (! file_exists(dirname($installPath))) {
 135              $io->write('<info>ocramius/package-versions:</info> Package not found (probably scheduled for removal); generation of version class skipped.');
 136  
 137              return;
 138          }
 139  
 140          $io->write('<info>ocramius/package-versions:</info>  Generating version class...');
 141  
 142          $installPathTmp = $installPath . '_' . uniqid('tmp', true);
 143          file_put_contents($installPathTmp, $versionClassSource);
 144          chmod($installPathTmp, 0664);
 145          rename($installPathTmp, $installPath);
 146  
 147          $io->write('<info>ocramius/package-versions:</info> ...done generating version class');
 148      }
 149  
 150      /**
 151       * @throws RuntimeException
 152       */
 153      private static function locateRootPackageInstallPath(
 154          Config $composerConfig,
 155          RootPackageInterface $rootPackage
 156      ) : string {
 157          if (self::getRootPackageAlias($rootPackage)->getName() === 'ocramius/package-versions') {
 158              return dirname($composerConfig->get('vendor-dir'));
 159          }
 160  
 161          return $composerConfig->get('vendor-dir') . '/ocramius/package-versions';
 162      }
 163  
 164      private static function getRootPackageAlias(RootPackageInterface $rootPackage) : PackageInterface
 165      {
 166          $package = $rootPackage;
 167  
 168          while ($package instanceof AliasPackage) {
 169              $package = $package->getAliasOf();
 170          }
 171  
 172          return $package;
 173      }
 174  
 175      /**
 176       * @return Generator|string[]
 177       */
 178      private static function getVersions(Locker $locker, RootPackageInterface $rootPackage) : Generator
 179      {
 180          $lockData = $locker->getLockData();
 181  
 182          $lockData['packages-dev'] = $lockData['packages-dev'] ?? [];
 183  
 184          foreach (array_merge($lockData['packages'], $lockData['packages-dev']) as $package) {
 185              yield $package['name'] => $package['version'] . '@' . (
 186                  $package['source']['reference']?? $package['dist']['reference'] ?? ''
 187              );
 188          }
 189  
 190          foreach ($rootPackage->getReplaces() as $replace) {
 191              $version = $replace->getPrettyConstraint();
 192              if ($version === 'self.version') {
 193                  $version = $rootPackage->getPrettyVersion();
 194              }
 195  
 196              yield $replace->getTarget() => $version . '@' . $rootPackage->getSourceReference();
 197          }
 198  
 199          yield $rootPackage->getName() => $rootPackage->getPrettyVersion() . '@' . $rootPackage->getSourceReference();
 200      }
 201  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1