|
8 | 8 | use Illuminate\Contracts\Auth\Access\Gate;
|
9 | 9 | use Illuminate\Contracts\Auth\Authenticatable;
|
10 | 10 | use Illuminate\Database\Eloquent\Model;
|
| 11 | +use Illuminate\Routing\Router; |
11 | 12 | use Symfony\Component\VarDumper\Cloner\VarCloner;
|
12 | 13 | use Illuminate\Support\Str;
|
13 | 14 |
|
14 | 15 | /**
|
15 |
| - * Collector for Laravel's Auth provider |
| 16 | + * Collector for Laravel's gate checks |
16 | 17 | */
|
17 | 18 | class GateCollector extends MessagesCollector
|
18 | 19 | {
|
| 20 | + /** @var int */ |
| 21 | + protected $backtraceLimit = 15; |
| 22 | + |
| 23 | + /** @var array */ |
| 24 | + protected $reflection = []; |
| 25 | + |
| 26 | + /** @var \Illuminate\Routing\Router */ |
| 27 | + protected $router; |
| 28 | + |
19 | 29 | /**
|
20 | 30 | * @param Gate $gate
|
21 | 31 | */
|
22 |
| - public function __construct(Gate $gate) |
| 32 | + public function __construct(Gate $gate, Router $router) |
23 | 33 | {
|
24 | 34 | parent::__construct('gate');
|
| 35 | + $this->router = $router; |
25 | 36 | $this->setDataFormatter(new SimpleFormatter());
|
26 | 37 | $gate->after(function ($user, $ability, $result, $arguments = []) {
|
27 | 38 | $this->addCheck($user, $ability, $result, $arguments);
|
@@ -82,4 +93,90 @@ public function addCheck($user, $ability, $result, $arguments = [])
|
82 | 93 | 'arguments' => $this->getDataFormatter()->formatVar($arguments),
|
83 | 94 | ], $label, false);
|
84 | 95 | }
|
| 96 | + |
| 97 | + /** |
| 98 | + * @param array $stacktrace |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + protected function getStackTraceItem($stacktrace) |
| 103 | + { |
| 104 | + foreach ($stacktrace as $i => $trace) { |
| 105 | + if (!isset($trace['file'])) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + if (str_ends_with($trace['file'], 'Illuminate/Routing/ControllerDispatcher.php')) { |
| 110 | + $trace = $this->findControllerFromDispatcher($trace); |
| 111 | + } elseif (str_starts_with($trace['file'], storage_path())) { |
| 112 | + $hash = pathinfo($trace['file'], PATHINFO_FILENAME); |
| 113 | + |
| 114 | + if ($file = $this->findViewFromHash($hash)) { |
| 115 | + $trace['file'] = $file; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if ($this->fileIsInExcludedPath($trace['file'])) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + return $trace; |
| 124 | + } |
| 125 | + |
| 126 | + return $stacktrace[0]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Find the route action file |
| 131 | + * |
| 132 | + * @param array $trace |
| 133 | + * @return array |
| 134 | + */ |
| 135 | + protected function findControllerFromDispatcher($trace) |
| 136 | + { |
| 137 | + /** @var \Closure|string|array $action */ |
| 138 | + $action = $this->router->current()->getAction('uses'); |
| 139 | + |
| 140 | + if (is_string($action)) { |
| 141 | + [$controller, $method] = explode('@', $action); |
| 142 | + |
| 143 | + $reflection = new \ReflectionMethod($controller, $method); |
| 144 | + $trace['file'] = $reflection->getFileName(); |
| 145 | + $trace['line'] = $reflection->getStartLine(); |
| 146 | + } elseif ($action instanceof \Closure) { |
| 147 | + $reflection = new \ReflectionFunction($action); |
| 148 | + $trace['file'] = $reflection->getFileName(); |
| 149 | + $trace['line'] = $reflection->getStartLine(); |
| 150 | + } |
| 151 | + |
| 152 | + return $trace; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Find the template name from the hash. |
| 157 | + * |
| 158 | + * @param string $hash |
| 159 | + * @return null|array |
| 160 | + */ |
| 161 | + protected function findViewFromHash($hash) |
| 162 | + { |
| 163 | + $finder = app('view')->getFinder(); |
| 164 | + |
| 165 | + if (isset($this->reflection['viewfinderViews'])) { |
| 166 | + $property = $this->reflection['viewfinderViews']; |
| 167 | + } else { |
| 168 | + $reflection = new \ReflectionClass($finder); |
| 169 | + $property = $reflection->getProperty('views'); |
| 170 | + $property->setAccessible(true); |
| 171 | + $this->reflection['viewfinderViews'] = $property; |
| 172 | + } |
| 173 | + |
| 174 | + $xxh128Exists = in_array('xxh128', hash_algos()); |
| 175 | + |
| 176 | + foreach ($property->getValue($finder) as $name => $path) { |
| 177 | + if (($xxh128Exists && hash('xxh128', 'v2' . $path) == $hash) || sha1('v2' . $path) == $hash) { |
| 178 | + return $path; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
85 | 182 | }
|
0 commit comments