1 <?php
2
3 namespace Scaffolder\Compilers\Core\Api;
4
5 use Illuminate\Support\Facades\File;
6 use Scaffolder\Compilers\Core\ControllerCompiler;
7 use Scaffolder\Support\FileToCompile;
8 use stdClass;
9
10 class ApiControllerCompiler extends ControllerCompiler
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_controller_' . $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 ->setValidations($modelData);
37
38 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
39 }
40 }
41
42 43 44 45 46 47 48 49 50 51
52 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
53 {
54 $path = base_path('../' . strtolower(str_replace(' ', '-', $scaffolderConfig->name . '-api'))) . '/app/Http/Controllers/' . $modelName . 'Controller.php';
55
56
57 if ($fileToCompile->cached)
58 {
59 File::copy(base_path('scaffolder-config/cache/api_controller_' . $fileToCompile->hash . self::CACHE_EXT), $path);
60 }
61 else
62 {
63 File::put(base_path('scaffolder-config/cache/api_controller_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
64 File::copy(base_path('scaffolder-config/cache/api_controller_' . $fileToCompile->hash . self::CACHE_EXT), $path);
65 }
66
67 return $path;
68 }
69 }
70