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 RouteCompiler 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 $this->stub = $stub;
29
30 $this->replaceResource($modelName);
31
32 return $this->stub;
33 }
34
35 36 37 38 39 40 41 42 43
44 public function compileGroup($stub, $compiledRoutes, stdClass $scaffolderConfig)
45 {
46 $this->stub = $stub;
47
48 $this->replaceRoutes($compiledRoutes)
49 ->replacePrefix($scaffolderConfig->routing->prefix)
50 ->store(null, $scaffolderConfig, $this->stub, new FileToCompile(null, null));
51
52 return $this->stub;
53 }
54
55 56 57 58 59 60 61 62 63 64
65 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
66 {
67 File::append(PathParser::parse($scaffolderConfig->paths->routes), PHP_EOL . $compiled);
68 }
69
70 71 72 73 74 75 76
77 protected function replaceResource($modelName)
78 {
79 $this->stub = str_replace('{{resource_lw}}', strtolower($modelName), $this->stub);
80 $this->stub = str_replace('{{resource}}', $modelName, $this->stub);
81
82 return $this;
83 }
84
85 86 87 88 89 90 91
92 protected function replaceRoutes($compiledRoutes)
93 {
94 $this->stub = str_replace('{{routes}}', $compiledRoutes, $this->stub);
95
96 return $this;
97 }
98
99 100 101 102 103 104 105
106 private function replacePrefix($prefix)
107 {
108 $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
109
110 return $this;
111 }
112 }
113