|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the lucid-console project. |
| 5 | + * |
| 6 | + |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Lucid\Console\Commands; |
| 13 | + |
| 14 | +use Lucid\Console\Finder; |
| 15 | +use Illuminate\Filesystem\Filesystem; |
| 16 | +use Illuminate\Console\GeneratorCommand; |
| 17 | +use Illuminate\Config\Repository as Config; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Abed Halawi <[email protected]> |
| 24 | + */ |
| 25 | +class ServiceMakeCommand extends GeneratorCommand |
| 26 | +{ |
| 27 | + use Finder; |
| 28 | + |
| 29 | + /** |
| 30 | + * The base namespace for this command. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private $namespace; |
| 35 | + |
| 36 | + /** |
| 37 | + * The Services path |
| 38 | + * |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + private $path; |
| 42 | + |
| 43 | + /** |
| 44 | + * The console command name. |
| 45 | + * |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + protected $name = 'make:service'; |
| 49 | + |
| 50 | + /** |
| 51 | + * The console command description. |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + protected $description = 'Create a new Service'; |
| 56 | + |
| 57 | + /** |
| 58 | + * The type of class being generated. |
| 59 | + * |
| 60 | + * @var string |
| 61 | + */ |
| 62 | + protected $type = 'Service'; |
| 63 | + |
| 64 | + /** |
| 65 | + * The directories to be created under the service directory. |
| 66 | + * |
| 67 | + * @var array |
| 68 | + */ |
| 69 | + protected $directories = [ |
| 70 | + 'Console/', |
| 71 | + 'Database/', |
| 72 | + 'Database/Migrations/', |
| 73 | + 'Database/Seeds/', |
| 74 | + 'Http/', |
| 75 | + 'Http/Controllers/', |
| 76 | + 'Http/Middleware/', |
| 77 | + 'Http/Requests/', |
| 78 | + 'Providers/', |
| 79 | + 'resources/', |
| 80 | + 'resources/lang/', |
| 81 | + 'resources/views/', |
| 82 | + ]; |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the stub file for the generator. |
| 86 | + * |
| 87 | + * @return string |
| 88 | + */ |
| 89 | + protected function getStub() |
| 90 | + { |
| 91 | + return __DIR__.'/stubs/service.stub'; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Execute the console command. |
| 96 | + * |
| 97 | + * @return bool|null |
| 98 | + */ |
| 99 | + public function fire() |
| 100 | + { |
| 101 | + $name = studly_case($this->getNameInput()); |
| 102 | + |
| 103 | + $slug = snake_case($name); |
| 104 | + |
| 105 | + $path = $this->getPath($name); |
| 106 | + |
| 107 | + if ($this->files->exists($path)) { |
| 108 | + $this->error($this->type.' already exists!'); |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // create service directory |
| 114 | + $this->createDirectory($path); |
| 115 | + // create .gitkeep file in it |
| 116 | + $this->createFile($path.'/.gitkeep'); |
| 117 | + |
| 118 | + $this->createServiceDirectories($path); |
| 119 | + |
| 120 | + $this->addServiceProviders($name, $slug, $path); |
| 121 | + |
| 122 | + $this->addRoutesFile($name, $slug, $path); |
| 123 | + |
| 124 | + $this->addWelcomeViewFile($path); |
| 125 | + |
| 126 | + $this->info('Service '.$name.' created successfully.'."\n"); |
| 127 | + |
| 128 | + $rootNamespace = $this->findRootNamespace(); |
| 129 | + $serviceNamespace = $this->findServiceNamespace($name); |
| 130 | + |
| 131 | + $serviceProvider = $serviceNamespace.'\\Providers\\'.$name.'ServiceProvider'; |
| 132 | + |
| 133 | + $this->info('Activate it by registering '. |
| 134 | + '<comment>'.$serviceProvider.'</comment> '. |
| 135 | + "\n". |
| 136 | + 'in <comment>'.$rootNamespace.'\Foundation\Providers\ServiceProvider@register</comment> '. |
| 137 | + 'with the following:'. |
| 138 | + "\n" |
| 139 | + ); |
| 140 | + |
| 141 | + $this->info('<comment>$this->app->register(\''.$serviceProvider.'\');</comment>'."\n"); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * {@inheritdoc} |
| 146 | + */ |
| 147 | + public function getPath($name) |
| 148 | + { |
| 149 | + return $this->findServicePath($name); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Create an empty directory at the given path. |
| 154 | + * |
| 155 | + * @param string $path |
| 156 | + * |
| 157 | + * @return bool |
| 158 | + */ |
| 159 | + public function createDirectory($path) |
| 160 | + { |
| 161 | + return $this->files->makeDirectory($path, 0755, true, true); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Create a file at the given path with the given contents. |
| 166 | + * |
| 167 | + * @param string $path |
| 168 | + * @param string $contents |
| 169 | + * |
| 170 | + * @return bool |
| 171 | + */ |
| 172 | + public function createFile($path, $contents = '') |
| 173 | + { |
| 174 | + return $this->files->put($path, $contents); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Create the default directories at the given service path. |
| 179 | + * |
| 180 | + * @param string $path |
| 181 | + * |
| 182 | + * @return void |
| 183 | + */ |
| 184 | + public function createServiceDirectories($path) |
| 185 | + { |
| 186 | + foreach ($this->directories as $directory) { |
| 187 | + $this->createDirectory($path.'/'.$directory); |
| 188 | + $this->createFile($path.'/'.$directory.'/.gitkeep'); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Add the corresponding service provider for the created service. |
| 194 | + * |
| 195 | + * @param string $name |
| 196 | + * @param string $path |
| 197 | + * |
| 198 | + * @return bool |
| 199 | + */ |
| 200 | + public function addServiceProviders($name, $slug, $path) |
| 201 | + { |
| 202 | + $namespace = $this->findServiceNamespace($name).'\\Providers'; |
| 203 | + |
| 204 | + $this->createRegistrationServiceProvider($name, $path, $slug, $namespace); |
| 205 | + |
| 206 | + $this->createRouteServiceProvider($name, $path, $slug, $namespace); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Create the service provider that registers this service. |
| 211 | + * |
| 212 | + * @param string $name |
| 213 | + * @param string $path |
| 214 | + */ |
| 215 | + public function createRegistrationServiceProvider($name, $path, $slug, $namespace) |
| 216 | + { |
| 217 | + $content = file_get_contents(__DIR__.'/stubs/serviceprovider.stub'); |
| 218 | + $content = str_replace( |
| 219 | + ['{{name}}', '{{slug}}', '{{namespace}}'], |
| 220 | + [$name, $slug, $namespace], |
| 221 | + $content |
| 222 | + ); |
| 223 | + |
| 224 | + $this->createFile($path.'/Providers/'.$name.'ServiceProvider.php', $content); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Create the routes service provider file. |
| 229 | + * |
| 230 | + * @param string $name |
| 231 | + * @param string $path |
| 232 | + * @param string $slug |
| 233 | + * @param string $namespace |
| 234 | + */ |
| 235 | + public function createRouteServiceProvider($name, $path, $slug, $namespace) |
| 236 | + { |
| 237 | + $serviceNamespace = $this->findServiceNamespace($name); |
| 238 | + $controllers = $serviceNamespace.'\Http\Controllers'; |
| 239 | + $foundation = $this->findRootNamespace().'\Foundation'; |
| 240 | + |
| 241 | + $content = file_get_contents(__DIR__.'/stubs/routeserviceprovider.stub'); |
| 242 | + $content = str_replace( |
| 243 | + ['{{name}}', '{{namespace}}', '{{controllers_namespace}}', '{{foundation_namespace}}'], |
| 244 | + [$name, $namespace, $controllers, $foundation], |
| 245 | + $content |
| 246 | + ); |
| 247 | + |
| 248 | + $this->createFile($path.'/Providers/RouteServiceProvider.php', $content); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Add the routes file. |
| 253 | + * |
| 254 | + * @param string $name |
| 255 | + * @param string $slug |
| 256 | + * @param string $path |
| 257 | + */ |
| 258 | + public function addRoutesFile($name, $slug, $path) |
| 259 | + { |
| 260 | + $controllers = 'src/Services/'.$name.'/Http/Controllers'; |
| 261 | + |
| 262 | + $content = file_get_contents(__DIR__.'/stubs/routes.stub'); |
| 263 | + $content = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $content); |
| 264 | + |
| 265 | + $this->createFile($path.'/Http/routes.php', $content); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Add the welcome view file. |
| 270 | + * |
| 271 | + * @param string $path |
| 272 | + */ |
| 273 | + public function addWelcomeViewFile($path) |
| 274 | + { |
| 275 | + $this->createFile( |
| 276 | + $path.'/resources/views/welcome.blade.php', |
| 277 | + file_get_contents(__DIR__.'/stubs/welcome.blade.stub') |
| 278 | + ); |
| 279 | + } |
| 280 | +} |
0 commit comments