Mercurial > packages > magicforger
diff examples/ExampleGenerator.php.stub @ 2:cf9993c5c7df
Updated .vimrc for some helper commands.
updated the Base Generator
Brought the controller generator into the package
created an example generator, but it needs some work.
| author | luka |
|---|---|
| date | Sun, 25 Jun 2023 14:45:15 -0400 |
| parents | |
| children | 6468684362c2 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/ExampleGenerator.php.stub Sun Jun 25 14:45:15 2023 -0400 @@ -0,0 +1,120 @@ +<?php + +namespace App\Console\Commands; + +use Symfony\Component\Console\Attribute\AsCommand; +use Wizzard\MagicForger\Generator\BaseGenerator; +use Wizzard\MagicForger\Replacer; +//use Illuminate\Console\Concerns\CreatesMatchingTest; + +#[AsCommand(name: 'mf:{{ Command Name }}')] +class {{ Class Name }}Generator extends BaseGenerator +{ + //use CreatesMatchingTest; + + /** + * The name and signature of the console command. + * + * @var string + */ + protected $name = 'mf:{{ Command Name }}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Generates the {{ Class Name }} File for a table.'; + + /** + * The type of class being generated. + * + * @var string + */ + protected $type = '{{ Class Name }}'; + + + /** + * Execute the console command. + */ + public function handle() + { + + + // First we need to ensure that the table exists, then we can + if (! $this->tableExists($this->getTableInput())) { + $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); + + return false; + } + + $name = $this->qualifyClass($this->getTableInput()); + + $path = $this->getPath($name); + + $file = $this->getFile($name); + + // Next, we will generate the path to the location where this class' file should get + // written. Then, we will build the class and make the proper replacements on the + // file so that it gets the correctly formatted namespace and class name. + $this->makeDirectory($path); + + $this->files->put($path, $this->sortImports($this->buildClass($name))); + + $info = $this->type; + + if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) { + if ($this->handleTestCreation($path)) { + $info .= ' and test'; + } + } + + $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + return $this->resolveStubPath('/stubs/seeder.stub'); + } + + /** + * Resolve the fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveStubPath($stub) + { + return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; + } + /** + * Parse the class name and format according to the root namespace. + * + * @param string $name + * @return string + */ + protected function qualifyClass($name) + { + $name = ltrim($name, '\\/'); + + $name = str_replace('/', '\\', $name); + + $rootNamespace = $this->rootNamespace(); + + if (Str::startsWith($name, $rootNamespace)) { + return $name; + } + + return $this->qualifyClass( + $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name + ); + } +} +
