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 CreateViewCompiler 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_create_' . $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 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
45
46 $this->stub = $themeExtension->runAfterCreateViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
47
48 foreach ($extensions as $extension)
49 {
50 $this->stub = $extension->runAfterCreateViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
51 }
52
53 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
54 }
55 }
56
57 58 59 60 61 62 63 64 65 66
67 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
68 {
69 $path = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) . '/create.blade.php';
70
71
72 if ($fileToCompile->cached)
73 {
74 File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
75 }
76 else
77 {
78 File::put(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
79 File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
80 }
81
82 return $path;
83 }
84
85 86 87 88 89 90 91
92 private function addFields($modelData)
93 {
94 $fields = '';
95 $firstIteration = true;
96
97 foreach ($modelData->fields as $field)
98 {
99 if ($firstIteration)
100 {
101 $fields .= self::getInputFor($field) . PHP_EOL;
102 $firstIteration = false;
103 }
104 else
105 {
106 $fields .= $this->tab(1) . self::getInputFor($field) . PHP_EOL;
107 }
108 }
109
110 $this->stub = str_replace('{{fields}}', $fields, $this->stub);
111
112 return $this;
113 }
114
115 116 117 118 119 120 121
122 private function replaceRoutePrefix($prefix)
123 {
124 $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
125
126 return $this;
127 }
128 }
129