Completed
Push — develop ( e4a933...b1fd58 )
by Tom
03:38
created

ConsoleCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use N98\Util\Console\Helper\DatabaseHelper;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class ConsoleCommand extends AbstractDatabaseCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('db:console')
16
            ->setAliases(array('mysql-client'))
17
            ->addOption(
18
                'use-mycli-instead-of-mysql',
19
                null,
20
                InputOption::VALUE_NONE,
21
                'Use `mycli` as the MySQL client instead of `mysql`'
22
            )
23
            ->addOption(
24
                'no-auto-rehash',
25
                null,
26
                InputOption::VALUE_NONE,
27
                'Same as `-A` option to MySQL client to turn off ' .
28
                'auto-complete (avoids long initial connection time).'
29
            )
30
            ->setDescription('Opens mysql client by database config from local.xml');
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     *
37
     * @return int|void
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $this->detectDbSettings($output);
42
43
        $args = array(
44
            $input->getOption('use-mycli-instead-of-mysql') ? 'mycli' : 'mysql',
45
        );
46
47
        if ($input->getOption('no-auto-rehash')) {
48
            $args[] = '--no-auto-rehash';
49
        }
50
51
        $args[] = $this->getMysqlClientToolConnection();
52
53
        $this->processCommand(implode(' ', $args));
54
    }
55
56
    /**
57
     * execute a command
58
     *
59
     * @param string $command
60
     */
61
    private function processCommand($command)
62
    {
63
        $descriptorSpec = array(
64
            0 => STDIN,
65
            1 => STDOUT,
66
            2 => STDERR,
67
        );
68
69
        $pipes = array();
70
        $process = proc_open($command, $descriptorSpec, $pipes);
71
72
        if (is_resource($process)) {
73
            proc_close($process);
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    private function getMysqlClientToolConnection()
81
    {
82
        /* @var $database DatabaseHelper */
83
        $database = $this->getHelper('database');
84
85
        return $database->getMysqlClientToolConnectionString();
86
    }
87
}
88