ghaskell /
scaffold
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Ghaskell\Scaffold; |
||
| 4 | |||
| 5 | use Ghaskell\Scaffold\Facades\Vibro; |
||
| 6 | use Ghaskell\Scaffold\ScaffoldException; |
||
| 7 | use Illuminate\Database\Eloquent\Collection; |
||
| 8 | use Illuminate\Filesystem\Filesystem; |
||
| 9 | use Illuminate\Support\Str; |
||
| 10 | |||
| 11 | class Scaffold |
||
| 12 | { |
||
| 13 | protected $model; |
||
| 14 | protected $migration; |
||
| 15 | protected $variables; |
||
| 16 | protected $migrationFileName; |
||
| 17 | protected $files; |
||
| 18 | |||
| 19 | public $messages = []; |
||
| 20 | public $created = []; |
||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | public function __construct($migration) |
||
|
0 ignored issues
–
show
|
|||
| 25 | { |
||
| 26 | |||
| 27 | |||
| 28 | } |
||
| 29 | |||
| 30 | public static function make($migrationName) |
||
| 31 | { |
||
| 32 | $scaffold = new Scaffold($migrationName); |
||
| 33 | $scaffold->files = new Filesystem(); |
||
| 34 | $scaffold->files->requireOnce($migrationName); |
||
| 35 | $migrationInstance = $scaffold->resolve($name = $scaffold->getMigrationName($migrationName)); //parse into instance |
||
| 36 | $reflector = new \ReflectionClass($migrationInstance); //reflect |
||
| 37 | $migrationFileName = $reflector->getFileName(); |
||
| 38 | $scaffold->migration = $scaffold->files->get($migrationFileName); |
||
| 39 | $scaffold->model = ModelPattern::make($scaffold->migration); |
||
| 40 | return $scaffold; |
||
| 41 | |||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Resolve a migration instance from a file. |
||
| 47 | * |
||
| 48 | * @param string $file |
||
| 49 | * @return object |
||
| 50 | */ |
||
| 51 | public function resolve($file) |
||
| 52 | { |
||
| 53 | $class = Str::studly(implode('_', array_slice(explode('_', $file), 4))); |
||
| 54 | |||
| 55 | return new $class; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the name of the migration. |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function getMigrationName($path) |
||
| 65 | { |
||
| 66 | return str_replace('.php', '', basename($path)); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get all of the migration files in a given path. |
||
| 71 | * |
||
| 72 | * @param string|array $paths |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function getMigrationFiles($paths) |
||
| 76 | { |
||
| 77 | return Collection::make($paths)->flatMap(function ($path) { |
||
| 78 | return $this->files->glob($path.'/*_*.php'); |
||
| 79 | })->filter()->sortBy(function ($file) { |
||
| 80 | return $this->getMigrationName($file); |
||
| 81 | })->values()->keyBy(function ($file) { |
||
| 82 | return $this->getMigrationName($file); |
||
| 83 | })->all(); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | public function build($stub, $data) |
||
| 89 | { |
||
| 90 | $path = $this->getStubPath($stub); |
||
| 91 | return Vibro::compileFile($path, $data); |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function getStub($stub) |
||
| 95 | { |
||
| 96 | $stub = Str::camel($stub); |
||
| 97 | if($this->files->exists(app_path("Scaffold/stubs/$stub.stub"))) { |
||
| 98 | return $this->files->get(app_path("Scaffold/stubs/$stub.stub")); |
||
| 99 | } else { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function getStubPath($stub) |
||
| 105 | { |
||
| 106 | $stub = Str::camel($stub); |
||
| 107 | return app_path("Scaffold/stubs/$stub.stub"); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | public function addRoutes() |
||
| 112 | { |
||
| 113 | foreach(config('scaffold.routes') as $key => $route) |
||
| 114 | { |
||
| 115 | $namespace = Str::studly($key); |
||
| 116 | $routeContent = $this->files->get($route['fileName']); |
||
| 117 | $uri = Str::plural(strtolower(preg_replace( |
||
| 118 | '/([a-zA-Z])(?=[A-Z])/', |
||
| 119 | '$1-', $this->model->name |
||
| 120 | ))); |
||
| 121 | |||
| 122 | |||
| 123 | if($key === 'web') { |
||
| 124 | $routeString = "\n\nRoute::resource('$uri', '$namespace\\{$this->model->name}Controller')->only(['index', 'show']);"; |
||
| 125 | } else { |
||
| 126 | $routeString = "\n\nRoute::apiResource('$uri', '$namespace\\{$this->model->name}Controller');"; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (!str_contains($routeContent, $routeString)) { |
||
| 130 | $this->files->append($route['fileName'], $routeString); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function generate($file) |
||
| 137 | { |
||
| 138 | $target = Str::camel(str_replace('build', '', $file)); |
||
| 139 | $config = config("scaffold.files.$target"); |
||
| 140 | if(empty($config)) { |
||
| 141 | throw new ScaffoldException("Configuration for $file not found"); |
||
| 142 | } |
||
| 143 | if(array_key_exists('dedependencies', $config)) { |
||
| 144 | foreach($config['dependencies'] as $dependency) { |
||
| 145 | $this->generate($dependency); //recursively generate dependencies |
||
| 146 | } |
||
| 147 | } |
||
| 148 | $pathPrefix = $config['path']; |
||
| 149 | $variables = ['model' => $this->model]; |
||
| 150 | |||
| 151 | if(!empty(config("scaffold.variables"))) { |
||
| 152 | foreach (config("scaffold.variables") as $variable => $value) { |
||
| 153 | $variables[$variable] = $this->compileVariable($value); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $fileName = Vibro::compileFileName($config['fileNamePattern'], $variables); |
||
| 158 | $pathPrefix = base_path(Vibro::compileFileName("$pathPrefix", $variables)); |
||
| 159 | if($this->files->exists("$pathPrefix/$fileName")) { |
||
| 160 | $this->messages[] = "File '$fileName' exists and was not overwritten."; |
||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | if(!$this->files->isDirectory($pathPrefix)) { |
||
| 165 | $this->files->makeDirectory("$pathPrefix"); |
||
| 166 | } |
||
| 167 | |||
| 168 | $content = $this->build($target, $variables); |
||
| 169 | if($content) { |
||
| 170 | $this->files->put("$pathPrefix/$fileName", $content); |
||
| 171 | $this->created[] = "$pathPrefix/$fileName"; |
||
| 172 | } else { |
||
| 173 | $this->messages[] = "File stub $target not found."; |
||
| 174 | } |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | |||
| 178 | } |
||
| 179 | |||
| 180 | public function compileVariable($string) { |
||
| 181 | $model = $this->model; |
||
|
0 ignored issues
–
show
|
|||
| 182 | ob_start(); |
||
| 183 | eval( "echo $string;"); |
||
| 184 | $output = ob_get_contents(); |
||
| 185 | ob_end_clean(); |
||
| 186 | |||
| 187 | return $output; |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.