1 | <?php |
||
2 | |||
3 | namespace ProtoneMedia\LaravelTracer; |
||
4 | |||
5 | use Illuminate\Http\Request; |
||
6 | use Illuminate\Routing\Route; |
||
7 | use Illuminate\Support\Collection; |
||
8 | |||
9 | class QualifiedRoute |
||
10 | { |
||
11 | /** |
||
12 | * Name of the qualified route. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private $name; |
||
17 | |||
18 | /** |
||
19 | * Seconds between the logs of the qualified route. |
||
20 | * |
||
21 | * @var bool|int|null |
||
22 | */ |
||
23 | private $secondsBetweenLogs; |
||
24 | |||
25 | /** |
||
26 | * Setups this instance. |
||
27 | * |
||
28 | * @param \Illuminate\Routing\Route $route |
||
29 | * @param string $name |
||
30 | * @param bool|int|null $secondsBetweenLogs |
||
31 | */ |
||
32 | public function __construct(Route $route, string $name, $secondsBetweenLogs = null) |
||
33 | { |
||
34 | $this->name = $this->replaceParameters($name, $route->parameters()); |
||
35 | $this->secondsBetweenLogs = is_null($secondsBetweenLogs) ? config('laravel-tracer.seconds_between_logs') : $secondsBetweenLogs; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Replaces the parameters within the qualified route with |
||
40 | * the actual given parameters (of the route). |
||
41 | * |
||
42 | * @param string $name |
||
43 | * @param array $parameters |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | private function replaceParameters(string $name, array $parameters): string |
||
48 | { |
||
49 | Collection::make($parameters)->map(function ($value, $parameter) use (&$name) { |
||
50 | $name = str_replace("{{$parameter}}", $value, $name); |
||
51 | }); |
||
52 | |||
53 | return $name; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns a new instance based on the given request. |
||
58 | * |
||
59 | * @param \Illuminate\Http\Request $request |
||
60 | * @return $this |
||
61 | */ |
||
62 | public static function fromRequest(Request $request) |
||
63 | { |
||
64 | return new static( |
||
65 | $route = $request->route(), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
66 | $name = $route->getName() ?: $request->path() |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Getter for the name |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function name(): string |
||
76 | { |
||
77 | return $this->name; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Getter fot the seconds between logs. |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function secondsBetweenLogs() |
||
86 | { |
||
87 | return $this->secondsBetweenLogs; |
||
88 | } |
||
89 | } |
||
90 |