|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2016 Cloud Creativity Limited |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace CloudCreativity\LaravelJsonApi\Console\Commands; |
| 20 | + |
| 21 | +use Illuminate\Console\GeneratorCommand; |
| 22 | +use Illuminate\Filesystem\Filesystem; |
| 23 | +use Symfony\Component\Console\Input\InputArgument; |
| 24 | +use Symfony\Component\Console\Input\InputOption; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class AbstractGeneratorCommand |
| 28 | + * |
| 29 | + * @package CloudCreativity\LaravelJsonApi |
| 30 | + */ |
| 31 | +abstract class AbstractGeneratorCommand extends GeneratorCommand |
| 32 | +{ |
| 33 | + |
| 34 | + /** |
| 35 | + * The console command name. |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $name; |
| 40 | + |
| 41 | + /** |
| 42 | + * The console command description. |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + protected $description; |
| 47 | + |
| 48 | + /** |
| 49 | + * The type of class being generated. |
| 50 | + * |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + protected $type; |
| 54 | + |
| 55 | + /** |
| 56 | + * Whether the resource type is non-dependent on eloquent. |
| 57 | + * |
| 58 | + * @var boolean |
| 59 | + */ |
| 60 | + protected $isIndependent = false; |
| 61 | + |
| 62 | + /** |
| 63 | + * Whether the resource should use eloquent implementations. |
| 64 | + * |
| 65 | + * @var boolean |
| 66 | + */ |
| 67 | + protected $useEloquent; |
| 68 | + |
| 69 | + /** |
| 70 | + * The folder within the root namespace, where files should be generated. |
| 71 | + * |
| 72 | + * @var string |
| 73 | + */ |
| 74 | + protected $subNamespace; |
| 75 | + |
| 76 | + /** |
| 77 | + * Whether generated files should be grouped by their resource files. |
| 78 | + * |
| 79 | + * @var mixed |
| 80 | + */ |
| 81 | + protected $namespaceByResource; |
| 82 | + |
| 83 | + /** |
| 84 | + * The location of all generator stubs |
| 85 | + * |
| 86 | + * @var string |
| 87 | + */ |
| 88 | + private $stubsDirectory = __DIR__ . '/../../../stubs'; |
| 89 | + |
| 90 | + /** |
| 91 | + * Create a new config clear command instance. |
| 92 | + * |
| 93 | + * @param Filesystem $files |
| 94 | + */ |
| 95 | + public function __construct(Filesystem $files) |
| 96 | + { |
| 97 | + parent::__construct($files); |
| 98 | + |
| 99 | + $this->useEloquent = config('json-api.generator.use-eloquent', true); |
| 100 | + $this->subNamespace = config('json-api.generator.namespace', 'JsonApi'); |
| 101 | + $this->namespaceByResource = config('json-api.generator.by-resource', true); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Build the class with the given name. |
| 106 | + * Remove the base controller import if we are already in base namespace. |
| 107 | + * |
| 108 | + * @param string $name |
| 109 | + * @return string |
| 110 | + */ |
| 111 | + protected function buildClass($name) |
| 112 | + { |
| 113 | + $stub = $this->files->get($this->getStub()); |
| 114 | + |
| 115 | + $this->replaceNamespace($stub, $name) |
| 116 | + ->replaceResourceType($stub, $this->getResourceName()); |
| 117 | + |
| 118 | + return $stub; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Replace the value of the resource type constant |
| 123 | + * |
| 124 | + * @param mixed $stub |
| 125 | + * @param mixed $resource |
| 126 | + * @return $this |
| 127 | + */ |
| 128 | + protected function replaceResourceType(&$stub, $resource) |
| 129 | + { |
| 130 | + $stub = str_replace('dummyResourceType', snake_case($resource, '-'), $stub); |
| 131 | + |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the resource name |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + protected function getResourceName() |
| 141 | + { |
| 142 | + $name = ucwords($this->argument('resource')); |
| 143 | + |
| 144 | + if ($this->namespaceByResource) { |
| 145 | + return str_plural($name); |
| 146 | + } |
| 147 | + |
| 148 | + return $name; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Get the desired class name from the input. |
| 153 | + * |
| 154 | + * @return string |
| 155 | + */ |
| 156 | + protected function getNameInput() |
| 157 | + { |
| 158 | + if (!$this->namespaceByResource) { |
| 159 | + return $this->getResourceName(); |
| 160 | + } |
| 161 | + |
| 162 | + return $this->type; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Get the stub file for the generator. |
| 167 | + * |
| 168 | + * @return string |
| 169 | + */ |
| 170 | + protected function getStub() |
| 171 | + { |
| 172 | + if ($this->isIndependent) { |
| 173 | + return $this->getStubFor('independent'); |
| 174 | + } |
| 175 | + |
| 176 | + if ($this->isEloquent()) { |
| 177 | + return $this->getStubFor('eloquent'); |
| 178 | + } |
| 179 | + |
| 180 | + return $this->getStubFor('abstract'); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Get the stub for specific generator type |
| 185 | + * |
| 186 | + * @param string $implementationType |
| 187 | + * @return string |
| 188 | + */ |
| 189 | + private function getStubFor($implementationType) |
| 190 | + { |
| 191 | + return implode('', [ |
| 192 | + $this->stubsDirectory, |
| 193 | + '/', |
| 194 | + $implementationType, |
| 195 | + '/', |
| 196 | + lcfirst($this->type), |
| 197 | + '.stub', |
| 198 | + ]); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Determine whether a resource is eloquent or not |
| 203 | + * |
| 204 | + * @return boolean |
| 205 | + */ |
| 206 | + private function isEloquent() |
| 207 | + { |
| 208 | + if ($this->isIndependent) { |
| 209 | + return false; |
| 210 | + } |
| 211 | + |
| 212 | + if ($this->option('no-eloquent')) { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + return $this->option('eloquent') ?: $this->useEloquent; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Get the default namespace for the class. |
| 221 | + * |
| 222 | + * @param string $rootNamespace |
| 223 | + * @return string |
| 224 | + */ |
| 225 | + protected function getDefaultNamespace($rootNamespace) |
| 226 | + { |
| 227 | + $namespace = [ |
| 228 | + $rootNamespace, // #0 |
| 229 | + '\\', // #1 |
| 230 | + $this->subNamespace, // #2 |
| 231 | + '\\', // #3 |
| 232 | + $this->getResourceName() // #4 |
| 233 | + ]; |
| 234 | + |
| 235 | + if (!$this->namespaceByResource) { |
| 236 | + $namespace[4] = str_plural($this->type); |
| 237 | + } |
| 238 | + |
| 239 | + return implode('', $namespace); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Get the console command arguments. |
| 244 | + * |
| 245 | + * @return array |
| 246 | + */ |
| 247 | + protected function getArguments() |
| 248 | + { |
| 249 | + return [ |
| 250 | + ['resource', InputArgument::REQUIRED, "The resource for which a {$this->type} class will be generated"], |
| 251 | + ]; |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Get the console command options. |
| 256 | + * |
| 257 | + * @return array |
| 258 | + */ |
| 259 | + protected function getOptions() |
| 260 | + { |
| 261 | + if ($this->isIndependent) { |
| 262 | + return []; |
| 263 | + } |
| 264 | + |
| 265 | + return [ |
| 266 | + ['eloquent', 'e', InputOption::VALUE_NONE, 'Use eloquent as adapter'], |
| 267 | + ['no-eloquent', 'ne', InputOption::VALUE_NONE, 'Use an abstract adapter'], |
| 268 | + ]; |
| 269 | + } |
| 270 | +} |
0 commit comments