Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConsoleRunner
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCommandList
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 run
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Console runner.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Console
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindConsole;
31
32use Psr\Container\ContainerInterface;
33use Symfony\Component\Console\Application;
34
35/**
36 * Console runner.
37 *
38 * @category VuFind
39 * @package  Console
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class ConsoleRunner
45{
46    /**
47     * List of commands
48     *
49     * @var array
50     */
51    protected $commands;
52
53    /**
54     * Plugin manager (to retrieve commands)
55     *
56     * @var ContainerInterface
57     */
58    protected $pluginManager;
59
60    /**
61     * Constructor
62     *
63     * @param ContainerInterface $pm Plugin manager (to retrieve commands)
64     */
65    public function __construct(ContainerInterface $pm)
66    {
67        $this->pluginManager = $pm;
68    }
69
70    /**
71     * Get the command or list of commands to run.
72     *
73     * @return array
74     */
75    protected function getCommandList()
76    {
77        // Does the first argument match a command alias? If so, load only that:
78        if ($this->pluginManager->has($_SERVER['argv'][1] ?? '')) {
79            return [$_SERVER['argv'][1]];
80        }
81
82        // Do the first two arguments match a command alias? If so, manipulate
83        // the arguments (converting legacy format to Symfony format) and return
84        // the resulting command:
85        $command = ($_SERVER['argv'][1] ?? '') . '/' . ($_SERVER['argv'][2] ?? '');
86        if ($this->pluginManager->has($command)) {
87            $_SERVER['argc']--;
88            array_splice($_SERVER['argv'], 1, 2, [$command]);
89            return [$command];
90        }
91
92        // Default behavior: return all values
93        return $this->pluginManager->getCommandList();
94    }
95
96    /**
97     * Run the console action
98     *
99     * @return mixed
100     */
101    public function run()
102    {
103        // Get command list before initializing Application, since we may need
104        // to manipulate $_SERVER for backward compatibility.
105        $commands = $this->getCommandList();
106
107        // Launch Symfony:
108        $consoleApp = new Application();
109        foreach ($commands as $command) {
110            $consoleApp->add($this->pluginManager->get($command));
111        }
112        return $consoleApp->run();
113    }
114}