Пример #1
0
            sleep(1);
        }
        $progress->finish();
        $output->writeln($container->getParameter('parameter.example'));
    }
}
class DatabaseCommand extends ContainerAwareCommand
{
    use ContainerInjector;
    protected function configure()
    {
        $this->setName('database');
        $this->setDescription('* Exemple with database');
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if (!class_exists('Doctrine\\DBAL\\Connection')) {
            $output->write('You need to include "doctrine/dbal" package in your composer.json');
            return false;
        }
        $databaseTables = $this->getContainer()->get('database.connection')->query('SHOW TABLES')->fetchAll();
        $tableHelper = $this->getHelper('table');
        $tableHelper->setHeaders(['Table name']);
        $tableHelper->setRows($databaseTables);
        $tableHelper->render($output);
    }
}
$application = new Application('Application name', 'Version', new ContainerConfig(__DIR__ . '/services.xml'));
$application->add(new ExampleCommand());
$application->add(new DatabaseCommand());
$application->run();
Пример #2
0
 /**
  * @test
  *
  * @covers LuisMulinari\Consoleful\Application::__construct
  * @covers LuisMulinari\Consoleful\Application::doRun
  * @covers LuisMulinari\Consoleful\Application::injectContainer
  * @covers Symfony\Component\Console\Application::__construct
  * @covers Symfony\Component\Console\Application::add
  * @covers Symfony\Component\Console\Application::doRun
  * @covers Lcobucci\DependencyInjection\ContainerInjector::setContainer
  * @covers Lcobucci\DependencyInjection\ContainerInjector::getContainer
  */
 public function doRunShouldInjectTheContainerOnContainerAwareCommands()
 {
     $input = $this->getMock(InputInterface::class);
     $output = $this->getMock(OutputInterface::class);
     $container = $this->getMock(ContainerInterface::class);
     $this->builder->expects($this->never())->method('getContainer');
     $command = new CommandTest();
     $command2 = new ContainerAwareCommandTest();
     $application = new Application('name', 'version', $this->config, $this->builder);
     $application->add($command);
     $application->add($command2);
     $application->setContainer($container);
     $application->doRun($input, $output);
     $this->assertAttributeSame($container, 'container', $command2);
 }