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