comparison src/Generator/BaseGenerator.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
34 /** 34 /**
35 * Execute the console command. 35 * Execute the console command.
36 */ 36 */
37 public function handle() 37 public function handle()
38 { 38 {
39
40
39 // First we need to ensure that the table exists, then we can 41 // First we need to ensure that the table exists, then we can
40 if (! $this->tableExists($this->getTableInput())) { 42 if (! $this->tableExists($this->getTableInput())) {
41 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); 43 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
42 44
43 return false; 45 return false;
44 } 46 }
45 47
46 $name = $this->qualifyClass($this->getTableInput()); 48 $path = $this->getPath();
47 49
48 $path = $this->getPath($name); 50 $file = $this->getFile($path);
49 51
50 $file = $this->getFile($name); 52 $file = $this->apply_replacements($file);
51 53
52 dd($this->get_all_inserts($file));
53
54 // Next, we will generate the path to the location where this class' file should get
55 // written. Then, we will build the class and make the proper replacements on the
56 // file so that it gets the correctly formatted namespace and class name.
57 $this->makeDirectory($path); 54 $this->makeDirectory($path);
58 55
59 $this->files->put($path, $this->sortImports($this->buildClass($name))); 56 $this->files->put($path, $this->sortImports($file));
60 57
61 $info = $this->type; 58 $info = $this->type;
62 59
63 if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
64 if ($this->handleTestCreation($path)) {
65 $info .= ' and test';
66 }
67 }
68
69 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); 60 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
70
71 } 61 }
72 62
73 /** 63 /**
74 * Override the original so that we can prompt for a table with autocomplete. 64 * Override the original so that we can prompt for a table with autocomplete.
75 * 65 *
127 * @return array 117 * @return array
128 */ 118 */
129 protected function getOptions() 119 protected function getOptions()
130 { 120 {
131 return [ 121 return [
122 ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'],
132 ]; 123 ];
133 } 124 }
134 125
135 /** 126 /**
136 * Interact further with the user if they were prompted for missing arguments. 127 * Interact further with the user if they were prompted for missing arguments.
137 * 128 *
138 * @return void 129 * @return void
139 */ 130 */
140 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) 131 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
141 { 132 {
133 }
134
135 /**
136 * Determines if the file exists
137 */
138 protected function fileExists(string $path): bool
139 {
140 return $this->files->exists($path);
142 } 141 }
143 142
144 /** 143 /**
145 * Gets the file that will be worked on. If there is already an existing file 144 * Gets the file that will be worked on. If there is already an existing file
146 * then we can open that. However if we are forcing the operation, then we 145 * then we can open that. However if we are forcing the operation, then we
147 * will start with an empty stub. 146 * will start with an empty stub.
148 * 147 *
149 */ 148 */
150 protected function getFile($name) 149 protected function getFile($name)
151 { 150 {
152 151 if ((! $this->hasOption('fresh') ||
153 if ((! $this->hasOption('force') || 152 ! $this->option('fresh')) &&
154 ! $this->option('force')) && 153 $this->fileExists($name)) {
155 $this->alreadyExists($name)) {
156 //Working with an existing file 154 //Working with an existing file
157 $this->files->get($name); 155 return $this->files->get($name);
158 } 156 }
159 157
160 //Working with a stub 158 //Working with a stub
161 return $this->files->get($this->getStub()); 159 return $this->files->get($this->getStub());
162 } 160 }
169 protected function getTableInput() 167 protected function getTableInput()
170 { 168 {
171 return trim($this->argument('table')); 169 return trim($this->argument('table'));
172 } 170 }
173 171
172
174 /** 173 /**
175 * Determines if the table exists in the current database 174 * Determines if the table exists in the current database
176 */ 175 */
177 protected function tableExists(string $table_name): bool 176 protected function tableExists(string $table_name): bool
178 { 177 {