1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SensioLabs\AnsiConverter\AnsiToHtmlConverter; |
4
|
|
|
use Symfony\Component\Console\Application; |
5
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
6
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
7
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
8
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ConsoleController |
13
|
|
|
* The Central Command Access Point and Bootstrapper. |
14
|
|
|
*/ |
15
|
|
|
class ConsoleController extends Controller |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private static $allowed_actions = [ |
|
|
|
|
21
|
|
|
'publish', |
22
|
|
|
'ConsoleForm' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SilverstripeApplication |
27
|
|
|
*/ |
28
|
|
|
protected $application; |
29
|
|
|
|
30
|
|
|
public function init() |
31
|
|
|
{ |
32
|
|
|
parent::init(); |
33
|
|
|
|
34
|
|
|
$this->application = new SilverstripeApplication(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function index(SS_HTTPRequest $request) |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Lets be clear when calling commands |
41
|
|
|
*/ |
42
|
|
|
ini_set('display_errors', 1); |
43
|
|
|
|
44
|
|
|
if(!Director::is_cli()) { |
45
|
|
|
if(!Permission::check("ADMIN")) { |
46
|
|
|
return Security::permissionFailure(); |
47
|
|
|
} |
48
|
|
|
return $this->callFromBrowser($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->callFromConsole(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function callFromConsole() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
// remove the framework/cli-script.php argument |
57
|
|
|
array_shift($_SERVER['argv']); |
58
|
|
|
|
59
|
|
|
$this->application->run(new ArgvInput($_SERVER['argv']), new ConsoleOutput()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function callFromBrowser(SS_HTTPRequest $request) |
63
|
|
|
{ |
64
|
|
|
$input = new ArrayInput(array( |
65
|
|
|
'command' => $request->param('ID') |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
$output = new BufferedOutput( |
69
|
|
|
OutputInterface::VERBOSITY_NORMAL, |
70
|
|
|
true // true for decorated |
71
|
|
|
); |
72
|
|
|
$this->application->run($input, $output); |
73
|
|
|
|
74
|
|
|
// return the output |
75
|
|
|
$converter = new AnsiToHtmlConverter(); |
76
|
|
|
$content = $output->fetch(); |
77
|
|
|
|
78
|
|
|
return ['ConsoleOutput' => $converter->convert($content)]; |
79
|
|
|
} |
80
|
|
|
/** |
81
|
|
|
* @return ConsoleForm |
82
|
|
|
*/ |
83
|
|
|
public function ConsoleForm() |
84
|
|
|
{ |
85
|
|
|
return new ConsoleForm($this, 'ConsoleForm'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.