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
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SilverstripeApplication |
26
|
|
|
*/ |
27
|
|
|
protected $application; |
28
|
|
|
|
29
|
|
|
public function init() |
30
|
|
|
{ |
31
|
|
|
parent::init(); |
32
|
|
|
|
33
|
|
|
$this->application = new SilverstripeApplication(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function index(SS_HTTPRequest $request) |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Lets be clear when calling commands |
40
|
|
|
*/ |
41
|
|
|
ini_set('display_errors', 1); |
42
|
|
|
|
43
|
|
|
if(!Director::is_cli()) { |
44
|
|
|
if(!Permission::check("ADMIN")) { |
45
|
|
|
return Security::permissionFailure(); |
46
|
|
|
} |
47
|
|
|
return $this->callFromBrowser($request); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->callFromConsole(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function callFromConsole() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
// remove the framework/cli-script.php argument |
56
|
|
|
array_shift($_SERVER['argv']); |
57
|
|
|
|
58
|
|
|
$this->application->run(new ArgvInput($_SERVER['argv']), new ConsoleOutput()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function callFromBrowser(SS_HTTPRequest $request) |
62
|
|
|
{ |
63
|
|
|
$input = new ArrayInput(array( |
64
|
|
|
'command' => $request->param('ID') |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$output = new BufferedOutput( |
68
|
|
|
OutputInterface::VERBOSITY_NORMAL, |
69
|
|
|
true // true for decorated |
70
|
|
|
); |
71
|
|
|
$this->application->run($input, $output); |
72
|
|
|
|
73
|
|
|
// return the output |
74
|
|
|
$converter = new AnsiToHtmlConverter(); |
75
|
|
|
$content = $output->fetch(); |
76
|
|
|
|
77
|
|
|
return ['ConsoleOutput' => $converter->convert($content)]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.