comparison src/Generator/Controller/ControllerGenerator.php @ 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
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\Support\Str; 8 use Illuminate\Support\Str;
9 9
10 //use Illuminate\Console\Concerns\CreatesMatchingTest;
11
12 #[AsCommand(name: 'mf:controller')] 10 #[AsCommand(name: 'mf:controller')]
13 class ControllerGenerator extends BaseGenerator 11 class ControllerGenerator extends BaseGenerator
14 { 12 {
15 //use CreatesMatchingTest;
16
17 /** 13 /**
18 * The name and signature of the console command. 14 * The name and signature of the console command.
19 * 15 *
20 * @var string 16 * @var string
21 */ 17 */
33 * 29 *
34 * @var string 30 * @var string
35 */ 31 */
36 protected $type = 'Controller'; 32 protected $type = 'Controller';
37 33
38
39 /** 34 /**
40 * Execute the console command. 35 * Execute the console command.
41 */ 36 */
42 public function handle() 37 public function handle()
43 { 38 {
44 39 parent::handle();
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 } 40 }
79 41
80 /** 42 /**
81 * Get the stub file for the generator. 43 * Get the stub file for the generator.
82 * 44 *
97 { 59 {
98 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) 60 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
99 ? $customPath 61 ? $customPath
100 : __DIR__.$stub; 62 : __DIR__.$stub;
101 } 63 }
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 64
125 protected function getClassName($name) 65 protected function getClassName($name)
126 { 66 {
127 return $this->controller_name($name); 67 return $this->controller_name($name);
128 } 68 }
130 /** 70 /**
131 * Get the stub file for the generator. 71 * Get the stub file for the generator.
132 * 72 *
133 * @return string 73 * @return string
134 */ 74 */
135 protected function getPath($name) 75 protected function getPath($name = null)
136 { 76 {
137 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getControllerNamespace() . '/' . $this->controller_name($this->getTableInput()) . '.php'); 77 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getControllerNamespace() . '/' . $this->controller_name($this->getTableInput()) . '.php');
138 } 78 }
139 } 79 }