10000 [WebProfileBundle] remove dependency on the DIC · s7ntech/symfony@a3b3c28 · GitHub
[go: up one dir, main page]

Skip to content

Commit a3b3c28

Browse files
committed
[WebProfileBundle] remove dependency on the DIC
The controllers are not relying on the DIC anymore and only Twig is used for rendering (instead of the Templating component). The Exception controller has not been updated yet as it relies on many external dependencies (and other bundles).
1 parent dfc53b0 commit a3b3c28

File tree

9 files changed

+127
-81
lines changed

9 files changed

+127
-81
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,37 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAware;
1514
use Symfony\Component\HttpFoundation\Response;
1615
use Symfony\Component\HttpFoundation\RedirectResponse;
1716
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17+
use Symfony\Component\HttpKernel\Profiler\Profiler;
1818
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2122

2223
/**
2324
* ProfilerController.
2425
*
2526
* @author Fabien Potencier <fabien@symfony.com>
2627
*/
27-
class ProfilerController extends ContainerAware
28+
class ProfilerController
2829
{
29-
protected $templateManager;
30+
private $templateManager;
31+
private $generator;
32+
private $profiler;
33+
private $twig;
34+
private $templates;
35+
private $toolbarPosition;
36+
37+
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
38+
{
39+
$this->generator = $generator;
40+
$this->profiler = $profiler;
41+
$this->twig = $twig;
42+
$this->templates = $templates;
43+
$this->toolbarPosition = $toolbarPosition;
44+
}
3045

3146
/**
3247
* Renders a profiler panel for the given token.
@@ -38,21 +53,20 @@ class ProfilerController extends ContainerAware
3853
*/
3954
public function panelAction(Request $request, $token)
4055
{
41-
$profiler = $this->container->get('profiler');
42-
$profiler->disable();
56+
$this->profiler->disable();
4357

4458
$panel = $request->query->get('panel', 'request');
4559
$page = $request->query->get('page', 'home');
4660

47-
if (!$profile = $profiler->loadProfile($token)) {
48-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:info.html.twig', array('about' => 'no_token', 'token' => $token));
61+
if (!$profile = $this->profiler->loadProfile($token)) {
62+
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array('about' => 'no_token', 'token' => $token)));
4963
}
5064

5165
if (!$profile->hasCollector($panel)) {
5266
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
5367
}
5468

55-
return $this->container->get('templating')->renderResponse($this->getTemplateManager()->getName($profile, $panel), array(
69+
return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
5670
'token' => $token,
5771
'profile' => $profile,
5872
'collector' => $profile->getCollector($panel),
@@ -61,7 +75,9 @@ public function panelAction(Request $request, $token)
6175
'request' => $request,
6276
'templates' => $this->getTemplateManager()->getTemplates($profile),
6377
'is_ajax' => $request->isXmlHttpRequest(),
64-
));
78+
// for BC compatibility
79+
'app' => array('request' => $request),
80+
)));
6581
}
6682

6783
/**
@@ -73,14 +89,13 @@ public function panelAction(Request $request, $token)
7389
*/
7490
public function exportAction($token)
7591
{
76-
$profiler = $this->container->get('profiler');
77-
$profiler->disable();
92+
$this->profiler->disable();
7893

79-
if (!$profile = $profiler->loadProfile($token)) {
94+
if (!$profile = $this->profiler->loadProfile($token)) {
8095
throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
8196
}
8297

83-
return new Response($profiler->export($profile), 200, array(
98+
return new Response($this->profiler->export($profile), 200, array(
8499
'Content-Type' => 'text/plain',
85100
'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
86101
));
@@ -93,11 +108,10 @@ public function exportAction($token)
93108
*/
94109
public function purgeAction()
95110
{
96-
$profiler = $this->container->get('profiler');
97-
$profiler->disable();
98-
$profiler->purge();
111+
$this->profiler->disable();
112+
$this->profiler->purge();
99113

100-
return new RedirectResponse($this->container->get('router')->generate('_profiler_info', array('about' => 'purge')));
114+
return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'purge')));
101115
}
102116

103117
/**
@@ -107,22 +121,19 @@ public function purgeAction()
107121
*/
108122
public function importAction(Request $request)
109123
{
110-
$profiler = $this->container->get('profiler');
111-
$profiler->disable();
112-
113-
$router = $this->container->get('router');
124+
$this->profiler->disable();
114125

115126
$file = $request->files->get('file');
116127

117128
if (empty($file) || !$file->isValid()) {
118129
return new RedirectResponse($router->generate('_profiler_info', array('about' => 'upload_error')));
119130
}
120131

121-
if (!$profile = $profiler->import(file_get_contents($file->getPathname()))) {
122-
return new RedirectResponse($router->generate('_profiler_info', array('about' => 'already_exists')));
132+
if (!$profile = $this->profiler->import(file_get_contents($file->getPathname()))) {
133+
return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'already_exists')));
123134
}
124135

