Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ArtifactRepository.php

class ArtifactRepository

@author Serge Smertin <serg.smertin@gmail.com>

Hierarchy

  • class \Composer\Repository\ArrayRepository implements \Composer\Repository\RepositoryInterface
    • class \Composer\Repository\ArtifactRepository extends \Composer\Repository\ArrayRepository implements \Composer\Repository\ConfigurableRepositoryInterface

Expanded class hierarchy of ArtifactRepository

File

vendor/composer/composer/src/Composer/Repository/ArtifactRepository.php, line 27

Namespace

Composer\Repository
View source
class ArtifactRepository extends ArrayRepository implements ConfigurableRepositoryInterface {
    
    /** @var LoaderInterface */
    protected $loader;
    
    /** @var string */
    protected $lookup;
    
    /** @var array{url: string} */
    protected $repoConfig;
    
    /** @var IOInterface */
    private $io;
    
    /**
     * @param array{url: string} $repoConfig
     */
    public function __construct(array $repoConfig, IOInterface $io) {
        parent::__construct();
        if (!extension_loaded('zip')) {
            throw new \RuntimeException('The artifact repository requires PHP\'s zip extension');
        }
        $this->loader = new ArrayLoader();
        $this->lookup = Platform::expandPath($repoConfig['url']);
        $this->io = $io;
        $this->repoConfig = $repoConfig;
    }
    public function getRepoName() {
        return 'artifact repo (' . $this->lookup . ')';
    }
    public function getRepoConfig() {
        return $this->repoConfig;
    }
    protected function initialize() {
        parent::initialize();
        $this->scanDirectory($this->lookup);
    }
    private function scanDirectory(string $path) : void {
        $io = $this->io;
        $directory = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
        $iterator = new \RecursiveIteratorIterator($directory);
        $regex = new \RegexIterator($iterator, '/^.+\\.(zip|tar|gz|tgz)$/i');
        foreach ($regex as $file) {
            
            /* @var $file \SplFileInfo */
            if (!$file->isFile()) {
                continue;
            }
            $package = $this->getComposerInformation($file);
            if (!$package) {
                $io->writeError("File <comment>{$file->getBasename()}</comment> doesn't seem to hold a package", true, IOInterface::VERBOSE);
                continue;
            }
            $template = 'Found package <info>%s</info> (<comment>%s</comment>) in file <info>%s</info>';
            $io->writeError(sprintf($template, $package->getName(), $package->getPrettyVersion(), $file->getBasename()), true, IOInterface::VERBOSE);
            $this->addPackage($package);
        }
    }
    
    /**
     * @return ?BasePackage
     */
    private function getComposerInformation(\SplFileInfo $file) : ?BasePackage {
        $json = null;
        $fileType = null;
        $fileExtension = pathinfo($file->getPathname(), PATHINFO_EXTENSION);
        if (in_array($fileExtension, [
            'gz',
            'tar',
            'tgz',
        ], true)) {
            $fileType = 'tar';
        }
        elseif ($fileExtension === 'zip') {
            $fileType = 'zip';
        }
        else {
            throw new \RuntimeException('Files with "' . $fileExtension . '" extensions aren\'t supported. Only ZIP and TAR/TAR.GZ/TGZ archives are supported.');
        }
        try {
            if ($fileType === 'tar') {
                $json = Tar::getComposerJson($file->getPathname());
            }
            else {
                $json = Zip::getComposerJson($file->getPathname());
            }
        } catch (\Exception $exception) {
            $this->io
                ->write('Failed loading package ' . $file->getPathname() . ': ' . $exception->getMessage(), false, IOInterface::VERBOSE);
        }
        if (null === $json) {
            return null;
        }
        $package = JsonFile::parseJson($json, $file->getPathname() . '#composer.json');
        $package['dist'] = [
            'type' => $fileType,
            'url' => strtr($file->getPathname(), '\\', '/'),
            'shasum' => hash_file('sha1', $file->getRealPath()),
        ];
        try {
            $package = $this->loader
                ->load($package);
        } catch (\UnexpectedValueException $e) {
            throw new \UnexpectedValueException('Failed loading package in ' . $file . ': ' . $e->getMessage(), 0, $e);
        }
        return $package;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ArrayRepository::$packageMap protected property
ArrayRepository::$packages protected property @var ?array&lt;BasePackage&gt;
ArrayRepository::addPackage public function Adds a new package to the repository 2
ArrayRepository::count public function Returns the number of packages in this repository
ArrayRepository::createAliasPackage protected function
ArrayRepository::findPackage public function @inheritDoc Overrides RepositoryInterface::findPackage 1
ArrayRepository::findPackages public function @inheritDoc Overrides RepositoryInterface::findPackages 1
ArrayRepository::getPackages public function @inheritDoc Overrides RepositoryInterface::getPackages 1
ArrayRepository::getProviders public function @inheritDoc Overrides RepositoryInterface::getProviders 1
ArrayRepository::hasPackage public function @inheritDoc Overrides RepositoryInterface::hasPackage
ArrayRepository::loadPackages public function @inheritDoc Overrides RepositoryInterface::loadPackages 1
ArrayRepository::removePackage public function Removes package from repository.
ArrayRepository::search public function @inheritDoc Overrides RepositoryInterface::search 2
ArtifactRepository::$io private property @var IOInterface
ArtifactRepository::$loader protected property @var LoaderInterface
ArtifactRepository::$lookup protected property @var string
ArtifactRepository::$repoConfig protected property @var array{url: string}
ArtifactRepository::getComposerInformation private function
ArtifactRepository::getRepoConfig public function Overrides ConfigurableRepositoryInterface::getRepoConfig
ArtifactRepository::getRepoName public function Returns a name representing this repository to the user Overrides ArrayRepository::getRepoName
ArtifactRepository::initialize protected function Initializes the packages array. Mostly meant as an extension point. Overrides ArrayRepository::initialize
ArtifactRepository::scanDirectory private function
ArtifactRepository::__construct public function Overrides ArrayRepository::__construct
RepositoryInterface::SEARCH_FULLTEXT public constant
RepositoryInterface::SEARCH_NAME public constant
RepositoryInterface::SEARCH_VENDOR public constant

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal