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