Skip to content

Commit

Permalink
add stack trace to Gate watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
paras-malhotra committed Dec 5, 2018
1 parent 72a14ab commit e31d4e7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 35 deletions.
20 changes: 19 additions & 1 deletion public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50567,7 +50567,25 @@ var render = function() {
]
)
])
])
]),
_vm._v(" "),
slotProps.entry.content.file
? _c("tr", [
_c("td", { staticClass: "table-fit font-weight-bold" }, [
_vm._v("Location")
]),
_vm._v(" "),
_c("td", [
_vm._v(
"\n " +
_vm._s(slotProps.entry.content.file) +
":" +
_vm._s(slotProps.entry.content.line) +
"\n "
)
])
])
: _vm._e()
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=8b839cf52d8d2895ec73",
"/app.js": "/app.js?id=cfc93f63ab192bcfa68d",
"/app.css": "/app.css?id=7c1ff2a14db6cf79b2c3",
"/app-dark.css": "/app-dark.css?id=3499432c9dbc93b0f541"
}
7 changes: 7 additions & 0 deletions resources/js/screens/gates/preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
</span>
</td>
</tr>

<tr v-if="slotProps.entry.content.file">
<td class="table-fit font-weight-bold">Location</td>
<td>
{{slotProps.entry.content.file}}:{{slotProps.entry.content.line}}
</td>
</tr>
</template>

<div slot="after-attributes-card" slot-scope="slotProps">
Expand Down
40 changes: 40 additions & 0 deletions src/Watchers/FetchesStackTrace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Laravel\Telescope\Watchers;

use Illuminate\Support\Str;

trait FetchesStackTrace
{
/**
* Find the first frame in the stack trace outside of Telescope/Laravel.
*
* @return array
*/
protected function getCallerFromStackTrace()
{
$trace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))->forget(0);

return $trace->first(function ($frame) {
if (! isset($frame['file'])) {
return false;
}

return ! Str::contains($frame['file'],
base_path('vendor'.DIRECTORY_SEPARATOR.$this->ignoredVendorPath())
);
});
}

/**
* Choose the frame outside of either Telescope/Laravel or all packages.
*
* @return string|null
*/
protected function ignoredVendorPath()
{
if (! ($this->options['ignore_packages'] ?? true)) {
return 'laravel';
}
}
}
6 changes: 6 additions & 0 deletions src/Watchers/GateWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class GateWatcher extends Watcher
{
use FetchesStackTrace;

/**
* Register the watcher.
*
Expand All @@ -36,10 +38,14 @@ public function recordGateCheck(?Authenticatable $user, $ability, $result, $argu
return;
}

$caller = $this->getCallerFromStackTrace();

Telescope::recordGate(IncomingEntry::make([
'ability' => $ability,
'result' => $result ? 'allowed' : 'denied',
'arguments' => $arguments,
'file' => $caller['file'],
'line' => $caller['line'],
]));

return $result;
Expand Down
35 changes: 2 additions & 33 deletions src/Watchers/QueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Laravel\Telescope\Watchers;

use Illuminate\Support\Str;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\IncomingEntry;
use Illuminate\Database\Events\QueryExecuted;

class QueryWatcher extends Watcher
{
use FetchesStackTrace;

/**
* Register the watcher.
*
Expand Down Expand Up @@ -68,36 +69,4 @@ protected function formatBindings($event)
{
return $event->connection->prepareBindings($event->bindings);
}

/**
* Find the first frame in the stack trace outside of Telescope/Laravel.
*
* @return array
*/
protected function getCallerFromStackTrace()
{
$trace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))->forget(0);

return $trace->first(function ($frame) {
if (! isset($frame['file'])) {
return false;
}

return ! Str::contains($frame['file'],
base_path('vendor'.DIRECTORY_SEPARATOR.$this->ignoredVendorPath())
);
});
}

/**
* Choose the frame outside of either Telescope/Laravel or all packages.
*
* @return string|null
*/
protected function ignoredVendorPath()
{
if (! ($this->options['ignore_packages'] ?? true)) {
return 'laravel';
}
}
}

0 comments on commit e31d4e7

Please sign in to comment.