@@ -68,7 +68,6 @@ For instance, if you want to add specific actions to your `UsersController` when
6868<?php
6969use Cake\Event\EventInterface;
7070use Cake\Event\EventManager;
71- use Cake\Utility\Hash;
7271
7372// in src/Application::bootstrapCli()
7473
@@ -107,48 +106,73 @@ bin/cake bake command Foo
107106The template used at ` vendor/cakephp/bake/templates/bake/Command/command.twig ` looks like this:
108107
109108``` php
110- <?php
111- declare(strict_types=1);
112-
113- namespace {{ namespace }}\Command;
114-
115- use Cake\Command\Command;
116- use Cake\Console\Arguments;
117- use Cake\Console\ConsoleIo;
118- use Cake\Console\ConsoleOptionParser;
109+ {{ element('Bake.file_header', {
110+ namespace: "#{namespace}\\Command",
111+ classImports: [
112+ 'Cake\\Command\\Command',
113+ 'Cake\\Console\\ConsoleOptionParser',
114+ ],
115+ }) }}
119116
120117/**
121- * {{ name }} command.
122- */
118+ * {{ name }} command.
119+ */
123120class {{ name }}Command extends Command
124121{
125122 /**
126- * Hook method for defining this command's option parser.
127- *
128- * @link https://book.cakephp.org/5/en/console-commands/commands.html#defining-arguments-and-options
129- * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
130- * @return \Cake\Console\ConsoleOptionParser The built parser.
131- */
132- public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
123+ * The name of this command.
124+ *
125+ * @var string
126+ */
127+ protected string $name = 'cake {{ command_name }}';
128+
129+ /**
130+ * Get the default command name.
131+ *
132+ * @return string
133+ */
134+ public static function defaultName(): string
135+ {
136+ return '{{ command_name }}';
137+ }
138+
139+ /**
140+ * Get the command description.
141+ *
142+ * @return string
143+ */
144+ public static function getDescription(): string
133145 {
134- $parser = parent::buildOptionParser($parser);
146+ return 'Command description here.';
147+ }
135148
136- return $parser;
149+ /**
150+ * Hook method for defining this command's option parser.
151+ *
152+ * @link https://book.cakephp.org/6/en/console-commands/commands.html#defining-arguments-and-options
153+ * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
154+ * @return \Cake\Console\ConsoleOptionParser The built parser.
155+ */
156+ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
157+ {
158+ return parent::buildOptionParser($parser)
159+ ->setDescription(static::getDescription());
137160 }
138161
139162 /**
140- * Implement this method with your command's logic.
141- *
142- * @param \Cake\Console\Arguments $args The command arguments.
143- * @param \Cake\Console\ConsoleIo $io The console io
144- * @return int|null|void The exit code or null for success
145- */
146- public function execute(Arguments $args, ConsoleIo $io)
163+ * Implement this method with your command's logic.
164+ *
165+ * @return int|null|void The exit code or null for success
166+ */
167+ public function execute()
147168 {
148169 }
149170}
150171```
151172
173+ The ` element() ` call emits the ` <?php declare(strict_types=1); ` header, the namespace, and the ` use ` statements.
174+ Note that commands use ` $this->args ` and ` $this->io ` instead of receiving them as ` execute() ` parameters.
175+
152176The resultant baked class at ` src/Command/FooCommand.php ` looks like this:
153177
154178``` php
@@ -158,37 +182,59 @@ declare(strict_types=1);
158182namespace App\Command;
159183
160184use Cake\Command\Command;
161- use Cake\Console\Arguments;
162- use Cake\Console\ConsoleIo;
163185use Cake\Console\ConsoleOptionParser;
164186
165187/**
166- * Foo command.
167- */
188+ * Foo command.
189+ */
168190class FooCommand extends Command
169191{
170192 /**
171- * Hook method for defining this command's option parser.
172- *
173- * @link https://book.cakephp.org/5/en/console-commands/commands.html#defining-arguments-and-options
174- * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
175- * @return \Cake\Console\ConsoleOptionParser The built parser.
176- */
177- public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
193+ * The name of this command.
194+ *
195+ * @var string
196+ */
197+ protected string $name = 'cake foo';
198+
199+ /**
200+ * Get the default command name.
201+ *
202+ * @return string
203+ */
204+ public static function defaultName(): string
178205 {
179- $parser = parent::buildOptionParser($parser);
206+ return 'foo';
207+ }
180208
181- return $parser;
209+ /**
210+ * Get the command description.
211+ *
212+ * @return string
213+ */
214+ public static function getDescription(): string
215+ {
216+ return 'Command description here.';
217+ }
218+
219+ /**
220+ * Hook method for defining this command's option parser.
221+ *
222+ * @link https://book.cakephp.org/6/en/console-commands/commands.html#defining-arguments-and-options
223+ * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
224+ * @return \Cake\Console\ConsoleOptionParser The built parser.
225+ */
226+ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
227+ {
228+ return parent::buildOptionParser($parser)
229+ ->setDescription(static::getDescription());
182230 }
183231
184232 /**
185- * Implement this method with your command's logic.
186- *
187- * @param \Cake\Console\Arguments $args The command arguments.
188- * @param \Cake\Console\ConsoleIo $io The console io
189- * @return int|null|void The exit code or null for success
190- */
191- public function execute(Arguments $args, ConsoleIo $io)
233+ * Implement this method with your command's logic.
234+ *
235+ * @return int|null|void The exit code or null for success
236+ */
237+ public function execute()
192238 {
193239 }
194240}
@@ -237,7 +283,7 @@ use Bake\Command\SimpleBakeCommand;
237283
238284class FooCommand extends SimpleBakeCommand
239285{
240- public $pathFragment = 'FooPath/';
286+ public string $pathFragment = 'FooPath/';
241287
242288 public function name(): string
243289 {
@@ -278,26 +324,26 @@ This generates `src/FooPath/ExampleFooOut.php`.
278324If you also want ` bake ` to create a test file for your ` ExampleFooOut ` class, override the ` bakeTest() ` method in ` FooCommand ` :
279325
280326``` php
281- use Cake\Console\Arguments;
282- use Cake\Console\ConsoleIo;
327+ use Bake\Command\TestCommand;
283328
284- public function bakeTest(string $className, Arguments $args, ConsoleIo $io ): void
329+ public function bakeTest(string $className): void
285330{
286- if (!isset($this->Test->classSuffixes[$this->name()])) {
287- $this->Test->classSuffixes[$this->name()] = 'Foo';
288- }
289-
290- $name = ucfirst($this->name());
291- if (!isset($this->Test->classTypes[$name])) {
292- $this->Test->classTypes[$name] = 'Foo';
331+ if ($this->args->getOption('no-test')) {
332+ return;
293333 }
294334
295- parent::bakeTest($className, $args, $io);
335+ $test = new TestCommand();
336+ $test->classSuffixes['Foo'] = 'FooOut';
337+ $test->classTypes['Foo'] = 'FooPath';
338+ $test->plugin = $this->plugin;
339+ $test->setArgs($this->args);
340+ $test->setIo($this->io);
341+ $test->bake('Foo', $className);
296342}
297343```
298344
299- - The ** class suffix** is appended to the name provided in your ` bake ` call. In the example above, that would create ` ExampleFooTest .php` .
300- - The ** class type** is the sub-namespace used to reach your file relative to the app or plugin you are baking into. In the example above, that would create the test namespace ` App\Test\TestCase\Foo ` .
345+ - The ** class suffix** is appended to the name provided in your ` bake ` call. In the example above, that would create ` ExampleFooOut ` and its test file ` tests/TestCase/FooPath/ExampleFooOutTest .php` .
346+ - The ** class type** value is the sub-namespace used to reach your file relative to the app or plugin you are baking into. In the example above, that would create the test namespace ` App\Test\TestCase\FooPath ` .
301347
302348## Configuring the BakeView Class
303349
0 commit comments