Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to disable every configurator #908

Draft
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/build/
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
composer.lock
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"Symfony\\Flex\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Symfony\\Flex\\Tests\\": "tests"
}
},
"extra": {
"class": "Symfony\\Flex\\Flex"
}
Expand Down
99 changes: 98 additions & 1 deletion src/Configurator/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Symfony\Flex\Configurator;

use Composer\Composer;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Symfony\Flex\Lock;
use Symfony\Flex\Options;
use Symfony\Flex\Path;
Expand All @@ -29,6 +32,8 @@ abstract class AbstractConfigurator
protected $options;
protected $path;

protected ?bool $shouldConfigure = null;

public function __construct(Composer $composer, IOInterface $io, Options $options)
{
$this->composer = $composer;
Expand All @@ -37,12 +42,104 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
$this->path = new Path($options->get('root-dir'));
}

abstract public function configure(Recipe $recipe, $config, Lock $lock, array $options = []);
/**
* @return bool True if configured
*/
abstract public function configure(Recipe $recipe, $config, Lock $lock, array $options = []): bool;

abstract public function unconfigure(Recipe $recipe, $config, Lock $lock);

abstract public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void;

abstract public function configureKey(): string;

public function isEnabledByDefault(): bool
{
return true;
}

public function shouldConfigure(Composer $composer, IOInterface $io, Recipe $recipe): bool
{
if (null !== $this->shouldConfigure) {
return $this->shouldConfigure;
}

if (null !== $preference = $composer->getPackage()->getExtra()['symfony'][$this->configureKey()] ?? null) {
return $this->shouldConfigure = $preference;
}

if ($this->isEnabledByDefault()) {
return true;
}

if ('install' !== $recipe->getJob()) {
return false;
}

$answer = $this->askSupport($io, $recipe);
if ('n' === $answer) {
return $this->shouldConfigure = false;
}

if ('y' === $answer) {
return $this->shouldConfigure = true;
}

$this->shouldConfigure = 'p' === $answer;

$this->persistPermanentChoice();

return $this->shouldConfigure;
}

protected function askSupport(IOInterface $io, Recipe $recipe): string
{
$io->writeError(sprintf(
' - <warning> %s </> %s',
$io->isInteractive() ? 'WARNING' : 'IGNORING',
$recipe->getFormattedOrigin()
));

return $io->askAndValidate(
$this->supportQuestion(),
function ($value) {
if (null === $value) {
return 'y';
}
$value = strtolower($value[0]);
if (!\in_array($value, ['y', 'n', 'p', 'x'], true)) {
throw new \InvalidArgumentException('Invalid choice.');
}

return $value;
},
null,
'y'
);
}

protected function supportQuestion(): string
{
$configuratorClass = substr(static::class, strrpos(static::class, '\\') + 1);

return ' The recipe for this package would like to run '.$configuratorClass.'.

Do you want to include this configuration from recipes?
[<comment>y</>] Yes
[<comment>n</>] No
[<comment>p</>] Yes permanently, never ask again for this project
[<comment>x</>] No permanently, never ask again for this project
(defaults to <comment>y</>): ';
}

protected function persistPermanentChoice(): void
{
$json = new JsonFile(Factory::getComposerFile());
$manipulator = new JsonManipulator(file_get_contents($json->getPath()));
$manipulator->addSubNode('extra', sprintf('symfony.%s', $this->configureKey()), $this->shouldConfigure);
file_put_contents($json->getPath(), $manipulator->getContents());
}

protected function write($messages)
{
if (!\is_array($messages)) {
Expand Down
17 changes: 16 additions & 1 deletion src/Configurator/BundlesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
*/
class BundlesConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $bundles, Lock $lock, array $options = [])
public function configure(Recipe $recipe, $bundles, Lock $lock, array $options = []): bool
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipe)) {
return false;
}

$this->write('Enabling the package as a Symfony bundle');
$registered = $this->configureBundles($bundles);
$this->dump($this->getConfFile(), $registered);

return true;
}

public function unconfigure(Recipe $recipe, $bundles, Lock $lock)
Expand All @@ -44,6 +50,10 @@ public function unconfigure(Recipe $recipe, $bundles, Lock $lock)

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipeUpdate->getNewRecipe())) {
return;
}

$originalBundles = $this->configureBundles($originalConfig, true);
$recipeUpdate->setOriginalFile(
$this->getLocalConfFile(),
Expand All @@ -57,6 +67,11 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
);
}

public function configureKey(): string
{
return 'bundles';
}

