1
0
Fork 0

Rename available-package-regexes to available-package-patterns and switch negated function around, refs #9197

pull/9200/head
Jordi Boggiano 2020-09-10 14:58:05 +02:00
parent 8d985634a8
commit 2a82371adf
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 29 additions and 30 deletions

View File

@ -68,7 +68,7 @@ This new metadata-url should serve all packages which are in the repository.
- The `foo/bar.json` and `foo/bar~dev.json` files containing package versions MUST contain only versions for the foo/bar package, as `{"packages":{"foo/bar":[ ... versions here ... ]}}`. - The `foo/bar.json` and `foo/bar~dev.json` files containing package versions MUST contain only versions for the foo/bar package, as `{"packages":{"foo/bar":[ ... versions here ... ]}}`.
- The array of versions can also optionally be minified using `Composer\Util\MetadataMinifier::minify()`. If you do that, you should add a `"minified": "composer/2.0"` key at the top level to indicate to Composer it must expand the version list back into the original data. See https://repo.packagist.org/p2/monolog/monolog.json for an example. - The array of versions can also optionally be minified using `Composer\Util\MetadataMinifier::minify()`. If you do that, you should add a `"minified": "composer/2.0"` key at the top level to indicate to Composer it must expand the version list back into the original data. See https://repo.packagist.org/p2/monolog/monolog.json for an example.
If your repository only has a small number of packages, and you want to avoid the 404-requests, you can also specify an `"available-packages"` key in `packages.json` which should be an array with all the package names that your repository contain. If your repository only has a small number of packages, and you want to avoid the 404-requests, you can also specify an `"available-packages"` key in `packages.json` which should be an array with all the package names that your repository contain. Alternatively you can specify an `"available-package-patterns"` key which is an array of package name patterns (with `*` matching any string, e.g. `vendor/*` would make composer look up every matching package name in this repository).
### providers-api ### providers-api

View File

@ -57,9 +57,9 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
protected $providersUrl; protected $providersUrl;
protected $listUrl; protected $listUrl;
/** @var bool Indicates whether a comprehensive list of packages this repository might provide is expressed in the repository root. **/ /** @var bool Indicates whether a comprehensive list of packages this repository might provide is expressed in the repository root. **/
protected $hasPredefinedPackageCoverage = false; protected $hasAvailablePackageList = false;
protected $availablePackages; protected $availablePackages;
protected $availablePackageRegexes; protected $availablePackagePatterns;
protected $lazyProvidersUrl; protected $lazyProvidersUrl;
protected $providerListing; protected $providerListing;
protected $loader; protected $loader;
@ -166,7 +166,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
return $this->filterPackages($this->whatProvides($name), $constraint, true); return $this->filterPackages($this->whatProvides($name), $constraint, true);
} }
if ($this->lazyProvidersRepoDoesNotContain($name)) { if ($this->hasAvailablePackageList && !$this->lazyProvidersRepoContains($name)) {
return; return;
} }
@ -206,7 +206,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
return $this->filterPackages($this->whatProvides($name), $constraint); return $this->filterPackages($this->whatProvides($name), $constraint);
} }
if ($this->lazyProvidersRepoDoesNotContain($name)) { if ($this->hasAvailablePackageList && !$this->lazyProvidersRepoContains($name)) {
return array(); return array();
} }
@ -264,7 +264,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
$hasProviders = $this->hasProviders(); $hasProviders = $this->hasProviders();
if ($this->lazyProvidersUrl) { if ($this->lazyProvidersUrl) {
if (is_array($this->availablePackages) && count($this->availablePackageRegexes) == 0) { if (is_array($this->availablePackages) && !$this->availablePackagePatterns) {
$packageMap = array(); $packageMap = array();
foreach ($this->availablePackages as $name) { foreach ($this->availablePackages as $name) {
$packageMap[$name] = new MatchAllConstraint(); $packageMap[$name] = new MatchAllConstraint();
@ -392,9 +392,9 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
} }
if ($this->lazyProvidersUrl && count($packageNameMap)) { if ($this->lazyProvidersUrl && count($packageNameMap)) {
if ($this->hasPredefinedPackageCoverage) { if ($this->hasAvailablePackageList) {
foreach ($packageNameMap as $name => $constraint) { foreach ($packageNameMap as $name => $constraint) {
if ($this->lazyProvidersRepoDoesNotContain(strtolower($name))) { if (!$this->lazyProvidersRepoContains(strtolower($name))) {
unset($packageNameMap[$name]); unset($packageNameMap[$name]);
} }
} }
@ -879,15 +879,17 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
if (!empty($data['available-packages'])) { if (!empty($data['available-packages'])) {
$availPackages = array_map('strtolower', $data['available-packages']); $availPackages = array_map('strtolower', $data['available-packages']);
$this->availablePackages = array_combine($availPackages, $availPackages); $this->availablePackages = array_combine($availPackages, $availPackages);
$this->hasPredefinedPackageCoverage = true; $this->hasAvailablePackageList = true;
} }
// Provides a list of package name regexes that are available in this repo // Provides a list of package name patterns (using * wildcards to match any substring, e.g. "vendor/*") that are available in this repo
// Disables lazy-provider behavior as with available-packages, but may allow much more compact expression of packages covered by this repository. // Disables lazy-provider behavior as with available-packages, but may allow much more compact expression of packages covered by this repository.
// Over-specifying covered packages is safe, but may result in increased traffic to your repository. // Over-specifying covered packages is safe, but may result in increased traffic to your repository.
if (!empty($data['available-package-regexes'])) { if (!empty($data['available-package-patterns'])) {
$this->availablePackageRegexes = $data['available-package-regexes']; $this->availablePackagePatterns = array_map(function ($pattern) {
$this->hasPredefinedPackageCoverage = true; return BasePackage::packageNameToRegexp($pattern);
}, $data['available-package-patterns']);
$this->hasAvailablePackageList = true;
} }
// Remove legacy keys as most repos need to be compatible with Composer v1 // Remove legacy keys as most repos need to be compatible with Composer v1
@ -1320,34 +1322,31 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
} }
/** /**
* Returns true only if: * Checks if the package name is present in this lazy providers repo
* - this repository uses lazy providers
* - this repository has predefined package coverage
* - the name passed is not among the known provider names
* *
* @param $name * @param string $name
* @return bool * @return bool true if the package name is present in availablePackages or matched by availablePackagePatterns
*/ */
protected function lazyProvidersRepoDoesNotContain($name) { protected function lazyProvidersRepoContains($name)
if (! $this->hasPredefinedPackageCoverage) { {
return false; if (!$this->hasAvailablePackageList) {
throw new \LogicException('lazyProvidersRepoContains should not be called unless hasAvailablePackageList is true');
} }
$ruledOutByExactMatch = $ruledOutByRegex = true; $ruledOutByExactMatch = $ruledOutByRegex = true;
if (is_array($this->availablePackages)) { if (is_array($this->availablePackages) && isset($this->availablePackages[$name])) {
$ruledOutByExactMatch = !isset($this->availablePackages[$name]); return true;
} }
if (is_array($this->availablePackageRegexes)) { if (is_array($this->availablePackagePatterns)) {
foreach ($this->availablePackageRegexes as $providerRegex) { foreach ($this->availablePackagePatterns as $providerRegex) {
if (preg_match(BasePackage::packageNameToRegexp($providerRegex), $name) == 1) { if (preg_match($providerRegex, $name)) {
$ruledOutByRegex = false; return true;
break;
} }
} }
} }
return $ruledOutByExactMatch && $ruledOutByRegex; return false;
} }
} }