1 <?php
2
3 namespace Scaffolder\Compilers\Core;
4
5 use Illuminate\Support\Facades\File;
6 use Scaffolder\Compilers\AbstractCoreCompiler;
7 use Scaffolder\Support\FileToCompile;
8 use Scaffolder\Support\PathParser;
9 use stdClass;
10
11 class ModelCompiler extends AbstractCoreCompiler
12 {
13 14 15 16 17 18 19 20 21 22 23 24 25
26 public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, array $extensions, $extra = null)
27 {
28 if (File::exists(base_path('scaffolder-config/cache/model_' . $hash . self::CACHE_EXT)))
29 {
30 return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
31 }
32 else
33 {
34 $this->stub = $stub;
35
36 $this->replaceNamespace($scaffolderConfig)
37 ->replaceNamespaceModelExtend($scaffolderConfig)
38 ->replaceClassName($modelName)
39 ->setPrimaryKey($modelData)
40 ->addFillable($modelData)
41 ->replaceTableName($scaffolderConfig, $modelName);
42
43 foreach ($extensions as $extension)
44 {
45 $this->stub = $extension->runAfterModelIsCompiled($this->stub, $modelData, $scaffolderConfig);
46 }
47
48 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
49 }
50 }
51
52 53 54 55 56 57 58 59 60 61
62 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
63 {
64 $path = PathParser::parse($scaffolderConfig->paths->models) . $modelName . '.php';
65
66
67 if ($fileToCompile->cached)
68 {
69 File::copy(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
70 }
71 else
72 {
73 File::put(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
74 File::copy(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
75 }
76
77 return $path;
78 }
79
80 81 82 83 84 85 86
87 private function replaceNamespaceModelExtend(stdClass $scaffolderConfig)
88 {
89 $this->stub = str_replace('{{namespace_model_extend}}', $scaffolderConfig->inheritance->model, $this->stub);
90
91 return $this;
92 }
93
94 95 96 97 98 99 100
101 protected function addFillable($modelData)
102 {
103 $fields = '';
104 $firstIteration = true;
105
106 foreach ($modelData->fields as $field)
107 {
108 if ($firstIteration)
109 {
110 $fields .= sprintf("'%s'," . PHP_EOL, $field->name);
111 $firstIteration = false;
112 }
113 else
114 {
115 $fields .= sprintf($this->tab(2) . "'%s'," . PHP_EOL, $field->name);
116 }
117 }
118
119 $this->stub = str_replace('{{fillable}}', $fields, $this->stub);
120
121 return $this;
122 }
123
124 125 126 127 128 129 130 131
132 protected function replaceTableName(stdClass $scaffolderConfig, $modelName)
133 {
134 $tableName = isset($scaffolderConfig->tableName) && !empty($scaffolderConfig->tableName) ? $scaffolderConfig->tableName : $modelName . 's';
135
136 $this->stub = str_replace('{{table_name}}', strtolower($tableName), $this->stub);
137
138 return $this;
139 }
140
141 142 143 144 145 146 147
148 protected function setPrimaryKey($modelData)
149 {
150 $primaryKey = '// Using default primary key' . PHP_EOL;
151
152 foreach ($modelData->fields as $field)
153 {
154 if ($field->index == 'primary')
155 {
156 $primaryKey = 'protected $primaryKey = \'' . $field->name . '\';' . PHP_EOL;
157 break;
158 }
159 }
160
161 $this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub);
162
163 return $this;
164 }
165 }
166