forked from RapotOR/ConsoleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sf2genConsoleListener.php
127 lines (105 loc) · 3.58 KB
/
Sf2genConsoleListener.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
<?php
namespace Sf2gen\Bundle\ConsoleBundle;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Bundle\TwigBundle\TwigEngine;
/**
* Listener for console
*
* @author Cédric Lahouste
* @author winzou
*
* @api
*/
class Sf2genConsoleListener
{
protected $templating;
protected $kernel;
protected $cacheDir;
protected $cacheFile;
public function __construct(Kernel $kernel, TwigEngine $templating)
{
$this->templating = $templating;
$this->kernel = $kernel;
$this->cacheDir = $this->kernel->getCacheDir() . DIRECTORY_SEPARATOR . 'sf2genconsole' . DIRECTORY_SEPARATOR;
$this->cacheFile = 'commands.json';
}
public function onKernelResponse(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$response = $event->getResponse();
$request = $event->getRequest();
if (!$this->needConsoleInjection($request, $response)) {
return;
}
$this->injectToolbar($response);
}
public function needConsoleInjection(Request $request, Response $response)
{
if ($request->isXmlHttpRequest()
|| !$response->headers->has('X-Debug-Token')
|| '3' === substr($response->getStatusCode(), 0, 1)
|| ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
|| 'html' !== $request->getRequestFormat()
) {
return false;
}
return true;
}
protected function injectToolbar(Response $response)
{
if (function_exists('mb_stripos')) {
$posrFunction = 'mb_strripos';
$substrFunction = 'mb_substr';
} else {
$posrFunction = 'strripos';
$substrFunction = 'substr';
}
$content = $response->getContent();
if (false !== $pos = $posrFunction($content, '</body>')) {
$toolbar = "\n".str_replace("\n", '', $this->templating->render(
'Sf2genConsoleBundle:Console:toolbar_js.html.twig',
array(
'commands' => $this->getCommands(),
)
))."\n";
$content = $substrFunction($content, 0, $pos).$toolbar.$substrFunction($content, $pos);
$response->setContent($content);
}
}
protected function getCommands()
{
$commands = $this->getCacheContent();
if ($commands === false) {
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0777);
}
$commands = $this->fetchCommands();
file_put_contents($this->cacheDir . $this->cacheFile, json_encode($commands));
} else {
$commands = json_decode($commands);
}
return $commands;
}
protected function fetchCommands()
{
$application = new Application($this->kernel);
foreach ($this->kernel->getBundles() as $bundle) {
$bundle->registerCommands($application);
}
return array_keys($application->all());
}
protected function getCacheContent()
{
if (is_file($this->cacheDir . $this->cacheFile)){
return file_get_contents($this->cacheDir . $this->cacheFile);
}
return false;
}
}