Completed
Push — develop ( e4a933...e2f019 )
by Tom
03:20
created

ConsoleCommand::execute()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 14
nc 8
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
        $descriptorSpec = array(
44
            0 => STDIN,
45
            1 => STDOUT,
46
            2 => STDERR,
47
        );
48
49
        $mysqlClient = $input->getOption('use-mycli-instead-of-mysql') ? 'mycli' : 'mysql';
50
        $noAutoRehash = $input->getOption('no-auto-rehash') ? '--no-auto-rehash ' : '';
51
52
        /* @var $database DatabaseHelper */
53
        $database = $this->getHelper('database');
54
        $exec = $mysqlClient . ' ' . $noAutoRehash . $database->getMysqlClientToolConnectionString();
55
56
        $pipes = array();
57
        $process = proc_open($exec, $descriptorSpec, $pipes);
58
59
        if (is_resource($process)) {
60
            proc_close($process);
61
        }
62
    }
63
}
64