1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager\Command; |
12
|
|
|
|
13
|
|
|
use ComponentManager\Console\Argument; |
14
|
|
|
use ComponentManager\Exception\MoodleException; |
15
|
|
|
use ComponentManager\MoodleInstallation; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Moodle command. |
24
|
|
|
* |
25
|
|
|
* Provides a query interface to a Moodle installation. |
26
|
|
|
*/ |
27
|
|
|
class MoodleCommand extends ProjectAwareCommand { |
28
|
|
|
/** |
29
|
|
|
* Help text. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
const HELP = <<<HELP |
34
|
|
|
Queries properties of the Moodle installation in the present working directory. |
35
|
|
|
HELP; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc Command |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
protected function configure() { |
|
|
|
|
41
|
|
|
$this |
42
|
|
|
->setName('moodle') |
43
|
|
|
->setDescription('Queries properties of a Moodle installation') |
44
|
|
|
->setHelp(static::HELP) |
45
|
|
|
->setDefinition(new InputDefinition([ |
46
|
|
|
new InputArgument(Argument::ARGUMENT_ACTION, |
47
|
|
|
InputArgument::REQUIRED, |
48
|
|
|
Argument::ARGUMENT_ACTION_HELP), |
49
|
|
|
new InputOption(Argument::ARGUMENT_MOODLE_DIR, null, |
50
|
|
|
InputOption::VALUE_REQUIRED, |
51
|
|
|
Argument::ARGUMENT_MOODLE_DIR_HELP), |
52
|
|
|
])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc Command |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
59
|
|
|
$action = $input->getArgument(Argument::ARGUMENT_ACTION); |
60
|
|
|
if (!$moodleDir = $input->getOption(Argument::ARGUMENT_MOODLE_DIR)) { |
61
|
|
|
$moodleDir = $this->platform->getWorkingDirectory(); |
62
|
|
|
} |
63
|
|
|
$moodle = new MoodleInstallation($this->platform, $moodleDir); |
64
|
|
|
$moodle->configure(); |
65
|
|
|
|
66
|
|
|
switch ($action) { |
67
|
|
|
case Argument::ARGUMENT_ACTION_LIST_PLUGIN_TYPES: |
68
|
|
|
$result = $moodle->getPluginTypes(); |
69
|
|
|
break; |
70
|
|
|
default: |
71
|
|
|
throw new MoodleException( |
72
|
|
|
sprintf('Invalid action "%s"', $action), |
73
|
|
|
MoodleException::CODE_INVALID_ACTION); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$moodle->dispose(); |
77
|
|
|
|
78
|
|
|
$output->writeln(json_encode($result, JSON_PRETTY_PRINT)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.