mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-20 00:58:48 +02:00
N°6934 - Symfony 6.4 - upgrade Symfony bundles to 6.4 (#580)
* Update Symfony lib to version ~6.4.0 * Update code missing return type * Add an iTop general configuration entry to store application secret (Symfony mandatory parameter) * Use dependency injection in ExceptionListener & UserProvider classes
This commit is contained in:
@@ -13,11 +13,14 @@ namespace Symfony\Bundle\FrameworkBundle\Command;
|
||||
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Completion\CompletionInput;
|
||||
use Symfony\Component\Console\Completion\CompletionSuggestions;
|
||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Console\Exception\LogicException;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
|
||||
@@ -33,23 +36,22 @@ use Symfony\Component\Yaml\Yaml;
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
#[AsCommand(name: 'debug:config', description: 'Dump the current configuration for an extension')]
|
||||
class ConfigDebugCommand extends AbstractConfigCommand
|
||||
{
|
||||
protected static $defaultName = 'debug:config';
|
||||
protected static $defaultDescription = 'Dump the current configuration for an extension';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$commentedHelpFormats = array_map(fn ($format) => sprintf('<comment>%s</comment>', $format), $this->getAvailableFormatOptions());
|
||||
$helpFormats = implode('", "', $commentedHelpFormats);
|
||||
|
||||
$this
|
||||
->setDefinition([
|
||||
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
|
||||
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
|
||||
new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Display resolved environment variable values instead of placeholders'),
|
||||
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'txt' : 'json'),
|
||||
])
|
||||
->setDescription(self::$defaultDescription)
|
||||
->setHelp(<<<'EOF'
|
||||
->setHelp(<<<EOF
|
||||
The <info>%command.name%</info> command dumps the current configuration for an
|
||||
extension/bundle.
|
||||
|
||||
@@ -58,6 +60,11 @@ Either the extension alias or bundle name can be used:
|
||||
<info>php %command.full_name% framework</info>
|
||||
<info>php %command.full_name% FrameworkBundle</info>
|
||||
|
||||
The <info>--format</info> option specifies the format of the configuration,
|
||||
these are "{$helpFormats}".
|
||||
|
||||
<info>php %command.full_name% framework --format=json</info>
|
||||
|
||||
For dumping a specific option, add its path as second argument:
|
||||
|
||||
<info>php %command.full_name% framework serializer.enabled</info>
|
||||
@@ -67,9 +74,6 @@ EOF
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
@@ -77,14 +81,7 @@ EOF
|
||||
|
||||
if (null === $name = $input->getArgument('name')) {
|
||||
$this->listBundles($errorIo);
|
||||
|
||||
$kernel = $this->getApplication()->getKernel();
|
||||
if ($kernel instanceof ExtensionInterface
|
||||
&& ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)
|
||||
&& $kernel->getAlias()
|
||||
) {
|
||||
$errorIo->table(['Kernel Extension'], [[$kernel->getAlias()]]);
|
||||
}
|
||||
$this->listNonBundleExtensions($errorIo);
|
||||
|
||||
$errorIo->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. <comment>debug:config FrameworkBundle</comment>)');
|
||||
$errorIo->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> configuration)');
|
||||
@@ -96,14 +93,24 @@ EOF
|
||||
$extensionAlias = $extension->getAlias();
|
||||
$container = $this->compileContainer();
|
||||
|
||||
$config = $this->getConfig($extension, $container);
|
||||
$config = $this->getConfig($extension, $container, $input->getOption('resolve-env'));
|
||||
|
||||
$format = $input->getOption('format');
|
||||
|
||||
if (\in_array($format, ['txt', 'yml'], true) && !class_exists(Yaml::class)) {
|
||||
$errorIo->error('Setting the "format" option to "txt" or "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (null === $path = $input->getArgument('path')) {
|
||||
$io->title(
|
||||
sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))
|
||||
);
|
||||
if ('txt' === $input->getOption('format')) {
|
||||
$io->title(
|
||||
sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))
|
||||
);
|
||||
}
|
||||
|
||||
$io->writeln(Yaml::dump([$extensionAlias => $config], 10));
|
||||
$io->writeln($this->convertToFormat([$extensionAlias => $config], $format));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -118,18 +125,26 @@ EOF
|
||||
|
||||
$io->title(sprintf('Current configuration for "%s.%s"', $extensionAlias, $path));
|
||||
|
||||
$io->writeln(Yaml::dump($config, 10));
|
||||
$io->writeln($this->convertToFormat($config, $format));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function convertToFormat(mixed $config, string $format): string
|
||||
{
|
||||
return match ($format) {
|
||||
'txt', 'yaml' => Yaml::dump($config, 10),
|
||||
'json' => json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE),
|
||||
default => throw new InvalidArgumentException(sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
|
||||
};
|
||||
}
|
||||
|
||||
private function compileContainer(): ContainerBuilder
|
||||
{
|
||||
$kernel = clone $this->getApplication()->getKernel();
|
||||
$kernel->boot();
|
||||
|
||||
$method = new \ReflectionMethod($kernel, 'buildContainer');
|
||||
$method->setAccessible(true);
|
||||
$container = $method->invoke($kernel);
|
||||
$container->getCompiler()->compile($container);
|
||||
|
||||
@@ -140,10 +155,8 @@ EOF
|
||||
* Iterate over configuration until the last step of the given path.
|
||||
*
|
||||
* @throws LogicException If the configuration does not exist
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getConfigForPath(array $config, string $path, string $alias)
|
||||
private function getConfigForPath(array $config, string $path, string $alias): mixed
|
||||
{
|
||||
$steps = explode('.', $path);
|
||||
|
||||
@@ -176,12 +189,12 @@ EOF
|
||||
|
||||
// Fall back to default config if the extension has one
|
||||
|
||||
if (!$extension instanceof ConfigurationExtensionInterface) {
|
||||
if (!$extension instanceof ConfigurationExtensionInterface && !$extension instanceof ConfigurationInterface) {
|
||||
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
|
||||
}
|
||||
|
||||
$configs = $container->getExtensionConfig($extensionAlias);
|
||||
$configuration = $extension->getConfiguration($configs, $container);
|
||||
$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($configs, $container);
|
||||
$this->validateConfiguration($extension, $configuration);
|
||||
|
||||
return (new Processor())->processConfiguration($configuration, $configs);
|
||||
@@ -190,7 +203,8 @@ EOF
|
||||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
||||
{
|
||||
if ($input->mustSuggestArgumentValuesFor('name')) {
|
||||
$suggestions->suggestValues($this->getAvailableBundles(!preg_match('/^[A-Z]/', $input->getCompletionValue())));
|
||||
$suggestions->suggestValues($this->getAvailableExtensions());
|
||||
$suggestions->suggestValues($this->getAvailableBundles());
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -200,27 +214,43 @@ EOF
|
||||
$config = $this->getConfig($this->findExtension($name), $this->compileContainer());
|
||||
$paths = array_keys(self::buildPathsCompletion($config));
|
||||
$suggestions->suggestValues($paths);
|
||||
} catch (LogicException $e) {
|
||||
} catch (LogicException) {
|
||||
}
|
||||
}
|
||||
|
||||
if ($input->mustSuggestOptionValuesFor('format')) {
|
||||
$suggestions->suggestValues($this->getAvailableFormatOptions());
|
||||
}
|
||||
}
|
||||
|
||||
private function getAvailableBundles(bool $alias): array
|
||||
private function getAvailableExtensions(): array
|
||||
{
|
||||
$kernel = $this->getApplication()->getKernel();
|
||||
|
||||
$extensions = [];
|
||||
foreach ($this->getContainerBuilder($kernel)->getExtensions() as $alias => $extension) {
|
||||
$extensions[] = $alias;
|
||||
}
|
||||
|
||||
return $extensions;
|
||||
}
|
||||
|
||||
private function getAvailableBundles(): array
|
||||
{
|
||||
$availableBundles = [];
|
||||
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
|
||||
$availableBundles[] = $alias ? $bundle->getContainerExtension()->getAlias() : $bundle->getName();
|
||||
$availableBundles[] = $bundle->getName();
|
||||
}
|
||||
|
||||
return $availableBundles;
|
||||
}
|
||||
|
||||
private function getConfig(ExtensionInterface $extension, ContainerBuilder $container)
|
||||
private function getConfig(ExtensionInterface $extension, ContainerBuilder $container, bool $resolveEnvs = false): mixed
|
||||
{
|
||||
return $container->resolveEnvPlaceholders(
|
||||
$container->getParameterBag()->resolveValue(
|
||||
$this->getConfigForExtension($extension, $container)
|
||||
)
|
||||
), $resolveEnvs ?: null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -229,7 +259,7 @@ EOF
|
||||
$completionPaths = [];
|
||||
foreach ($paths as $key => $values) {
|
||||
if (\is_array($values)) {
|
||||
$completionPaths = $completionPaths + self::buildPathsCompletion($values, $prefix.$key.'.');
|
||||
$completionPaths += self::buildPathsCompletion($values, $prefix.$key.'.');
|
||||
} else {
|
||||
$completionPaths[$prefix.$key] = null;
|
||||
}
|
||||
@@ -237,4 +267,9 @@ EOF
|
||||
|
||||
return $completionPaths;
|
||||
}
|
||||
|
||||
private function getAvailableFormatOptions(): array
|
||||
{
|
||||
return ['txt', 'yaml', 'json'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user