1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaffolder\Compilers\Blade; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Scaffolder\Compilers\AbstractViewCompiler; |
7
|
|
|
use Scaffolder\Compilers\Support\FileToCompile; |
8
|
|
|
use Scaffolder\Compilers\Support\InputTypeResolverTrait; |
9
|
|
|
use Scaffolder\Compilers\Support\PathParser; |
10
|
|
|
use Scaffolder\Support\Directory; |
11
|
|
|
|
12
|
|
View Code Duplication |
class EditViewCompiler extends AbstractViewCompiler |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
use InputTypeResolverTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Compiles the edit view. |
18
|
|
|
* |
19
|
|
|
* @param $stub |
20
|
|
|
* @param $modelName |
21
|
|
|
* @param $modelData |
22
|
|
|
* @param $scaffolderConfig |
23
|
|
|
* @param $hash |
24
|
|
|
* @param null $extra |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function compile($stub, $modelName, $modelData, $scaffolderConfig, $hash, $extra = null) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
if (File::exists(base_path('scaffolder-config/cache/view_edit_' . $hash . self::CACHE_EXT))) |
31
|
|
|
{ |
32
|
|
|
return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash)); |
33
|
|
|
} |
34
|
|
|
else |
35
|
|
|
{ |
36
|
|
|
$this->stub = $stub; |
37
|
|
|
|
38
|
|
|
return $this->replaceClassName($modelName) |
|
|
|
|
39
|
|
|
->replaceBreadcrumb($modelName) |
40
|
|
|
->addFields($modelData) |
41
|
|
|
->replacePrimaryKey($modelData) |
42
|
|
|
->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix) |
43
|
|
|
->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash)); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Store the compiled stub. |
49
|
|
|
* |
50
|
|
|
* @param $modelName |
51
|
|
|
* @param $scaffolderConfig |
52
|
|
|
* @param $compiled |
53
|
|
|
* @param FileToCompile $fileToCompile |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile) |
58
|
|
|
{ |
59
|
|
|
$folder = PathParser::parse($scaffolderConfig->generator->paths->views) . strtolower($modelName) ; |
60
|
|
|
|
61
|
|
|
// create folder directory |
62
|
|
|
Directory::createIfNotExists($folder, 0755, true); |
63
|
|
|
|
64
|
|
|
$path = $folder . '/edit.blade.php'; |
65
|
|
|
|
66
|
|
|
// Store in cache |
67
|
|
|
if ($fileToCompile->cached) |
68
|
|
|
{ |
69
|
|
|
File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path); |
70
|
|
|
} |
71
|
|
|
else |
72
|
|
|
{ |
73
|
|
|
File::put(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $compiled); |
74
|
|
|
File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $path; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add fields to the edit view. |
82
|
|
|
* |
83
|
|
|
* @param $modelData |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
private function addFields($modelData) |
88
|
|
|
{ |
89
|
|
|
$fields = ''; |
90
|
|
|
$firstIteration = true; |
91
|
|
|
|
92
|
|
|
foreach ($modelData->fields as $field) |
93
|
|
|
{ |
94
|
|
|
if ($firstIteration) |
95
|
|
|
{ |
96
|
|
|
$fields .= sprintf(self::getInputFor($field) . PHP_EOL, $field->name); |
97
|
|
|
$firstIteration = false; |
98
|
|
|
} |
99
|
|
|
else |
100
|
|
|
{ |
101
|
|
|
$fields .= sprintf("\t" . self::getInputFor($field) . PHP_EOL, $field->name); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->stub = str_replace('{{fields}}', $fields, $this->stub); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Replace the prefix. |
112
|
|
|
* |
113
|
|
|
* @param $prefix |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
private function replaceRoutePrefix($prefix) |
118
|
|
|
{ |
119
|
|
|
$this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Replace the primary key. |
126
|
|
|
* |
127
|
|
|
* @param $modelData |
128
|
|
|
*/ |
129
|
|
|
private function replacePrimaryKey($modelData) |
130
|
|
|
{ |
131
|
|
|
$primaryKey = 'id'; |
132
|
|
|
|
133
|
|
|
foreach ($modelData->fields as $field) |
134
|
|
|
{ |
135
|
|
|
if ($field->index == 'primary') |
136
|
|
|
{ |
137
|
|
|
$primaryKey = $field->name; |
138
|
|
|
break; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
} |