private function configureBundles(array $bundles, bool $resetEnvironments = false): array
{
$file = $this->getConfFile();
Expand Down
18 changes: 16 additions & 2 deletions src/Configurator/ComposerScriptsConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
*/
class ComposerScriptsConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $scripts, Lock $lock, array $options = [])
public function configure(Recipe $recipe, $scripts, Lock $lock, array $options = []): bool
{
$json = new JsonFile(Factory::getComposerFile());
if (!$this->shouldConfigure($this->composer, $this->io, $recipe)) {
return false;
}

$json = new JsonFile(Factory::getComposerFile());
file_put_contents($json->getPath(), $this->configureScripts($scripts, $json));

return true;
}

public function unconfigure(Recipe $recipe, $scripts, Lock $lock)
Expand All @@ -48,6 +53,10 @@ public function unconfigure(Recipe $recipe, $scripts, Lock $lock)

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipeUpdate->getNewRecipe())) {
return;
}

$json = new JsonFile(Factory::getComposerFile());
$jsonPath = ltrim(str_replace($recipeUpdate->getRootDir(), '', $json->getPath()), '/\\');

Expand All @@ -61,6 +70,11 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
);
}

public function configureKey(): string
{
return 'composer-scripts';
}

private function configureScripts(array $scripts, JsonFile $json): string
{
$jsonContents = $json->read();
Expand Down
25 changes: 20 additions & 5 deletions src/Configurator/ContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
*/
class ContainerConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $parameters, Lock $lock, array $options = [])
public function configure(Recipe $recipe, $parameters, Lock $lock, array $options = []): bool
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipe)) {
return false;
}

$this->write('Setting parameters');
$contents = $this->configureParameters($parameters);

if (null !== $contents) {
file_put_contents($this->options->get('root-dir').'/'.$this->getServicesPath(), $contents);
}
file_put_contents(
$this->options->get('root-dir').'/'.$this->getServicesPath(),
$this->configureParameters($parameters)
);

return true;
}

public function unconfigure(Recipe $recipe, $parameters, Lock $lock)
Expand All @@ -46,6 +52,10 @@ public function unconfigure(Recipe $recipe, $parameters, Lock $lock)

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipeUpdate->getNewRecipe())) {
return;
}

if ($originalConfig) {
$recipeUpdate->setOriginalFile(
$this->getServicesPath(),
Expand All @@ -61,6 +71,11 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
}
}

public function configureKey(): string
{
return 'container';
}

private function configureParameters(array $parameters, bool $update = false): string
{
$target = $this->options->get('root-dir').'/'.$this->getServicesPath();
Expand Down
17 changes: 16 additions & 1 deletion src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
*/
class CopyFromPackageConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $config, Lock $lock, array $options = [])
public function configure(Recipe $recipe, $config, Lock $lock, array $options = []): bool
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipe)) {
return false;
}

$this->write('Copying files from package');
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
$options = array_merge($this->options->toArray(), $options);
Expand All @@ -30,6 +34,8 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
foreach ($files as $source => $target) {
$this->copyFile($source, $target, $options);
}

return true;
}

public function unconfigure(Recipe $recipe, $config, Lock $lock)
Expand All @@ -41,6 +47,10 @@ public function unconfigure(Recipe $recipe, $config, Lock $lock)

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipeUpdate->getNewRecipe())) {
return;
}

$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipeUpdate->getNewRecipe()->getPackage());
foreach ($originalConfig as $source => $target) {
if (isset($newConfig[$source])) {
Expand All @@ -66,6 +76,11 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
}
}

public function configureKey(): string
{
return 'copy-from-package';
}

private function getFilesToCopy(array $manifest, string $from): array
{
$files = [];
Expand Down
17 changes: 16 additions & 1 deletion src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
*/
class CopyFromRecipeConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $config, Lock $lock, array $options = [])
public function configure(Recipe $recipe, $config, Lock $lock, array $options = []): bool
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipe)) {
return false;
}

$this->write('Copying files from recipe');
$options = array_merge($this->options->toArray(), $options);

$lock->add($recipe->getName(), ['files' => $this->copyFiles($config, $recipe->getFiles(), $options)]);

return true;
}

public function unconfigure(Recipe $recipe, $config, Lock $lock)
Expand All @@ -36,6 +42,10 @@ public function unconfigure(Recipe $recipe, $config, Lock $lock)

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if (!$this->shouldConfigure($this->composer, $this->io, $recipeUpdate->getNewRecipe())) {
return;
}

foreach ($recipeUpdate->getOriginalRecipe()->getFiles() as $filename => $data) {
$recipeUpdate->setOriginalFile($filename, $data['contents']);
}
Expand All @@ -49,6 +59,11 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
$recipeUpdate->getLock()->add($recipeUpdate->getPackageName(), ['files' => $files]);
}

public function configureKey(): string
{
return 'copy-from-recipe';
}

private function getRemovableFilesFromRecipeAndLock(Recipe $recipe, Lock $lock): array
{
$lockedFiles = array_unique(
Expand Down
Loading