mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-25 11:38:44 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -11,10 +11,16 @@
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Command;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor;
|
||||
use Symfony\Component\Console\Completion\CompletionInput;
|
||||
use Symfony\Component\Console\Completion\CompletionSuggestions;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
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\HttpKernel\Debug\FileLinkFormatter;
|
||||
|
||||
/**
|
||||
* A console command for autowiring information.
|
||||
@@ -26,6 +32,17 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
class DebugAutowiringCommand extends ContainerDebugCommand
|
||||
{
|
||||
protected static $defaultName = 'debug:autowiring';
|
||||
protected static $defaultDescription = 'List classes/interfaces you can use for autowiring';
|
||||
|
||||
private $supportsHref;
|
||||
private $fileLinkFormatter;
|
||||
|
||||
public function __construct(string $name = null, FileLinkFormatter $fileLinkFormatter = null)
|
||||
{
|
||||
$this->supportsHref = method_exists(OutputFormatterStyle::class, 'setHref');
|
||||
$this->fileLinkFormatter = $fileLinkFormatter;
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -35,10 +52,11 @@ class DebugAutowiringCommand extends ContainerDebugCommand
|
||||
$this
|
||||
->setDefinition([
|
||||
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'),
|
||||
new InputOption('all', null, InputOption::VALUE_NONE, 'Show also services that are not aliased'),
|
||||
])
|
||||
->setDescription('Lists classes/interfaces you can use for autowiring')
|
||||
->setDescription(self::$defaultDescription)
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> command displays all classes and interfaces that
|
||||
The <info>%command.name%</info> command displays the classes and interfaces that
|
||||
you can use as type-hints for autowiring:
|
||||
|
||||
<info>php %command.full_name%</info>
|
||||
@@ -55,18 +73,20 @@ EOF
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$errorIo = $io->getErrorStyle();
|
||||
|
||||
$builder = $this->getContainerBuilder();
|
||||
$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
|
||||
$serviceIds = $builder->getServiceIds();
|
||||
$serviceIds = array_filter($serviceIds, [$this, 'filterToServiceTypes']);
|
||||
|
||||
if ($search = $input->getArgument('search')) {
|
||||
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($search) {
|
||||
return false !== stripos($serviceId, $search);
|
||||
$searchNormalized = preg_replace('/[^a-zA-Z0-9\x7f-\xff $]++/', '', $search);
|
||||
|
||||
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($searchNormalized) {
|
||||
return false !== stripos(str_replace('\\', '', $serviceId), $searchNormalized) && !str_starts_with($serviceId, '.');
|
||||
});
|
||||
|
||||
if (empty($serviceIds)) {
|
||||
@@ -76,24 +96,82 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
asort($serviceIds);
|
||||
uasort($serviceIds, 'strnatcmp');
|
||||
|
||||
$io->title('Autowirable Services');
|
||||
$io->title('Autowirable Types');
|
||||
$io->text('The following classes & interfaces can be used as type-hints when autowiring:');
|
||||
if ($search) {
|
||||
$io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search));
|
||||
}
|
||||
$io->newLine();
|
||||
$tableRows = [];
|
||||
$hasAlias = [];
|
||||
$all = $input->getOption('all');
|
||||
$previousId = '-';
|
||||
$serviceIdsNb = 0;
|
||||
foreach ($serviceIds as $serviceId) {
|
||||
$tableRows[] = [sprintf('<fg=cyan>%s</fg=cyan>', $serviceId)];
|
||||
if ($builder->hasAlias($serviceId)) {
|
||||
$tableRows[] = [sprintf(' alias to %s', $builder->getAlias($serviceId))];
|
||||
$text = [];
|
||||
$resolvedServiceId = $serviceId;
|
||||
if (!str_starts_with($serviceId, $previousId)) {
|
||||
$text[] = '';
|
||||
if ('' !== $description = Descriptor::getClassDescription($serviceId, $resolvedServiceId)) {
|
||||
if (isset($hasAlias[$serviceId])) {
|
||||
continue;
|
||||
}
|
||||
$text[] = $description;
|
||||
}
|
||||
$previousId = $serviceId.' $';
|
||||
}
|
||||
|
||||
$serviceLine = sprintf('<fg=yellow>%s</>', $serviceId);
|
||||
if ($this->supportsHref && '' !== $fileLink = $this->getFileLink($serviceId)) {
|
||||
$serviceLine = sprintf('<fg=yellow;href=%s>%s</>', $fileLink, $serviceId);
|
||||
}
|
||||
|
||||
if ($builder->hasAlias($serviceId)) {
|
||||
$hasAlias[$serviceId] = true;
|
||||
$serviceAlias = $builder->getAlias($serviceId);
|
||||
$serviceLine .= ' <fg=cyan>('.$serviceAlias.')</>';
|
||||
|
||||
if ($serviceAlias->isDeprecated()) {
|
||||
$serviceLine .= ' - <fg=magenta>deprecated</>';
|
||||
}
|
||||
} elseif (!$all) {
|
||||
++$serviceIdsNb;
|
||||
continue;
|
||||
}
|
||||
$text[] = $serviceLine;
|
||||
$io->text($text);
|
||||
}
|
||||
|
||||
$io->table([], $tableRows);
|
||||
$io->newLine();
|
||||
|
||||
return null;
|
||||
if (0 < $serviceIdsNb) {
|
||||
$io->text(sprintf('%s more concrete service%s would be displayed when adding the "--all" option.', $serviceIdsNb, $serviceIdsNb > 1 ? 's' : ''));
|
||||
}
|
||||
if ($all) {
|
||||
$io->text('Pro-tip: use interfaces in your type-hints instead of classes to benefit from the dependency inversion principle.');
|
||||
}
|
||||
|
||||
$io->newLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getFileLink(string $class): string
|
||||
{
|
||||
if (null === $this->fileLinkFormatter
|
||||
|| (null === $r = $this->getContainerBuilder($this->getApplication()->getKernel())->getReflectionClass($class, false))) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string) $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine());
|
||||
}
|
||||
|
||||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
||||
{
|
||||
if ($input->mustSuggestArgumentValuesFor('search')) {
|
||||
$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
|
||||
|
||||
$suggestions->suggestValues(array_filter($builder->getServiceIds(), [$this, 'filterToServiceTypes']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user