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