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) { |
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/Solutions') !== 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
|
|
|
$expectedTokens = collect($tokens)->map(function ($token) use ($parameters) { |
79
|
|
|
if ($token[0] === T_CONSTANT_ENCAPSED_STRING && ( |
80
|
|
|
$token[1] == "'".$parameters['missingView']."'" || |
81
|
|
|
$token[1] == '"'.$parameters['missingView'].'"' |
82
|
|
|
)) { |
83
|
|
|
$token[1] = "'".$parameters['suggestedView']."'"; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $token; |
87
|
|
|
})->toArray(); |
88
|
|
|
|
89
|
|
|
$newContents = collect($expectedTokens)->map(function ($token) { |
90
|
|
|
return is_array($token) ? $token[1] : $token; |
91
|
|
|
})->implode(''); |
92
|
|
|
|
93
|
|
|
$newTokens = token_get_all($newContents); |
94
|
|
|
|
95
|
|
|
// If we're generating a file that doesn't mean the same thing then don't allow |
96
|
|
|
if ($expectedTokens !== $newTokens) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $newContents; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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: