1 <?php
2
3 namespace Scaffolder\Compilers\Core\Api;
4
5 use Illuminate\Support\Facades\File;
6 use Scaffolder\Compilers\Core\ModelCompiler;
7 use Scaffolder\Support\FileToCompile;
8 use stdClass;
9
10 class ApiModelCompiler extends ModelCompiler
11 {
12 13 14 15 16 17 18 19 20 21 22 23 24
25 public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, array $extensions, $extra = null)
26 {
27 if (File::exists(base_path('scaffolder-config/cache/api_model_' . $hash . self::CACHE_EXT)))
28 {
29 return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
30 }
31 else
32 {
33 $this->stub = $stub;
34
35 $this->replaceClassName($modelName)
36 ->setPrimaryKey($modelData)
37 ->addFillable($modelData)
38 ->replaceTableName($scaffolderConfig, $modelName);
39
40 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
41 }
42 }
43
44 45 46 47 48 49 50 51 52 53
54 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
55 {
56 $path = base_path('../' . strtolower(str_replace(' ', '-', $scaffolderConfig->name . '-api'))) . '/app/Models/' . $modelName . '.php';
57
58
59 if ($fileToCompile->cached)
60 {
61 File::copy(base_path('scaffolder-config/cache/api_model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
62 }
63 else
64 {
65 File::put(base_path('scaffolder-config/cache/api_model_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
66 File::copy(base_path('scaffolder-config/cache/api_model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
67 }
68
69 return $path;
70 }
71 }
72