Completed
Push — master ( b060f9...d49b30 )
by Luis Henrique
14:49 queued 40s
created

DatabaseCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
require_once __DIR__ . '/../vendor/autoload.php';
4
5
use Lcobucci\DependencyInjection\ContainerConfig;
6
use LuisMulinari\Consoleful\Command\ContainerAwareCommand;
7
use LuisMulinari\Consoleful\Application;
8
use Lcobucci\DependencyInjection\ContainerInjector;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
13
class ExampleCommand extends ContainerAwareCommand
14
{
15
    use ContainerInjector;
16
17
    protected function configure()
18
    {
19
        $this->setName("example");
20
        $this->setDescription('* Simple example');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $container = $this->getContainer();
26
27
        $progress = new ProgressBar($output, 5);
28
29
        $progress->start();
30
31
        $i = 0;
32
        while ($i++ < 5) {
33
            $progress->advance();
34
35
            sleep(1);
36
        }
37
38
        $progress->finish();
39
40
        $output->writeln($container->getParameter('parameter.example'));
41
    }
42
}
43
44
class DatabaseCommand extends ContainerAwareCommand
45
{
46
    use ContainerInjector;
47
48
    protected function configure()
49
    {
50
        $this->setName('database');
51
        $this->setDescription('* Exemple with database');
52
    }
53
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        if (!class_exists('Doctrine\DBAL\Connection')) {
57
            $output->write('You need to include "doctrine/dbal" package in your composer.json');
58
59
            return false;
60
        }
61
62
        $databaseTables = $this->getContainer()->get('database.connection')->query('SHOW TABLES')->fetchAll();
63
64
        $tableHelper = $this->getHelper('table');
65
66
        $tableHelper->setHeaders(['Table name']);
67
        $tableHelper->setRows($databaseTables);
68
69
        $tableHelper->render($output);
70
    }
71
}
72
73
74
$application = new Application(
75
    'Application name',
76
    'Version',
77
    new ContainerConfig(__DIR__ . '/services.xml') // services.[xml|yml|php]
78
);
79
80
$application->add(new ExampleCommand());
81
$application->add(new DatabaseCommand());
82
83
$application->run();
84