Completed
Push — master ( b9fa67...e31f65 )
by Andreas
24:00 queued 04:20
created

exec   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 15.69%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 49
dl 0
loc 83
ccs 8
cts 51
cp 0.1569
rs 10
c 4
b 0
f 1
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 30 5
A interact() 0 17 6
A list_files() 0 15 4
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use midcom\console\loginhelper;
17
use midcom;
18
19
/**
20
 * CLI wrapper for midcom-exec calls
21
 *
22
 * @package midcom.console
23
 */
24
class exec extends Command
25
{
26
    use loginhelper;
0 ignored issues
show
Bug introduced by
The trait midcom\console\loginhelper requires the property $auth which is not provided by midcom\console\command\exec.
Loading history...
27
28 1
    protected function configure()
29
    {
30 1
        $this->setName('midcom:exec')
31 1
            ->setAliases(['exec'])
32 1
            ->setDescription('Runs a script in midcom context')
33 1
            ->addArgument('file', InputArgument::OPTIONAL, 'The file to run (leave empty to list available files)')
34 1
            ->addArgument('get', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Additional GET parameters (key=value pairs, space-separated)')
35 1
            ->addOption('login', 'l', InputOption::VALUE_NONE, 'Use Midgard authorization');
36 1
    }
37
38
    protected function interact(InputInterface $input, OutputInterface $output)
39
    {
40
        if ($input->getOption('login')) {
41
            $dialog = $this->getHelperSet()->get('question');
42
            $this->login($dialog, $input, $output);
43
        }
44
        $get = $input->getArgument('get');
45
46
        if (is_array($get)) {
47
            foreach ($get as $data) {
48
                $input_data = explode('=', $data);
49
                if (count($input_data) == 1) {
50
                    $_GET[] = $input_data;
51
                } elseif (count($input_data) == 2) {
52
                    $_GET[$input_data[0]] = $input_data[1];
53
                } else {
54
                    throw new \RuntimeException('Could not parse input ' . $data);
55
                }
56
            }
57
        }
58
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output) : int
61
    {
62
        $file = $input->getArgument('file');
63
64
        if (empty($file)) {
65
            $this->list_files($output);
66
            return 0;
67
        }
68
69
        if (!file_exists(OPENPSA_PROJECT_BASEDIR . $file)) {
0 ignored issues
show
Bug introduced by
Are you sure $file of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        if (!file_exists(OPENPSA_PROJECT_BASEDIR . /** @scrutinizer ignore-type */ $file)) {
Loading history...
70
            throw new \midcom_error('File not found');
71
        }
72
73
        if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
74
            $output->writeln('Running ' . $file);
75
        }
76
77
        midcom::get()->auth->request_sudo('midcom.exec');
78
        midcom::get()->cache->content->enable_live_mode();
79
80
        try {
81
            require OPENPSA_PROJECT_BASEDIR . $file;
82
        } catch (\midcom_error_forbidden $e) {
83
            $dialog = $this->getHelperSet()->get('question');
84
            $this->login($dialog, $input, $output);
85
            require OPENPSA_PROJECT_BASEDIR . $file;
86
        }
87
88
        midcom::get()->auth->drop_sudo();#
89
        return 0;
90
    }
91
92
    private function list_files(OutputInterface $output)
93
    {
94
        $output->writeln("\n<comment>Available exec files:</comment>\n");
95
96
        $loader = midcom::get()->componentloader;
97
        foreach (array_keys($loader->manifests) as $name) {
98
            $exec_dir = $loader->path_to_snippetpath($name) . '/exec';
99
100
            if (is_dir($exec_dir)) {
101
                foreach (glob($exec_dir . '/*.php') as $file) {
102
                    $path = preg_replace('/^' . preg_quote(OPENPSA_PROJECT_BASEDIR, '/') . '/', '', $file);
103
                    $parts = pathinfo($path);
104
                    $path = '  <info>' . $parts['dirname'] . '/</info>' . $parts['filename'] . '<info>.' . $parts['extension'] . '</info>';
105
106
                    $output->writeln($path);
107
                }
108
            }
109
        }
110
    }
111
}
112