|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\ViewXray; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
class XrayMiddleware |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** @var Xray */ |
|
11
|
|
|
private $xray; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(Xray $xray) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->xray = $xray; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handle an incoming request. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Request $request |
|
22
|
|
|
* @param Closure $next |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
public function handle($request, Closure $next) |
|
26
|
|
|
{ |
|
27
|
|
|
if (! $this->xray->isEnabled()) { |
|
28
|
|
|
return $next($request); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->xray->boot(); |
|
32
|
|
|
|
|
33
|
|
|
/** @var \Illuminate\Http\Response $response */ |
|
34
|
|
|
$response = $next($request); |
|
35
|
|
|
|
|
36
|
|
|
if ($response->isRedirection()) { |
|
37
|
|
|
return $response; |
|
38
|
|
|
} elseif ( |
|
39
|
|
|
($response->headers->has('Content-Type') && |
|
40
|
|
|
strpos($response->headers->get('Content-Type'), 'html') === false) |
|
41
|
|
|
|| $request->getRequestFormat() !== 'html' |
|
42
|
|
|
|| $response->getContent() === false |
|
43
|
|
|
) { |
|
44
|
|
|
return $response; |
|
45
|
|
|
} elseif (is_null($response->exception) && !is_null($this->xray->getBaseView())) { |
|
46
|
|
|
// Modify the response to add the Debugbar |
|
47
|
|
|
$this->injectXrayBar($response); |
|
48
|
|
|
} |
|
49
|
|
|
return $response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the route information for a given route. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Illuminate\Routing\Route $route |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getRouteInformation($route) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!is_a($route, 'Illuminate\Routing\Route')) { |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
$uri = head($route->methods()) . ' ' . $route->uri(); |
|
64
|
|
|
$action = $route->getAction(); |
|
65
|
|
|
|
|
66
|
|
|
$result = [ |
|
67
|
|
|
'uri' => $uri ?: '-', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$result = array_merge($result, $action); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
if (isset($action['controller']) && strpos($action['controller'], '@') !== false) { |
|
74
|
|
|
list($controller, $method) = explode('@', $action['controller']); |
|
75
|
|
|
if(class_exists($controller) && method_exists($controller, $method)) { |
|
76
|
|
|
$reflector = new \ReflectionMethod($controller, $method); |
|
77
|
|
|
} |
|
78
|
|
|
unset($result['uses']); |
|
79
|
|
|
} elseif (isset($action['uses']) && $action['uses'] instanceof \Closure) { |
|
80
|
|
|
$reflector = new \ReflectionFunction($action['uses']); |
|
81
|
|
|
$result['uses'] = $result['uses']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (isset($reflector)) { |
|
85
|
|
|
$filename = ltrim(str_replace(base_path(), '', $reflector->getFileName()), '/'); |
|
86
|
|
|
$result['file'] = $filename . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function injectXrayBar($response) |
|
93
|
|
|
{ |
|
94
|
|
|
$routeInformation = $this->getRouteInformation(app('router')->current()); |
|
95
|
|
|
|
|
96
|
|
|
$content = $response->getContent(); |
|
97
|
|
|
|
|
98
|
|
|
$xrayJs = file_get_contents(__DIR__.'/../resources/assets/xray.js'); |
|
99
|
|
|
$xrayCss = file_get_contents(__DIR__.'/../resources/assets/xray.css'); |
|
100
|
|
|
$xrayBar = view('xray::xray', [ |
|
101
|
|
|
'routeInformation' => $routeInformation, |
|
102
|
|
|
'viewPath' => str_replace(base_path(), '', $this->xray->getBaseView()->getPath()), |
|
103
|
|
|
'viewName' => $this->xray->getBaseView()->name(), |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
|
|
$renderedContent = '<script>'.$xrayJs.'</script><style>'.$xrayCss.'</style>'.$xrayBar; |
|
107
|
|
|
|
|
108
|
|
|
$pos = strripos($content, '</body>'); |
|
109
|
|
|
if (false !== $pos) { |
|
110
|
|
|
$content = substr($content, 0, $pos) . $renderedContent . substr($content, $pos); |
|
111
|
|
|
} else { |
|
112
|
|
|
$content = $content . $renderedContent; |
|
113
|
|
|
} |
|
114
|
|
|
// Update the new content and reset the content length |
|
115
|
|
|
$response->setContent($content); |
|
116
|
|
|
$response->headers->remove('Content-Length'); |
|
117
|
|
|
} |
|
118
|
|
|
} |