125-
return new RedirectResponse($router->generate('_profiler', array('token' => $profile->getToken())));
136+
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $profile->getToken())));
126137
}
127138

128139
/**
@@ -134,12 +145,11 @@ public function importAction(Request $request)
134145
*/
135146
public function infoAction($about)
136147
{
137-
$profiler = $this->container->get('profiler');
138-
$profiler->disable();
148+
$this->profiler->disable();
139149

140-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:info.html.twig', array(
150+
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
141151
'about' => $about
142-
));
152+
)));
143153
}
144154

145155
/**
@@ -164,31 +174,30 @@ public function toolbarAction(Request $request, $token, $position = null)
164174
return new Response();
165175
}
166176

167-
$profiler = $this->container->get('profiler');
168-
$profiler->disable();
177+
$this->profiler->disable();
169178

170-
if (!$profile = $profiler->loadProfile($token)) {
179+
if (!$profile = $this->profiler->loadProfile($token)) {
171180
return new Response();
172181
}
173182

174183
if (null === $position) {
175-
$position = $this->container->getParameter('web_profiler.debug_toolbar.position');
184+
$position = $this->toolbarPosition;
176185
}
177186

178187
$url = null;
179188
try {
180-
$url = $this->container->get('router')->generate('_profiler', array('token' => $token));
189+
$url = $this->generator->generate('_profiler', array('token' => $token));
181190
} catch (\Exception $e) {
182191
// the profiler is not enabled
183192
}
184193

185-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
194+
return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
186195
'position' => $position,
187196
'profile' => $profile,
188197
'templates' => $this->getTemplateManager()->getTemplates($profile),
189198
'profiler_url' => $url,
190199
'token' => $token,
191-
));
200+
)));
192201
}
193202

194203
/**
@@ -200,8 +209,7 @@ public function toolbarAction(Request $request, $token, $position = null)
200209
*/
201210
public function searchBarAction(Request $request)
202211
{
203-
$profiler = $this->container->get('profiler');
204-
$profiler->disable();
212+
$this->profiler->disable();
205213

206214
if (null === $session = $request->getSession()) {
207215
$ip =
@@ -217,13 +225,13 @@ public function searchBarAction(Request $request)
217225
$token = $session->get('_profiler_search_token');
218226
}
219227

220-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
228+
return new Response($this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
221229
'token' => $token,
222230
'ip' => $ip,
223231
'method' => $method,
224232
'url' => $url,
225233
'limit' => $limit,
226-
));
234+
)));
227235
}
228236

229237
/**
@@ -236,26 +244,25 @@ public function searchBarAction(Request $request)
236244
*/
237245
public function searchResultsAction(Request $request, $token)
238246
{
239-
$profiler = $this->container->get('profiler');
240-
$profiler->disable();
247+
$this->profiler->disable();
241248

242-
$profile = $profiler->loadProfile($token);
249+
$profile = $this->profiler->loadProfile($token);
243250

244251
$ip = $request->query->get('ip');
245252
$method = $request->query->get('method');
246253
$url = $request->query->get('url');
247254
$limit = $request->query->get('limit');
248255

249-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
256+
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
250257
'token' => $token,
251258
'profile' => $profile,
252-
'tokens' => $profiler->find($ip, $url, $limit, $method),
259+
'tokens' => $this->profiler->find($ip, $url, $limit, $method),
253260
'ip' => $ip,
254261
'method' => $method,
255262
'url' => $url,
256263
'limit' => $limit,
257264
'panel' => null,
258-
));
265+
)));
259266
}
260267

