1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationTracy;
9:
10: use Nette;
11: use Nette\Application\Routers;
12: use Nette\Application\UI\Presenter;
13: use Tracy;
14: use Tracy\Dumper;
15:
16:
17: 18: 19:
20: class RoutingPanel extends Nette\Object implements Tracy\IBarPanel
21: {
22:
23: private $router;
24:
25:
26: private $httpRequest;
27:
28:
29: private $presenterFactory;
30:
31:
32: private $routers = array();
33:
34:
35: private $request;
36:
37:
38: private $source;
39:
40:
41: public static function initializePanel(Nette\Application\Application $application)
42: {
43: Tracy\Debugger::getBlueScreen()->addPanel(function ($e) use ($application) {
44: return $e ? NULL : array(
45: 'tab' => 'Nette Application',
46: 'panel' => '<h3>Requests</h3>' . Dumper::toHtml($application->getRequests(), array(Dumper::LIVE => TRUE))
47: . '<h3>Presenter</h3>' . Dumper::toHtml($application->getPresenter(), array(Dumper::LIVE => TRUE)),
48: );
49: });
50: }
51:
52:
53: public function __construct(Nette\Application\IRouter $router, Nette\Http\IRequest $httpRequest, Nette\Application\IPresenterFactory $presenterFactory)
54: {
55: $this->router = $router;
56: $this->httpRequest = $httpRequest;
57: $this->presenterFactory = $presenterFactory;
58: }
59:
60:
61: 62: 63: 64:
65: public function getTab()
66: {
67: $this->analyse($this->router);
68: ob_start(function () {});
69: $request = $this->request;
70: require __DIR__ . '/templates/RoutingPanel.tab.phtml';
71: return ob_get_clean();
72: }
73:
74:
75: 76: 77: 78:
79: public function getPanel()
80: {
81: ob_start(function () {});
82: $request = $this->request;
83: $routers = $this->routers;
84: $source = $this->source;
85: $hasModule = (bool) array_filter($routers, function($rq) { return $rq['module']; });
86: $url = $this->httpRequest->getUrl();
87: $method = $this->httpRequest->getMethod();
88: require __DIR__ . '/templates/RoutingPanel.panel.phtml';
89: return ob_get_clean();
90: }
91:
92:
93: 94: 95: 96: 97:
98: private function analyse($router, $module = '')
99: {
100: if ($router instanceof Routers\RouteList) {
101: foreach ($router as $subRouter) {
102: $this->analyse($subRouter, $module . $router->getModule());
103: }
104: return;
105: }
106:
107: $matched = 'no';
108: $request = $router->match($this->httpRequest);
109: if ($request) {
110: $request->setPresenterName($module . $request->getPresenterName());
111: $matched = 'may';
112: if (empty($this->request)) {
113: $this->request = $request;
114: $this->findSource();
115: $matched = 'yes';
116: }
117: }
118:
119: $this->routers[] = array(
120: 'matched' => $matched,
121: 'class' => get_class($router),
122: 'defaults' => $router instanceof Routers\Route || $router instanceof Routers\SimpleRouter ? $router->getDefaults() : array(),
123: 'mask' => $router instanceof Routers\Route ? $router->getMask() : NULL,
124: 'request' => $request,
125: 'module' => rtrim($module, ':'),
126: );
127: }
128:
129:
130: private function findSource()
131: {
132: $request = $this->request;
133: $presenter = $request->getPresenterName();
134: try {
135: $class = $this->presenterFactory->getPresenterClass($presenter);
136: } catch (Nette\Application\InvalidPresenterException $e) {
137: return;
138: }
139: $rc = new \ReflectionClass($class);
140:
141: if ($rc->isSubclassOf('Nette\Application\UI\Presenter')) {
142: if ($request->getParameter(Presenter::SIGNAL_KEY)) {
143: $method = $class::formatSignalMethod($request->getParameter(Presenter::SIGNAL_KEY));
144:
145: } elseif ($request->getParameter(Presenter::ACTION_KEY)) {
146: $action = $request->getParameter(Presenter::ACTION_KEY);
147: $method = $class::formatActionMethod($action);
148: if (!$rc->hasMethod($method)) {
149: $method = $class::formatRenderMethod($action);
150: }
151: }
152: }
153:
154: $this->source = isset($method) && $rc->hasMethod($method) ? $rc->getMethod($method) : $rc;
155: }
156:
157: }
158: