1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
5
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
6
|
|
|
use Symfony\Component\Console\Application as SymfonyApplication; |
7
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Application |
11
|
|
|
* |
12
|
|
|
* Shameless copy/paste from Taylor Otwell's Laravel |
13
|
|
|
*/ |
14
|
|
|
class Application extends SymfonyApplication |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The output from the previous command. |
19
|
|
|
* |
20
|
|
|
* @var \Symfony\Component\Console\Output\BufferedOutput |
21
|
|
|
*/ |
22
|
|
|
protected $lastOutput; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
parent::__construct(); |
27
|
|
|
|
28
|
|
|
$this->loadCommands(); |
29
|
|
|
|
30
|
|
|
$this->setAutoExit(false); |
31
|
|
|
$this->setCatchExceptions(false); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Run an Artisan console command by name. |
36
|
|
|
* |
37
|
|
|
* @param string $command |
38
|
|
|
* @param array $parameters |
39
|
|
|
* @return int |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function call($command, array $parameters = []) |
42
|
|
|
{ |
43
|
|
|
$parameters = array_merge((array)$command, $parameters); |
44
|
|
|
|
45
|
|
|
$this->lastOutput = new BufferedOutput; |
46
|
|
|
|
47
|
|
|
$this->setCatchExceptions(false); |
48
|
|
|
|
49
|
|
|
$result = $this->run(new ArrayInput($parameters), $this->lastOutput); |
50
|
|
|
|
51
|
|
|
$this->setCatchExceptions(true); |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the output for the last run command. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function output() |
62
|
|
|
{ |
63
|
|
|
return $this->lastOutput ? $this->lastOutput->fetch() : ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add a command to the console. |
68
|
|
|
* |
69
|
|
|
* @param \Symfony\Component\Console\Command\Command $command |
70
|
|
|
* @return \Symfony\Component\Console\Command\Command |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function add(SymfonyCommand $command) |
73
|
|
|
{ |
74
|
|
|
return $this->addToParent($command); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add the command to the parent instance. |
79
|
|
|
* |
80
|
|
|
* @param \Symfony\Component\Console\Command\Command $command |
81
|
|
|
* @return \Symfony\Component\Console\Command\Command |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
protected function addToParent(SymfonyCommand $command) |
84
|
|
|
{ |
85
|
|
|
return parent::add($command); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the default input definitions for the applications. |
90
|
|
|
* |
91
|
|
|
* This is used to add the --env option to every available command. |
92
|
|
|
* |
93
|
|
|
* @return \Symfony\Component\Console\Input\InputDefinition |
94
|
|
|
*/ |
95
|
|
|
protected function getDefaultInputDefinition() |
96
|
|
|
{ |
97
|
|
|
$definition = parent::getDefaultInputDefinition(); |
98
|
|
|
|
99
|
|
|
$definition->addOption($this->getEnvironmentOption()); |
100
|
|
|
|
101
|
|
|
return $definition; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the global environment option for the definition. |
106
|
|
|
* |
107
|
|
|
* @return \Symfony\Component\Console\Input\InputOption |
108
|
|
|
*/ |
109
|
|
|
protected function getEnvironmentOption() |
110
|
|
|
{ |
111
|
|
|
$message = 'The environment the command should run under.'; |
112
|
|
|
|
113
|
|
|
return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Load all available commands into the console application |
118
|
|
|
*/ |
119
|
|
|
protected function loadCommands() |
120
|
|
|
{ |
121
|
|
|
/** |
122
|
|
|
* Why does this not work |
123
|
|
|
*/ |
124
|
|
|
$commands = ClassInfo::subclassesFor('SilverstripeCommand'); //var_dump($commands);exit(); |
|
|
|
|
125
|
|
|
|
126
|
|
|
// This works, but does not load custom Commands |
127
|
|
|
$commands = ClassInfo::classes_for_folder(BASE_PATH . '/console/code/'); |
128
|
|
|
|
129
|
|
|
/** @var SilverstripeCommand $command */ |
130
|
|
|
foreach ($commands as $command) { |
131
|
|
|
$reflection = new ReflectionClass($command); |
132
|
|
|
if (!$reflection->isAbstract() && $reflection->isSubclassOf('SilverstripeCommand')) { |
133
|
|
|
$this->add(new $command()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.