|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\WebProfilerBundle\Controller; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\ContainerAware; |
|
|
15 |
use Symfony\Component\HttpFoundation\Response; |
|
|
16 |
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
|
17 |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* ProfilerController. |
|
|
21 |
* |
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
23 |
*/ |
|
|
24 |
class ProfilerController extends ContainerAware |
|
|
25 |
{ |
|
|
26 |
/** |
|
|
27 |
* Renders a profiler panel for the given token. |
|
|
28 |
* |
|
|
29 |
* @param string $token The profiler token |
|
|
30 |
* |
|
|
31 |
* @return Response A Response instance |
|
|
32 |
*/ |
|
|
33 |
public function panelAction($token) |
|
|
34 |
{ |
|
|
35 |
$profiler = $this->container->get('profiler'); |
|
|
36 |
$profiler->disable(); |
|
|
37 |
|
|
|
38 |
$panel = $this->container->get('request')->query->get('panel', 'request'); |
|
|
39 |
|
|
|
40 |
if (!$profile = $profiler->loadProfile($token)) { |
|
|
41 |
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound.html.twig', array('token' => $token)); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
if (!$profile->hasCollector($panel)) { |
|
|
45 |
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token)); |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
return $this->container->get('templating')->renderResponse($this->getTemplateName($profiler, $panel), array( |
|
|
49 |
'token' => $token, |
|
|
50 |
'profile' => $profile, |
|
|
51 |
'collector' => $profile->getCollector($panel), |
|
|
52 |
'panel' => $panel, |
|
|
53 |
'templates' => $this->getTemplates($profiler), |
|
|
54 |
)); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* Exports data for a given token. |
|
|
59 |
* |
|
|
60 |
* @param string $token The profiler token |
|
|
61 |
* |
|
|
62 |
* @return Response A Response instance |
|
|
63 |
*/ |
|
|
64 |
public function exportAction($token) |
|
|
65 |
{ |
|
|
66 |
$profiler = $this->container->get('profiler'); |
|
|
67 |
$profiler->disable(); |
|
|
68 |
|
|
|
69 |
if (!$profile = $profiler->loadProfile($token)) { |
|
|
70 |
throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token)); |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
return new Response($profiler->export($profile), 200, array( |
|
|
74 |
'Content-Type' => 'text/plain', |
|
|
75 |
'Content-Disposition' => 'attachment; filename= '.$token.'.txt', |
|
|
76 |
)); |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Purges all tokens. |
|
|
81 |
* |
|
|
82 |
* @return Response A Response instance |
|
|
83 |
*/ |
|
|
84 |
public function purgeAction() |
|
|
85 |
{ |
|
|
86 |
$profiler = $this->container->get('profiler'); |
|
|
87 |
$profiler->disable(); |
|
|
88 |
$profiler->purge(); |
|
|
89 |
|
|
|
90 |
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => '-'))); |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
/** |
|
|
94 |
* Imports token data. |
|
|
95 |
* |
|
|
96 |
* @return Response A Response instance |
|
|
97 |
*/ |
|
|
98 |
public function importAction() |
|
|
99 |
{ |
|
|
100 |
$profiler = $this->container->get('profiler'); |
|
|
101 |
$profiler->disable(); |
|
|
102 |
|
|
|
103 |
$file = $this->container->get('request')->files->get('file'); |
|
|
104 |
if (!$file || UPLOAD_ERR_OK !== $file->getError()) { |
|
|
105 |
throw new \RuntimeException('Problem uploading the data.'); |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
if (!$profile = $profiler->import(file_get_contents($file->getPath()))) { |
|
|
109 |
throw new \RuntimeException('Problem uploading the data (token already exists).'); |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $profile->getToken()))); |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* Renders the Web Debug Toolbar. |
|
|
117 |
* |
|
|
118 |
* @param string $token The profiler token |
|
|
119 |
* @param string $position The toolbar position (bottom, normal, or null -- automatically guessed) |
|
|
120 |
* |
|
|
121 |
* @return Response A Response instance |
|
|
122 |
*/ |
|
|
123 |
public function toolbarAction($token, $position = null) |
|
|
124 |
{ |
|
|
125 |
$request = $this->container->get('request'); |
|
|
126 |
|
|
|
127 |
if (null !== $session = $request->getSession()) { |
|
|
128 |
// keep current flashes for one more request |
|
|
129 |
$session->setFlashes($session->getFlashes()); |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
if (null === $token) { |
|
|
133 |
return new Response(); |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
$profiler = $this->container->get('profiler'); |
|
|
137 |
$profiler->disable(); |
|
|
138 |
|
|
|
139 |
if (!$profile = $profiler->loadProfile($token)) { |
|
|
140 |
return new Response(); |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
if (null === $position) { |
|
|
144 |
$position = false === strpos($this->container->get('request')->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute'; |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
$url = null; |
|
|
148 |
try { |
|
|
149 |
$url = $this->container->get('router')->generate('_profiler', array('token' => $token)); |
|
|
150 |
} catch (\Exception $e) { |
|
|
151 |
// the profiler is not enabled |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array( |
|
|
155 |
'position' => $position, |
|
|
156 |
'profile' => $profile, |
|
|
157 |
'templates' => $this->getTemplates($profiler), |
|
|
158 |
'profiler_url' => $url, |
|
|
159 |
'verbose' => $this->container->get('web_profiler.debug_toolbar')->isVerbose() |
|
|
160 |
)); |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
/** |
|
|
164 |
* Renders the profiler search bar. |
|
|
165 |
* |
|
|
166 |
* @return Response A Response instance |
|
|
167 |
*/ |
|
|
168 |
public function searchBarAction() |
|
|
169 |
{ |
|
|
170 |
$profiler = $this->container->get('profiler'); |
|
|
171 |
$profiler->disable(); |
|
|
172 |
|
|
|
173 |
if (null === $session = $this->container->get('request')->getSession()) { |
|
|
174 |
$ip = |
|
|
175 |
$url = |
|
|
176 |
$limit = |
|
|
177 |
$token = null; |
|
|
178 |
} else { |
|
|
179 |
$ip = $session->get('_profiler_search_ip'); |
|
|
180 |
$url = $session->get('_profiler_search_url'); |
|
|
181 |
$limit = $session->get('_profiler_search_limit'); |
|
|
182 |
$token = $session->get('_profiler_search_token'); |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array( |
|
|
186 |
'token' => $token, |
|
|
187 |
'ip' => $ip, |
|
|
188 |
'url' => $url, |
|
|
189 |
'limit' => $limit, |
|
|
190 |
)); |
|
|
191 |
} |
|
|
192 |
|
|
|
193 |
/** |
|
|
194 |
* Search results. |
|
|
195 |
* |
|
|
196 |
* @param string $token The token |
|
|
197 |
* @return Response A Response instance |
|
|
198 |
*/ |
|
|
199 |
public function searchResultsAction($token) |
|
|
200 |
{ |
|
|
201 |
$profiler = $this->container->get('profiler'); |
|
|
202 |
$profiler->disable(); |
|
|
203 |
|
|
|
204 |
$profile = $profiler->loadProfile($token); |
|
|
205 |
|
|
|
206 |
$ip = $this->container->get('request')->query->get('ip'); |
|
|
207 |
$url = $this->container->get('request')->query->get('url'); |
|
|
208 |
$limit = $this->container->get('request')->query->get('limit'); |
|
|
209 |
|
|
|
210 |
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array( |
|
|
211 |
'token' => $token, |
|
|
212 |
'profile' => $profile, |
|
|
213 |
'tokens' => $profiler->find($ip, $url, $limit), |
|
|
214 |
'ip' => $ip, |
|
|
215 |
'url' => $url, |
|
|
216 |
'limit' => $limit, |
|
|
217 |
'panel' => null, |
|
|
218 |
)); |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
/** |
|
|
222 |
* Narrow the search bar. |
|
|
223 |
* |
|
|
224 |
* @return Response A Response instance |
|
|
225 |
*/ |
|
|
226 |
public function searchAction() |
|
|
227 |
{ |
|
|
228 |
$profiler = $this->container->get('profiler'); |
|
|
229 |
$profiler->disable(); |
|
|
230 |
|
|
|
231 |
$request = $this->container->get('request'); |
|
|
232 |
|
|
|
233 |
$ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip')); |
|
|
234 |
$url = $request->query->get('url'); |
|
|
235 |
$limit = $request->query->get('limit'); |
|
|
236 |
$token = $request->query->get('token'); |
|
|
237 |
|
|
|
238 |
if (null !== $session = $request->getSession()) { |
|
|
239 |
$session->set('_profiler_search_ip', $ip); |
|
|
240 |
$session->set('_profiler_search_url', $url); |
|
|
241 |
$session->set('_profiler_search_limit', $limit); |
|
|
242 |
$session->set('_profiler_search_token', $token); |
|
|
243 |
} |
|
|
244 |
|
|
|
245 |
if (!empty($token)) { |
|
|
246 |
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token))); |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
$tokens = $profiler->find($ip, $url, $limit); |
|
|
250 |
|
|
|
251 |
return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array( |
|
|
252 |
'token' => $tokens ? $tokens[0]['token'] : 'empty', |
|
|
253 |
'ip' => $ip, |
|
|
254 |
'url' => $url, |
|
|
255 |
'limit' => $limit, |
|
|
256 |
))); |
|
|
257 |
} |
|
|
258 |
|
|
|
259 |
protected function getTemplateNames($profiler) |
|
|
260 |
{ |
|
|
261 |
$templates = array(); |
|
|
262 |
foreach ($this->container->getParameter('data_collector.templates') as $id => $arguments) { |
|
|
263 |
if (null === $arguments) { |
|
|
264 |
continue; |
|
|
265 |
} |
|
|
266 |
|
|
|
267 |
list($name, $template) = $arguments; |
|
|
268 |
if (!$profiler->has($name)) { |
|
|
269 |
continue; |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
if ('.html.twig' === substr($template, -10)) { |
|
|
273 |
$template = substr($template, 0, -10); |
|
|
274 |
} |
|
|
275 |
|
|
|
276 |
if (!$this->container->get('templating')->exists($template.'.html.twig')) { |
|
|
277 |
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name)); |
|
|
278 |
} |
|
|
279 |
|
|
|
280 |
$templates[$name] = $template.'.html.twig'; |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
return $templates; |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
protected function getTemplateName($profiler, $panel) |
|
|
287 |
{ |
|
|
288 |
$templates = $this->getTemplateNames($profiler); |
|
|
289 |
|
|
|
290 |
if (!isset($templates[$panel])) { |
|
|
291 |
throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel)); |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
return $templates[$panel]; |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
protected function getTemplates($profiler) |
|
|
298 |
{ |
|
|
299 |
$templates = $this->getTemplateNames($profiler); |
|
|
300 |
foreach ($templates as $name => $template) { |
|
|
301 |
$templates[$name] = $this->container->get('twig')->loadTemplate($template); |
|
|
302 |
} |
|
|
303 |
|
|
|
304 |
return $templates; |
|
|
305 |
} |
|
|
306 |
} |