-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConsoleController.php
165 lines (137 loc) · 6.34 KB
/
ConsoleController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Sf2gen\Bundle\ConsoleBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
//Uses for shell access
use Symfony\Component\Process\Process;
use Symfony\Component\Process\PhpExecutableFinder;
//Uses for script access
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
//TODO: add the output formatter in the core
//use Symfony\Component\Console\Formatter\OutputFormatterHtml;
use Sf2gen\Bundle\ConsoleBundle\Formatter\OutputFormatterHtml;
/**
* Controller for console
*
* @author Cédric Lahouste
* @author Nicolas de Marqué
*
* @api
* @todo nico : add the output formatter in the core
* @todo nico : app validity test is not really efficient
*/
class ConsoleController extends Controller
{
public function requestAction()
{
$request = $this->get('request');
if ($request->isXmlHttpRequest() && $request->getMethod() == 'POST') {
// retrieve command string
$sf2Command = stripslashes($request->request->get('command'));
if ($sf2Command == '.') {
// this trick is used to give the possibility to have "php app/console" equivalent
$sf2Command = 'list';
} elseif ($sf2Command == 'cache:clear') {
// warming up the cache cannot be done after clearing it
// fix issue #11
$sf2Command .= ' --no-warmup';
}
//TODO: not really efficient
$rootFolder = basename($this->container->getParameter('kernel.root_dir'));
$app = $request->request->get('app') ?: $rootFolder;
if (!in_array($app, $this->container->getParameter('sf2gen_console.apps') )) {
return new Response('This application is not allowed...' , 200); // set to 200 to allow console display
}
//Try to run a separate shell process
if ($this->container->getParameter('sf2gen_console.new_process')) {
//Try to run a separate shell process
try {
$php = $this->getPhpExecutable();
$commandLine = $php . ' ' . $app . '/' . 'console ';
if(!empty($sf2Command)) {
$commandLine .= $sf2Command;
}
$p = new Process(
$commandLine,
$rootFolder,
null,
null,
30,
array(
'suppress_errors' => false,
'bypass_shell' => false,
)
);
$p->run();
$output = $p->getOutput();
/*
if the process is not successful:
- 1) Symfony throws an error and ouput is not empty; continue without Exception.
- 2) Process throws an error and ouput is empty => Exception!
*/
if (!$p->isSuccessful() && empty($output)) {
throw new \RuntimeException('Unabled to run the process.');
}
} catch(\Exception $e) { // not trying the other method. It is interesting to know where it is not working (single process or not)
return new Response(nl2br("The request failed when using a separated shell process. Try to use 'new_process: false' in configuration.\n Error : ".$e->getMessage()));
}
} else {
//Try to execute a console within this process
//TODO: fix cache:clear issue
try {
$input = new StringInput($sf2Command);
//Prepare output
ob_start();
$output = new StreamOutput(fopen("php://output", 'w'), StreamOutput::VERBOSITY_NORMAL, true, new OutputFormatterHtml(true));
//Start a kernel/console and an application
$env = $input->getParameterOption(array('--env', '-e'), $this->container->getParameter('sf2gen_console.default_env'));
$debug = !$input->hasParameterOption(array('--no-debug', ''));
$kernel = new \AppKernel($env, $debug);
$application = new Application($kernel);
$application->setAutoExit(false);
//Find, initialize and run the real command
$run = $application->run($input, $output);
$output = ob_get_contents();
ob_end_clean();
} catch(\Exception $e){
return new Response(nl2br("The request failed when using same process.\n Error : ".$e->getMessage()));
}
}
// common response for both methods
if (empty($output)) {
$output = 'The command "'.$sf2Command.'" was successful.';
}
return new Response($this->convertOutput($output));
}
return new Response('This request was not found.', 404); // request is not a POST request
}
public function convertOutput($output)
{
// TODO : use OutputFormatterHtml
return '<pre>'.$output.'</pre>';
}
public function toolbarAction()
{
$request = $this->container->get('request');
if (null !== $session = $request->getSession()) {
// keep current flashes for one more request
$session->setFlashes($session->getFlashes());
}
$position = false === strpos($request->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
return $this->container->get('templating')->renderResponse('Sf2genConsoleBundle:Console:toolbar.html.twig', array(
'position' => $position,
'apps' => $this->container->getParameter('sf2gen_console.apps'),
));
}
public function getPhpExecutable()
{
$executableFinder = new PhpExecutableFinder();
$php = $executableFinder->find();
if (empty($php)) {
throw new \RuntimeException('Unable to find the PHP executable. Verify your PATH variable.');
}
return $php;
}
}