1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
8
|
|
|
use Illuminate\Support\Facades\View; |
9
|
|
|
|
10
|
|
|
class UpdateViewNameSolution implements RunnableSolution |
11
|
|
|
{ |
12
|
|
|
private $missingView; |
13
|
|
|
private $suggestedView; |
14
|
|
|
|
15
|
|
|
public function __construct($missingView = null, $suggestedView = null, $controllerPath = null) |
16
|
|
|
{ |
17
|
|
|
$this->missingView = $missingView; |
18
|
|
|
$this->suggestedView = $suggestedView; |
19
|
|
|
$this->controllerPath = $controllerPath; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getSolutionTitle(): string |
23
|
|
|
{ |
24
|
|
|
return $this->missingView . ' was not found.'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getDocumentationLinks(): array |
28
|
|
|
{ |
29
|
|
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getSolutionActionDescription(): string |
33
|
|
|
{ |
34
|
|
|
return 'Did you mean `' . $this->suggestedView . '`?'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getRunButtonText(): string |
38
|
|
|
{ |
39
|
|
|
return 'Update view name'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSolutionDescription(): string |
43
|
|
|
{ |
44
|
|
|
return ''; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getRunParameters(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'missingView' => $this->missingView, |
51
|
|
|
'suggestedView' => $this->suggestedView, |
52
|
|
|
'controllerPath' => $this->controllerPath |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isRunnable() |
57
|
|
|
{ |
58
|
|
|
return $this->updateViewName($this->getRunParameters()) !== false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function run(array $parameters = []) |
62
|
|
|
{ |
63
|
|
|
$output = $this->updateViewName($parameters); |
64
|
|
|
if ($output !== false) { |
65
|
|
|
file_put_contents(app_path() . $parameters['controllerPath'], $output); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function updateViewName(array $parameters = []) |
70
|
|
|
{ |
71
|
|
|
if (strpos($parameters['controllerPath'], 'ignition/tests/Solutions') !== false) { |
72
|
|
|
$file = $parameters['controllerPath']; |
73
|
|
|
} else { |
74
|
|
|
$file = app_path() . $parameters['controllerPath']; |
75
|
|
|
} |
76
|
|
|
if (! is_file($file)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
$contents = file_get_contents($file); |
80
|
|
|
$tokens = token_get_all($contents); |
81
|
|
|
$expectedTokens = collect($tokens)->map(function($token) use ($parameters) { |
82
|
|
|
if ($token[0] === T_CONSTANT_ENCAPSED_STRING && ( |
83
|
|
|
$token[1] == "'" . $parameters['missingView'] . "'" || |
84
|
|
|
$token[1] == '"' . $parameters['missingView'] . '"' |
85
|
|
|
)) { |
86
|
|
|
$token[1] = "'" . $parameters['suggestedView'] . "'"; |
87
|
|
|
} |
88
|
|
|
return $token; |
89
|
|
|
})->toArray(); |
90
|
|
|
|
91
|
|
|
$newContents = collect($expectedTokens)->map(function($token) { |
92
|
|
|
return is_array($token) ? $token[1] : $token; |
93
|
|
|
})->implode(''); |
94
|
|
|
|
95
|
|
|
$newTokens = token_get_all($newContents); |
96
|
|
|
|
97
|
|
|
// If we're generating a file that doesn't mean the same thing then don't allow |
98
|
|
|
if ($expectedTokens !== $newTokens) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
return $newContents; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|