123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\FrameworkBundle\Console;
- use Symfony\Component\Console\Application as BaseApplication;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Command\ListCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\ConsoleOutputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Style\SymfonyStyle;
- use Symfony\Component\DependencyInjection\ContainerAwareInterface;
- use Symfony\Component\HttpKernel\Bundle\Bundle;
- use Symfony\Component\HttpKernel\Kernel;
- use Symfony\Component\HttpKernel\KernelInterface;
- /**
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class Application extends BaseApplication
- {
- private $kernel;
- private $commandsRegistered = false;
- private $registrationErrors = [];
- public function __construct(KernelInterface $kernel)
- {
- $this->kernel = $kernel;
- parent::__construct('Symfony', Kernel::VERSION);
- $inputDefinition = $this->getDefinition();
- $inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
- $inputDefinition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switch off debug mode.'));
- }
- /**
- * Gets the Kernel associated with this Console.
- *
- * @return KernelInterface A KernelInterface instance
- */
- public function getKernel()
- {
- return $this->kernel;
- }
- /**
- * {@inheritdoc}
- */
- public function reset()
- {
- if ($this->kernel->getContainer()->has('services_resetter')) {
- $this->kernel->getContainer()->get('services_resetter')->reset();
- }
- }
- /**
- * Runs the current application.
- *
- * @return int 0 if everything went fine, or an error code
- */
- public function doRun(InputInterface $input, OutputInterface $output)
- {
- $this->registerCommands();
- if ($this->registrationErrors) {
- $this->renderRegistrationErrors($input, $output);
- }
- $this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
- return parent::doRun($input, $output);
- }
- /**
- * {@inheritdoc}
- */
- protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
- {
- if (!$command instanceof ListCommand) {
- if ($this->registrationErrors) {
- $this->renderRegistrationErrors($input, $output);
- $this->registrationErrors = [];
- }
- return parent::doRunCommand($command, $input, $output);
- }
- $returnCode = parent::doRunCommand($command, $input, $output);
- if ($this->registrationErrors) {
- $this->renderRegistrationErrors($input, $output);
- $this->registrationErrors = [];
- }
- return $returnCode;
- }
- /**
- * {@inheritdoc}
- */
- public function find($name)
- {
- $this->registerCommands();
- return parent::find($name);
- }
- /**
- * {@inheritdoc}
- */
- public function get($name)
- {
- $this->registerCommands();
- $command = parent::get($name);
- if ($command instanceof ContainerAwareInterface) {
- $command->setContainer($this->kernel->getContainer());
- }
- return $command;
- }
- /**
- * {@inheritdoc}
- */
- public function all($namespace = null)
- {
- $this->registerCommands();
- return parent::all($namespace);
- }
- /**
- * {@inheritdoc}
- */
- public function getLongVersion()
- {
- return parent::getLongVersion().sprintf(' (env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false');
- }
- public function add(Command $command)
- {
- $this->registerCommands();
- return parent::add($command);
- }
- protected function registerCommands()
- {
- if ($this->commandsRegistered) {
- return;
- }
- $this->commandsRegistered = true;
- $this->kernel->boot();
- $container = $this->kernel->getContainer();
- foreach ($this->kernel->getBundles() as $bundle) {
- if ($bundle instanceof Bundle) {
- try {
- $bundle->registerCommands($this);
- } catch (\Throwable $e) {
- $this->registrationErrors[] = $e;
- }
- }
- }
- if ($container->has('console.command_loader')) {
- $this->setCommandLoader($container->get('console.command_loader'));
- }
- if ($container->hasParameter('console.command.ids')) {
- $lazyCommandIds = $container->hasParameter('console.lazy_command.ids') ? $container->getParameter('console.lazy_command.ids') : [];
- foreach ($container->getParameter('console.command.ids') as $id) {
- if (!isset($lazyCommandIds[$id])) {
- try {
- $this->add($container->get($id));
- } catch (\Throwable $e) {
- $this->registrationErrors[] = $e;
- }
- }
- }
- }
- }
- private function renderRegistrationErrors(InputInterface $input, OutputInterface $output)
- {
- if ($output instanceof ConsoleOutputInterface) {
- $output = $output->getErrorOutput();
- }
- (new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
- foreach ($this->registrationErrors as $error) {
- $this->doRenderThrowable($error, $output);
- }
- }
- }
|