261268
/**
@@ -267,8 +274,7 @@ public function searchResultsAction(Request $request, $token)
267274
*/
268275
public function searchAction(Request $request)
269276
{
270-
$profiler = $this->container->get('profiler');
271-
$profiler->disable();
277+
$this->profiler->disable();
272278

273279
$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
274280
$method = $request->query->get('method');
@@ -285,12 +291,12 @@ public function searchAction(Request $request)
285291
}
286292

287293
if (!empty($token)) {
288-
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
294+
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)));
289295
}
290296

291-
$tokens = $profiler->find($ip, $url, $limit, $method);
297+
$tokens = $this->profiler->find($ip, $url, $limit, $method);
292298

293-
return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array(
299+
return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
294300
'token' => $tokens ? $tokens[0]['token'] : 'empty',
295301
'ip' => $ip,
296302
'method' => $method,
@@ -306,8 +312,7 @@ public function searchAction(Request $request)
306312
*/
307313
public function phpinfoAction()
308314
{
309-
$profiler = $this->container->get('profiler');
310-
$profiler->disable();
315+
$this->profiler->disable();
311316

312317
ob_start();
313318
phpinfo();
@@ -319,11 +324,7 @@ public function phpinfoAction()
319324
protected function getTemplateManager()
320325
{
321326
if (null === $this->templateManager) {
322-
$this->templateManager = new TemplateManager(
323-
$this->container->get('profiler'),
324-
$this->container->get('twig'),
325-
$this->container->getParameter('data_collector.templates')
326-
);
327+
$this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
327328
}
328329

329330
return $this->templateManager;

src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAware;
1514
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
1616
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
17+
use Symfony\Component\Routing\RouterInterface;
18+
use Symfony\Component\HttpKernel\Profiler\Profiler;
1719

1820
/**
1921
* RouterController.
2022
*
2123
* @author Fabien Potencier <fabien@symfony.com>
2224
*/
23-
class RouterController extends ContainerAware
25+
class RouterController
2426
{
27+
private $profiler;
28+
private $twig;
29+
private $matcher;
30+
private $routes;
31+
32+
public function __construct(Profiler $profiler, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, $routes = null)
33+
{
34+
$this->profiler = $profiler;
35+
$this->twig = $twig;
36+
$this->matcher = $matcher;
37+
$this->routes = $routes;
38+
39+
if (null === $this->routes && null !== $this->matcher && $this->matcher instanceof RouterInterface) {
40+
$this->routes = $matcher->getRouteCollection();
41+
}
42+
}
43+
2544
/**
2645
* Renders the profiler panel for the given token.
2746
*
@@ -31,26 +50,24 @@ class RouterController extends ContainerAware
3150
*/
3251
public function panelAction($token)
3352
{
34-
$profiler = $this->container->get('profiler');
35-
$profiler->disable();
53+
$this->profiler->disable();
3654

37-
if (!$this->container->has('router')) {
55+
if (null === $this->matcher || null === $this->routes) {
3856
return new Response('The Router is not enabled.');
3957
}
40-
$router = $this->container->get('router');
4158

42-
$profile = $profiler->loadProfile($token);
59+
$profile = $this->profiler->loadProfile($token);
4360

44-
$context = $router->getContext();
61+
$context = $this->matcher->getContext();
4562
$context->setMethod($profile->getMethod());
46-
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context);
63+
$matcher = new TraceableUrlMatcher($this->routes, $context);
4764

4865
$request = $profile->getCollector('request');
4966

50-
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Router:panel.html.twig', array(
67+
return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
5168
'request' => $request,
5269
'router' => $profile->getCollector('router'),
5370
'traces' => $matcher->getTraces($request->getPathInfo()),
54-
));
71+
)));
5572
}
5673
}

src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function load(array $configs, ContainerBuilder $container)
4343
$config = $this->processConfiguration($configuration, $configs);
4444

4545
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46+
$loader->load('profiler.xml');
4647
$loader->load('toolbar.xml');
4748

4849
$container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']);

0 commit comments

Comments
 (0)
0