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