comparison src/Generator/Controller/ControllerGenerator.php @ 3:6468684362c2

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