-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recipe provider which loads recipes directly from the bundle.
- Loading branch information
Matthias Moser
committed
Sep 26, 2022
1 parent
0763da1
commit de737ac
Showing
8 changed files
with
326 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Symfony\Flex; | ||
|
||
class CompositeRecipeProvider implements RecipeProviderInterface | ||
{ | ||
/** | ||
* @var RecipeProviderInterface[] | ||
*/ | ||
private array $recipeProviders; | ||
|
||
/** | ||
* @param RecipeProviderInterface[] $recipeProviders | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(array $recipeProviders) | ||
{ | ||
$this->recipeProviders = array_reduce( | ||
$recipeProviders, | ||
function (array $providers, RecipeProviderInterface $provider) { | ||
if (self::class == $provider::class) { | ||
throw new \InvalidArgumentException('You cannot add an instance of this provider to itself.'); | ||
} | ||
$providers[$provider::class] = $provider; | ||
|
||
return $providers; | ||
}, | ||
[]); | ||
} | ||
|
||
/** | ||
* This method adds an instance RecipeProviderInterface to this provider. | ||
* You can only have one instance per class registered in this provider. | ||
* | ||
* @return $this | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function add(RecipeProviderInterface $recipeProvider): self | ||
{ | ||
if (self::class == $recipeProvider::class) { | ||
throw new \InvalidArgumentException('You cannot add an instance of this provider to itself.'); | ||
} | ||
if (isset($this->recipeProviders[$recipeProvider::class])) { | ||
throw new \InvalidArgumentException('Given Provider has been added already.'); | ||
} | ||
$this->recipeProviders[] = $recipeProvider; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return array_reduce($this->recipeProviders, function (bool $isEnabled, RecipeProviderInterface $provider) { return $provider->isEnabled() && $isEnabled; }, true); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function disable(): void | ||
{ | ||
array_walk($this->recipeProviders, function (RecipeProviderInterface $provider) { $provider->disable(); }); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getVersions(): array | ||
{ | ||
return array_reduce($this->recipeProviders, function (array $carry, RecipeProviderInterface $provider) { return array_merge($carry, $provider->getVersions()); }, []); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getAliases(): array | ||
{ | ||
return array_reduce($this->recipeProviders, function (array $carry, RecipeProviderInterface $provider) { return array_merge($carry, $provider->getAliases()); }, []); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getRecipes(array $operations): array | ||
{ | ||
return array_reduce($this->recipeProviders, function (array $carry, RecipeProviderInterface $provider) use ($operations) { return array_merge_recursive($carry, $provider->getRecipes($operations)); }, []); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function removeRecipeFromIndex(string $packageName, string $version): void | ||
{ | ||
array_walk($this->recipeProviders, function (RecipeProviderInterface $provider) use ($packageName, $version) { $provider->removeRecipeFromIndex($packageName, $version); }); | ||
} | ||
|
||
public function getSessionId(): string | ||
{ | ||
return implode(' ', array_reduce( | ||
$this->recipeProviders, | ||
function (array $carry, RecipeProviderInterface $provider) { | ||
$carry[] = $provider::class.'=>'.$provider->getSessionId(); | ||
|
||
return $carry; | ||
}, | ||
[])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
|
||
use Composer\Cache; | ||
use Composer\Composer; | ||
use Composer\DependencyResolver\Operation\OperationInterface; | ||
use Composer\DependencyResolver\Operation\UninstallOperation; | ||
use Composer\DependencyResolver\Operation\UpdateOperation; | ||
use Composer\IO\IOInterface; | ||
|
@@ -26,7 +25,7 @@ | |
* @author Fabien Potencier <[email protected]> | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
class Downloader | ||
class Downloader implements RecipeProviderInterface | ||
{ | ||
private const DEFAULT_ENDPOINTS = [ | ||
'https://raw.githubusercontent.com/symfony/recipes/flex/main/index.json', | ||
|
@@ -95,39 +94,52 @@ public function __construct(Composer $composer, IoInterface $io, HttpDownloader | |
$this->composer = $composer; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSessionId(): string | ||
{ | ||
return $this->sess; | ||
} | ||
|
||
public function isEnabled() | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function disable() | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function disable(): void | ||
{ | ||
$this->enabled = false; | ||
} | ||
|
||
public function getVersions() | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getVersions(): array | ||
{ | ||
$this->initialize(); | ||
|
||
return self::$versions ?? self::$versions = current($this->get([$this->legacyEndpoint.'/versions.json'])); | ||
} | ||
|
||
public function getAliases() | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getAliases(): array | ||
{ | ||
$this->initialize(); | ||
|
||
return self::$aliases ?? self::$aliases = current($this->get([$this->legacyEndpoint.'/aliases.json'])); | ||
} | ||
|
||
/** | ||
* Downloads recipes. | ||
* | ||
* @param OperationInterface[] $operations | ||
* {@inheritDoc} | ||
*/ | ||
public function getRecipes(array $operations): array | ||
{ | ||
|
@@ -307,11 +319,9 @@ public function getRecipes(array $operations): array | |
} | ||
|
||
/** | ||
* Used to "hide" a recipe version so that the next most-recent will be returned. | ||
* | ||
* This is used when resolving "conflicts". | ||
* {@inheritDoc} | ||
*/ | ||
public function removeRecipeFromIndex(string $packageName, string $version) | ||
public function removeRecipeFromIndex(string $packageName, string $version): void | ||
{ | ||
unset($this->index[$packageName][$version]); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.