1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
4
|
|
|
|
5
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
6
|
|
|
|
7
|
|
|
class UpdateViewNameSolution implements RunnableSolution |
8
|
|
|
{ |
9
|
|
|
private $missingView; |
10
|
|
|
private $suggestedView; |
11
|
|
|
|
12
|
|
|
public function __construct($missingView = null, $suggestedView = null, $controllerPath = null) |
13
|
|
|
{ |
14
|
|
|
$this->missingView = $missingView; |
15
|
|
|
$this->suggestedView = $suggestedView; |
16
|
|
|
$this->controllerPath = $controllerPath; |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getSolutionTitle(): string |
20
|
|
|
{ |
21
|
|
|
return $this->missingView.' was not found.'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getDocumentationLinks(): array |
25
|
|
|
{ |
26
|
|
|
return []; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getSolutionActionDescription(): string |
30
|
|
|
{ |
31
|
|
|
return 'Did you mean `'.$this->suggestedView.'`?'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getRunButtonText(): string |
35
|
|
|
{ |
36
|
|
|
return 'Update view name'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getSolutionDescription(): string |
40
|
|
|
{ |
41
|
|
|
return ''; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getRunParameters(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
'missingView' => $this->missingView, |
48
|
|
|
'suggestedView' => $this->suggestedView, |
49
|
|
|
'controllerPath' => $this->controllerPath, |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function isRunnable() |
54
|
|
|
{ |
55
|
|
|
return $this->updateViewName($this->getRunParameters()) !== false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function run(array $parameters = []) |
59
|
|
|
{ |
60
|
|
|
$output = $this->updateViewName($parameters); |
61
|
|
|
if ($output !== false && $output !== '') { |
62
|
|
|
file_put_contents(app_path().$parameters['controllerPath'], $output); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function updateViewName(array $parameters = []) |
67
|
|
|
{ |
68
|
|
|
if (strpos($parameters['controllerPath'], 'ignition/tests/stubs') !== false) { |
69
|
|
|
$file = $parameters['controllerPath']; |
70
|
|
|
} else { |
71
|
|
|
$file = app_path().$parameters['controllerPath']; |
72
|
|
|
} |
73
|
|
|
if (! is_file($file)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
$contents = file_get_contents($file); |
77
|
|
|
$tokens = token_get_all($contents); |
78
|
|
|
|
79
|
|
|
$contents = $this->getProposedFileFromTokens( |
80
|
|
|
$tokens, |
81
|
|
|
$parameters['missingView'], |
82
|
|
|
$parameters['suggestedView'] |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
if ($contents === false) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $contents; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getProposedFileFromTokens(array $tokens, string $missingView, string $suggestedView) |
93
|
|
|
{ |
94
|
|
|
$viewKey = 0; |
95
|
|
|
$expectedTokens = collect($tokens)->map(function ($token, $key) use ($missingView, $suggestedView, &$viewKey) { |
96
|
|
|
if ($token[0] == T_STRING && $token[1] == 'view') { |
97
|
|
|
$viewKey = $key; |
98
|
|
|
} |
99
|
|
|
if ($token[0] === T_CONSTANT_ENCAPSED_STRING && ($viewKey == ($key - 2)) && ( |
100
|
|
|
$token[1] == "'$missingView'" || |
101
|
|
|
$token[1] == '"'.$missingView.'"' |
102
|
|
|
)) { |
103
|
|
|
$token[1] = "'$suggestedView'"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $token; |
107
|
|
|
})->toArray(); |
108
|
|
|
$newContents = collect($expectedTokens)->map(function ($token) { |
109
|
|
|
return is_array($token) ? $token[1] : $token; |
110
|
|
|
})->implode(''); |
111
|
|
|
|
112
|
|
|
$newTokens = token_get_all($newContents); |
113
|
|
|
|
114
|
|
|
if ($expectedTokens !== $newTokens) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $newContents; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: