Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\LaravelIgnition\Recorders\QueryRecorder;
4
 
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Database\Events\QueryExecuted;
7
 
8
class QueryRecorder
9
{
10
    /** @var \Spatie\LaravelIgnition\Recorders\QueryRecorder\Query[] */
11
    protected array $queries = [];
12
 
13
    protected Application $app;
14
 
15
    protected bool $reportBindings = true;
16
 
17
    protected ?int $maxQueries;
18
 
19
    public function __construct(
20
        Application $app,
21
        bool $reportBindings = true,
22
        ?int $maxQueries = 200
23
    ) {
24
        $this->app = $app;
25
        $this->reportBindings = $reportBindings;
26
        $this->maxQueries = $maxQueries;
27
    }
28
 
29
    public function start(): self
30
    {
31
        /** @phpstan-ignore-next-line  */
32
        $this->app['events']->listen(QueryExecuted::class, [$this, 'record']);
33
 
34
        return $this;
35
    }
36
 
37
    public function record(QueryExecuted $queryExecuted): void
38
    {
39
        $this->queries[] = Query::fromQueryExecutedEvent($queryExecuted, $this->reportBindings);
40
 
41
        if (is_int($this->maxQueries)) {
42
            $this->queries = array_slice($this->queries, -$this->maxQueries);
43
        }
44
    }
45
 
46
    /**
47
     * @return array<int, array<string, mixed>>
48
     */
49
    public function getQueries(): array
50
    {
51
        $queries = [];
52
 
53
        foreach ($this->queries as $query) {
54
            $queries[] = $query->toArray();
55
        }
56
 
57
        return $queries;
58
    }
59
 
60
    public function reset(): void
61
    {
62
        $this->queries = [];
63
    }
64
 
65
    public function getReportBindings(): bool
66
    {
67
        return $this->reportBindings;
68
    }
69
 
70
    public function setReportBindings(bool $reportBindings): self
71
    {
72
        $this->reportBindings = $reportBindings;
73
 
74
        return $this;
75
    }
76
 
77
    public function getMaxQueries(): ?int
78
    {
79
        return $this->maxQueries;
80
    }
81
 
82
    public function setMaxQueries(?int $maxQueries): self
83
    {
84
        $this->maxQueries = $maxQueries;
85
 
86
        return $this;
87
    }
88
}