function PathRepository::initialize
Initializes path repository.
This method will basically read the folder and add the found package.
Overrides ArrayRepository::initialize
File
-
vendor/
composer/ composer/ src/ Composer/ Repository/ PathRepository.php, line 145
Class
- PathRepository
- This repository allows installing local packages that are not necessarily under their own VCS.
Namespace
Composer\RepositoryCode
protected function initialize() : void {
parent::initialize();
$urlMatches = $this->getUrlMatches();
if (empty($urlMatches)) {
if (Preg::isMatch('{[*{}]}', $this->url)) {
$url = $this->url;
while (Preg::isMatch('{[*{}]}', $url)) {
$url = dirname($url);
}
// the parent directory before any wildcard exists, so we assume it is correctly configured but simply empty
if (is_dir($url)) {
return;
}
}
throw new \RuntimeException('The `url` supplied for the path (' . $this->url . ') repository does not exist');
}
foreach ($urlMatches as $url) {
$path = realpath($url) . DIRECTORY_SEPARATOR;
$composerFilePath = $path . 'composer.json';
if (!file_exists($composerFilePath)) {
continue;
}
$json = file_get_contents($composerFilePath);
$package = JsonFile::parseJson($json, $composerFilePath);
$package['dist'] = [
'type' => 'path',
'url' => $url,
];
$reference = $this->options['reference'] ?? 'auto';
if ('none' === $reference) {
$package['dist']['reference'] = null;
}
elseif ('config' === $reference || 'auto' === $reference) {
$package['dist']['reference'] = hash('sha1', $json . serialize($this->options));
}
// copy symlink/relative options to transport options
$package['transport-options'] = array_intersect_key($this->options, [
'symlink' => true,
'relative' => true,
]);
// use the version provided as option if available
if (isset($package['name'], $this->options['versions'][$package['name']])) {
$package['version'] = $this->options['versions'][$package['name']];
}
// carry over the root package version if this path repo is in the same git repository as root package
if (!isset($package['version']) && ($rootVersion = Platform::getEnv('COMPOSER_ROOT_VERSION'))) {
if (0 === $this->process
->execute([
'git',
'rev-parse',
'HEAD',
], $ref1, $path) && 0 === $this->process
->execute([
'git',
'rev-parse',
'HEAD',
], $ref2) && $ref1 === $ref2) {
$package['version'] = $this->versionGuesser
->getRootVersionFromEnv();
}
}
$output = '';
if ('auto' === $reference && is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process
->execute(array_merge([
'git',
'log',
'-n1',
'--pretty=%H',
], GitUtil::getNoShowSignatureFlags($this->process)), $output, $path)) {
$package['dist']['reference'] = trim($output);
}
if (!isset($package['version'])) {
$versionData = $this->versionGuesser
->guessVersion($package, $path);
if (is_array($versionData) && $versionData['pretty_version']) {
// if there is a feature branch detected, we add a second packages with the feature branch version
if (!empty($versionData['feature_pretty_version'])) {
$package['version'] = $versionData['feature_pretty_version'];
$this->addPackage($this->loader
->load($package));
}
$package['version'] = $versionData['pretty_version'];
}
else {
$package['version'] = 'dev-main';
}
}
try {
$this->addPackage($this->loader
->load($package));
} catch (\Exception $e) {
throw new \RuntimeException('Failed loading the package in ' . $composerFilePath, 0, $e);
}
}
}