-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
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.