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\Solutions;
4
 
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\Str;
7
use Spatie\Ignition\Contracts\RunnableSolution;
8
 
9
class MakeViewVariableOptionalSolution implements RunnableSolution
10
{
11
    protected ?string $variableName;
12
 
13
    protected ?string $viewFile;
14
 
15
    public function __construct(string $variableName = null, string $viewFile = null)
16
    {
17
        $this->variableName = $variableName;
18
 
19
        $this->viewFile = $viewFile;
20
    }
21
 
22
    public function getSolutionTitle(): string
23
    {
24
        return "$$this->variableName is undefined";
25
    }
26
 
27
    public function getDocumentationLinks(): array
28
    {
29
        return [];
30
    }
31
 
32
    public function getSolutionActionDescription(): string
33
    {
34
        $output = [
35
            'Make the variable optional in the blade template.',
36
            "Replace `{{ $$this->variableName }}` with `{{ $$this->variableName ?? '' }}`",
37
        ];
38
 
39
        return implode(PHP_EOL, $output);
40
    }
41
 
42
    public function getRunButtonText(): string
43
    {
44
        return 'Make variable optional';
45
    }
46
 
47
    public function getSolutionDescription(): string
48
    {
49
        return '';
50
    }
51
 
52
    public function getRunParameters(): array
53
    {
54
        return [
55
            'variableName' => $this->variableName,
56
            'viewFile' => $this->viewFile,
57
        ];
58
    }
59
 
60
    /**
61
     * @param array<string, mixed>  $parameters
62
     *
63
     * @return bool
64
     */
65
    public function isRunnable(array $parameters = []): bool
66
    {
67
        return $this->makeOptional($this->getRunParameters()) !== false;
68
    }
69
 
70
    /**
71
     * @param array<string, string> $parameters
72
     *
73
     * @return void
74
     */
75
    public function run(array $parameters = []): void
76
    {
77
        $output = $this->makeOptional($parameters);
78
        if ($output !== false) {
79
            file_put_contents($parameters['viewFile'], $output);
80
        }
81
    }
82
 
83
    protected function isSafePath(string $path): bool
84
    {
85
        if (! Str::startsWith($path, ['/', './'])) {
86
            return false;
87
        }
88
        if (! Str::endsWith($path, '.blade.php')) {
89
            return false;
90
        }
91
 
92
        return true;
93
    }
94
 
95
    /**
96
     * @param array<string, string> $parameters
97
     *
98
     * @return bool|string
99
     */
100
    public function makeOptional(array $parameters = []): bool|string
101
    {
102
        if (! $this->isSafePath($parameters['viewFile'])) {
103
            return false;
104
        }
105
 
106
        $originalContents = (string)file_get_contents($parameters['viewFile']);
107
        $newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents);
108
 
109
        $originalTokens = token_get_all(Blade::compileString($originalContents));
110
        $newTokens = token_get_all(Blade::compileString($newContents));
111
 
112
        $expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName']);
113
 
114
        if ($expectedTokens !== $newTokens) {
115
            return false;
116
        }
117
 
118
        return $newContents;
119
    }
120
 
121
    /**
122
     * @param array<int, mixed> $originalTokens
123
     * @param string $variableName
124
     *
125
     * @return array<int, mixed>
126
     */
127
    protected function generateExpectedTokens(array $originalTokens, string $variableName): array
128
    {
129
        $expectedTokens = [];
130
        foreach ($originalTokens as $token) {
131
            $expectedTokens[] = $token;
132
            if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) {
133
                $expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
134
                $expectedTokens[] = [T_COALESCE, '??', $token[2]];
135
                $expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
136
                $expectedTokens[] = [T_CONSTANT_ENCAPSED_STRING, "''", $token[2]];
137
            }
138
        }
139
 
140
        return $expectedTokens;
141
    }
142
}