comparison 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
comparison
equal deleted inserted replaced
1:ca36acd2bef2 2:cf9993c5c7df
1 <?php
2
3 namespace App\Console\Commands;
4
5 use Symfony\Component\Console\Attribute\AsCommand;
6 use Wizzard\MagicForger\Generator\BaseGenerator;
7 use Wizzard\MagicForger\Replacer;
8 //use Illuminate\Console\Concerns\CreatesMatchingTest;
9
10 #[AsCommand(name: 'mf:{{ Command Name }}')]
11 class {{ Class Name }}Generator extends BaseGenerator
12 {
13 //use CreatesMatchingTest;
14
15 /**
16 * The name and signature of the console command.
17 *
18 * @var string
19 */
20 protected $name = 'mf:{{ Command Name }}';
21
22 /**
23 * The console command description.
24 *
25 * @var string
26 */
27 protected $description = 'Generates the {{ Class Name }} File for a table.';
28
29 /**
30 * The type of class being generated.
31 *
32 * @var string
33 */
34 protected $type = '{{ Class Name }}';
35
36
37 /**
38 * Execute the console command.
39 */
40 public function handle()
41 {
42
43
44 // First we need to ensure that the table exists, then we can
45 if (! $this->tableExists($this->getTableInput())) {
46 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
47
48 return false;
49 }
50
51 $name = $this->qualifyClass($this->getTableInput());
52
53 $path = $this->getPath($name);
54
55 $file = $this->getFile($name);
56
57 // Next, we will generate the path to the location where this class' file should get
58 // written. Then, we will build the class and make the proper replacements on the
59 // file so that it gets the correctly formatted namespace and class name.
60 $this->makeDirectory($path);
61
62 $this->files->put($path, $this->sortImports($this->buildClass($name)));
63
64 $info = $this->type;
65
66 if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
67 if ($this->handleTestCreation($path)) {
68 $info .= ' and test';
69 }
70 }
71
72 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
73 }
74
75 /**
76 * Get the stub file for the generator.
77 *
78 * @return string
79 */
80 protected function getStub()
81 {
82 return $this->resolveStubPath('/stubs/seeder.stub');
83 }
84
85 /**
86 * Resolve the fully-qualified path to the stub.
87 *
88 * @param string $stub
89 * @return string
90 */
91 protected function resolveStubPath($stub)
92 {
93 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
94 ? $customPath
95 : __DIR__.$stub;
96 }
97 /**
98 * Parse the class name and format according to the root namespace.
99 *
100 * @param string $name
101 * @return string
102 */
103 protected function qualifyClass($name)
104 {
105 $name = ltrim($name, '\\/');
106
107 $name = str_replace('/', '\\', $name);
108
109 $rootNamespace = $this->rootNamespace();
110
111 if (Str::startsWith($name, $rootNamespace)) {
112 return $name;
113 }
114
115 return $this->qualifyClass(
116 $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
117 );
118 }
119 }
120