|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheCodeRepublic\Repository\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +class BothCommand extends Command |
| 8 | +{ |
| 9 | + |
| 10 | + protected $signature = 'make:logic {name : ServiceName (ex: ProductSearchService)}'; |
| 11 | + |
| 12 | + protected $description = 'Create a repository and a service'; |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + parent::__construct(); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + public function handle() |
| 21 | + { |
| 22 | + |
| 23 | + //SERVICE |
| 24 | + $name = $this->argument('name'); |
| 25 | + |
| 26 | + if ( ! file_exists ( $path = base_path ( 'app/Services' ) ) ) |
| 27 | + mkdir($path, 0777, true); |
| 28 | + |
| 29 | + if ( file_exists ( base_path ( "app/Services/{$name}Service.php" ) ) ) { |
| 30 | + $this->error("Service with that name ({$name}) already exists"); |
| 31 | + exit(0); |
| 32 | + } |
| 33 | + |
| 34 | + self::createService($name); |
| 35 | + |
| 36 | + $this->info("Service pattern implemented for model ". $name); |
| 37 | + |
| 38 | + //REPO |
| 39 | + if ( ! file_exists ( $path = base_path ( 'app/Repositories' ) ) ) |
| 40 | + mkdir($path, 0777, true); |
| 41 | + |
| 42 | + if ( file_exists ( base_path ( "app/Repositories/{$name}Repository.php" ) ) ) { |
| 43 | + $this->error("Repository with that name ({$name}) already exists"); |
| 44 | + exit(0); |
| 45 | + } |
| 46 | + |
| 47 | + self::createRepository($name); |
| 48 | + |
| 49 | + $this->info("Repository pattern implemented for model ". $name); |
| 50 | + |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + protected static function getStubs($type) |
| 56 | + { |
| 57 | + return file_get_contents("vendor/thecoderepublic/repository/src/resources/$type.stub"); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + protected static function createService($name) |
| 62 | + { |
| 63 | + $template = str_replace( ['{{serviceName}}'], [$name], self::GetStubs('Service') ); |
| 64 | + file_put_contents(base_path("app/Services/{$name}Service.php"), $template); |
| 65 | + } |
| 66 | + |
| 67 | + protected static function createRepository($name) |
| 68 | + { |
| 69 | + $template = str_replace( ['{{modelName}}'], [$name], self::GetStubs('Repository') ); |
| 70 | + file_put_contents(base_path("app/Repositories/{$name}Repository.php"), $template); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +} |
0 commit comments