Mercurial > packages > magicforger
comparison examples/ExampleGenerator.php.stub @ 4:a20439b1c9d3
Added Model generator and other updates.
| author | luka |
|---|---|
| date | Tue, 27 Jun 2023 20:16:55 -0400 |
| parents | 6468684362c2 |
| children | b0b2e79ad8e6 |
comparison
equal
deleted
inserted
replaced
| 3:6468684362c2 | 4:a20439b1c9d3 |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace App\Console\Commands; | 3 namespace Wizzard\MagicForger\Generator\Controller; |
| 4 | 4 |
| 5 use Symfony\Component\Console\Attribute\AsCommand; | 5 use Symfony\Component\Console\Attribute\AsCommand; |
| 6 use Wizzard\MagicForger\Generator\BaseGenerator; | 6 use Wizzard\MagicForger\Generator\BaseGenerator; |
| 7 use Wizzard\MagicForger\Replacer; | 7 use Wizzard\MagicForger\Replacer; |
| 8 //use Illuminate\Console\Concerns\CreatesMatchingTest; | 8 use Illuminate\Support\Str; |
| 9 | 9 |
| 10 #[AsCommand(name: 'mf:{{ Command Name }}')] | 10 #[AsCommand(name: 'mf:{{ Command Name }}')] |
| 11 class {{ Class Name }}Generator extends BaseGenerator | 11 class {{ Class Name }}Generator extends BaseGenerator |
| 12 { | 12 { |
| 13 //use CreatesMatchingTest; | |
| 14 | |
| 15 /** | 13 /** |
| 16 * The name and signature of the console command. | 14 * The name and signature of the console command. |
| 17 * | 15 * |
| 18 * @var string | 16 * @var string |
| 19 */ | 17 */ |
| 31 * | 29 * |
| 32 * @var string | 30 * @var string |
| 33 */ | 31 */ |
| 34 protected $type = '{{ Class Name }}'; | 32 protected $type = '{{ Class Name }}'; |
| 35 | 33 |
| 36 | |
| 37 /** | 34 /** |
| 38 * Execute the console command. | 35 * Execute the console command. |
| 39 */ | 36 */ |
| 40 public function handle() | 37 public function handle() |
| 41 { | 38 { |
| 47 * | 44 * |
| 48 * @return string | 45 * @return string |
| 49 */ | 46 */ |
| 50 protected function getStub() | 47 protected function getStub() |
| 51 { | 48 { |
| 52 return $this->resolveStubPath('/stubs/seeder.stub'); | 49 return $this->resolveStubPath('/stubs/{{ Command Name }}.stub'); |
| 53 } | 50 } |
| 54 | 51 |
| 55 /** | 52 /** |
| 56 * Resolve the fully-qualified path to the stub. | 53 * Resolve the fully-qualified path to the stub. |
| 57 * | 54 * |
| 62 { | 59 { |
| 63 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) | 60 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) |
| 64 ? $customPath | 61 ? $customPath |
| 65 : __DIR__.$stub; | 62 : __DIR__.$stub; |
| 66 } | 63 } |
| 64 | |
| 65 protected function getClassName($name) | |
| 66 { | |
| 67 return $this->{{ Command Name }}_name($name); | |
| 68 } | |
| 69 | |
| 67 /** | 70 /** |
| 68 * Parse the class name and format according to the root namespace. | 71 * Get the stub file for the generator. |
| 69 * | 72 * |
| 70 * @param string $name | |
| 71 * @return string | 73 * @return string |
| 72 */ | 74 */ |
| 73 protected function qualifyClass($name) | 75 protected function getPath($name = null) |
| 74 { | 76 { |
| 75 $name = ltrim($name, '\\/'); | 77 return str_replace(['App\\', '\\'], ['app/', '/'], $this->get{{ Class Name }}Namespace() . '/' . $this->{{ Command Name }}_name($this->getTableInput()) . '.php'); |
| 76 | |
| 77 $name = str_replace('/', '\\', $name); | |
| 78 | |
| 79 $rootNamespace = $this->rootNamespace(); | |
| 80 | |
| 81 if (Str::startsWith($name, $rootNamespace)) { | |
| 82 return $name; | |
| 83 } | |
| 84 | |
| 85 return $this->qualifyClass( | |
| 86 $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name | |
| 87 ); | |
| 88 } | 78 } |
| 89 } | 79 } |
| 90 | 80 |
