@@ -17,20 +17,19 @@ Bake イベント
1717例えば、bake ビュークラスに他のヘルパーを追加するためにこのイベントは使用されます。 ::
1818
1919 <?php
20- // config/bootstrap_cli.php
21-
22- use Cake\Event\Event;
20+ use Cake\Event\EventInterface;
2321 use Cake\Event\EventManager;
2422
25- EventManager::instance()->on('Bake.initialize', function (Event $event) {
23+ // in src/Application::bootstrapCli()
24+
25+ EventManager::instance()->on('Bake.initialize', function (EventInterface $event) {
2626 $view = $event->getSubject();
2727
2828 // bake テンプレートの中で MySpecial ヘルパーの使用を可能にします
2929 $view->loadHelper('MySpecial', ['some' => 'config']);
3030
3131 // そして、$author 変数を利用可能にするために追加
32- $view->set('author', 'Andy');
33-
32+ \ $view->set('author', 'Andy');
3433 });
3534
3635別のプラグインの中から bake を変更したい場合は、プラグインの ``config/bootstrap.php ``
@@ -42,12 +41,12 @@ Bake イベントは、既存のテンプレートに小さな変更を行うた
4241``Bake.beforeRender `` で呼び出される関数を使用することができます。 ::
4342
4443 <?php
45- // config/bootstrap_cli.php
46-
47- use Cake\Event\Event;
44+ use Cake\Event\EventInterface;
4845 use Cake\Event\EventManager;
4946
50- EventManager::instance()->on('Bake.beforeRender', function (Event $event) {
47+ // in src/Application::bootstrapCli()
48+
49+ EventManager::instance()->on('Bake.beforeRender', function (EventInterface $event) {
5150 $view = $event->getSubject();
5251
5352 // indexes の中のメインデータ変数に $rows を使用
@@ -65,36 +64,34 @@ Bake イベントは、既存のテンプレートに小さな変更を行うた
6564 if ($view->get('singularVar')) {
6665 $view->set('singularVar', 'theOne');
6766 }
68-
69- });
70-
67+
7168特定の生成されたファイルへの ``Bake.beforeRender `` と ``Bake.afterRender ``
7269イベントを指定することもあるでしょう。例えば、
7370**Controller/controller.twig ** ファイルから生成する際、 UsersController
7471に特定のアクションを追加したい場合、以下のイベントを使用することができます。 ::
7572
7673 <?php
77- // config/bootstrap_cli.php
78-
79- use Cake\Event\Event;
74+ use Cake\Event\EventInterface;
8075 use Cake\Event\EventManager;
8176 use Cake\Utility\Hash;
8277
78+ // in src/Application::bootstrapCli()
79+
8380 EventManager::instance()->on(
8481 'Bake.beforeRender.Controller.controller',
85- function (Event $event) {
82+ function (EventInterface $event) {
8683 $view = $event->getSubject();
87- if ($view->viewVars[ 'name'] == 'Users') {
84+ if ($view->get( 'name') = == 'Users') {
8885 // Users コントローラーに login と logout を追加
89- $view->viewVars[ 'actions'] = [
86+ $view->set( 'actions', [
9087 'login',
9188 'logout',
9289 'index',
9390 'view',
9491 'add',
9592 'edit',
96- 'delete'
97- ];
93+ 'delete',
94+ ]) ;
9895 }
9996 }
10097 );
@@ -109,71 +106,100 @@ Bake テンプレート構文
109106Bake テンプレートファイルは、 `Twig <https://twig.symfony.com/doc/2.x/ >`__
110107テンプレート構文を使用します。
111108
112- だから、例えば、以下のようにシェルを bake した場合:
109+ だから、例えば、以下のようにコマンドを bake した場合:
113110
114111.. code-block :: bash
115112
116- bin/cake bake shell Foo
113+ bin/cake bake command Foo
117114
118- (**vendor/cakephp/bake/src/Template/Bake/Shell/shell .twig **) を使用した
115+ (**vendor/cakephp/bake/templates/bake/Command/command .twig **) を使用した
119116テンプレートは、以下のようになります。 ::
120117
121118 <?php
122- namespace {{ namespace }}\Shell;
119+ declare(strict_types=1);
120+
121+ namespace {{ namespace }}\Command;
123122
124- use Cake\Console\Shell;
123+ use Cake\Command\Command;
124+ use Cake\Console\Arguments;
125+ use Cake\Console\ConsoleIo;
126+ use Cake\Console\ConsoleOptionParser;
125127
126128 /**
127- * {{ name }} shell command.
128- */
129- class {{ name }}Shell extends Shell
129+ * {{ name }} command.
130+ */
131+ class {{ name }}Command extends Command
130132 {
131133 /**
132- * main() method.
133- *
134- * @return bool|int Success or error code.
135- */
136- public function main()
134+ * Hook method for defining this command's option parser.
135+ *
136+ * @see https://book.cakephp.org/5/en/console-commands/commands.html#defining-arguments-and-options
137+ * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
138+ * @return \Cake\Console\ConsoleOptionParser The built parser.
139+ */
140+ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
137141 {
142+ $parser = parent::buildOptionParser($parser);
143+
144+ return $parser;
138145 }
139146
147+ /**
148+ * Implement this method with your command's logic.
149+ *
150+ * @param \Cake\Console\Arguments $args The command arguments.
151+ * @param \Cake\Console\ConsoleIo $io The console io
152+ * @return int|null|void The exit code or null for success
153+ */
154+ public function execute(Arguments $args, ConsoleIo $io)
155+ {
156+ }
140157 }
141158
142- そして、 bake で得られたクラス (**src/Shell/FooShell .php **) は、
159+ そして、 bake で得られたクラス (**src/Command/FooCommand .php **) は、
143160このようになります。 ::
144161
145162 <?php
146- namespace App\Shell ;
163+ declare(strict_types=1) ;
147164
148- use Cake\Console\Shell;
165+ namespace App\Command;
166+
167+ use Cake\Command\Command;
168+ use Cake\Console\Arguments;
169+ use Cake\Console\ConsoleIo;
170+ use Cake\Console\ConsoleOptionParser;
149171
150172 /**
151- * Foo shell command.
152- */
153- class FooShell extends Shell
173+ * Foo command.
174+ */
175+ class FooCommand extends Command
154176 {
155177 /**
156- * main() method.
157- *
158- * @return bool|int Success or error code.
159- */
160- public function main()
178+ * Hook method for defining this command's option parser.
179+ *
180+ * @see https://book.cakephp.org/5/en/console-commands/commands.html#defining-arguments-and-options
181+ * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
182+ * @return \Cake\Console\ConsoleOptionParser The built parser.
183+ */
184+ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
161185 {
186+ $parser = parent::buildOptionParser($parser);
187+
188+ return $parser;
162189 }
163190
191+ /**
192+ * Implement this method with your command's logic.
193+ *
194+ * @param \Cake\Console\Arguments $args The command arguments.
195+ * @param \Cake\Console\ConsoleIo $io The console io
196+ * @return int|null|void The exit code or null for success
197+ */
198+ public function execute(Arguments $args, ConsoleIo $io)
199+ {
200+ }
164201 }
165202
166- .. note ::
167-
168- バージョン 1.5.0 より前の bake は、 .ctp テンプレートファイルでカスタム erb スタイルのタグを
169- 使用していました。
170-
171- * ``<% `` Bake テンプレートの PHP 開始タグ
172- * ``%> `` Bake テンプレートの PHP 終了タグ
173- * ``<%= `` Bake テンプレートの PHP ショートエコータグ
174- * ``<%- `` Bake テンプレートの PHP 開始タグ、タグの前に、先頭の空白を除去
175- * ``-%> `` Bake テンプレートの PHP 終了タグ、タグの後に末尾の空白を除去
176-
177203.. _creating-a-bake-theme :
178204
179205Bake テーマの作成
@@ -184,102 +210,107 @@ Bake テーマの作成
184210これを行うための最善の方法は、次のとおりです。
185211
186212#. 新しいプラグインを bake します。プラグインの名前は bake の「テーマ」名になります。
187- #. 新しいディレクトリー **plugins/[name]/src/Template/Bake/Template/ ** を作成します。
188- #. **vendor/cakephp/bake/src/Template/Bake/Template ** から上書きしたい
213+ 例 ``bin/cake bake plugin custom_bake ``
214+ #. 新しいディレクトリー **plugins/CustomBake/templates/bake/ ** を作成します。
215+ #. **vendor/cakephp/bake/templates/bake ** から上書きしたい
189216 テンプレートをあなたのプラグインの中の適切なファイルにコピーしてください。
190- #. bake を実行するときに、必要であれば、 bake のテーマを指定するための ``--theme ``
217+ #. bake を実行するときに、必要であれば、 bake のテーマを指定するための ``--theme CustomBake ``
191218 オプションを使用してください。各呼び出しでこのオプションを指定しなくても済むように、
192219 カスタムテーマをデフォルトテーマとして使用するように設定することもできます。 ::
193220
194221 <?php
195- // config/bootstrap.php または config/bootstrap_cli.php の中で
222+ // src/Application::bootstrapCli()の中の'Bake'プラグインを読み込む前に
196223 Configure::write('Bake.theme', 'MyTheme');
197224
198- Bake テンプレートのカスタマイズ
225+ アプリケーション Bake テンプレート
199226===============================
200227
201- "bake" コマンドによって生成されるデフォルトの出力を変更したい場合、アプリケーションで独自の
202- bake テンプレートを作成することができます。この方法では、bake する際、コマンドラインで
203- ``--theme `` オプションを使用していません。これを行うための最善の方法は、次のとおりです。
228+ 幾つかのbakeテンプレートのカスタマイズが必要か、もしくはアプリケーション依存のテンプレートを使う必要がある場合、アプリケーションテンプレートを上書きするテンプレートを含めることができます。この上書きは他のプラグインテンプレートの上書きと同様に機能します。
204229
205230#. 新しいディレクトリー **/templates/plugin/Bake/ ** を作成します。
206231#. **vendor/cakephp/bake/templates/bake/ ** から上書きしたいテンプレートを
207232 あなたのアプリケーションの中の適切なファイルにコピーします。
208233
234+ アプリケーションテンプレートの使用には``--theme`` オプションを使う必要はありません。
235+
209236Bake コマンドオプションの新規作成
210237=================================
211238
212- あなたのアプリケーションやプラグインでタスクを作成することによって 、新しい bake コマンドの
213- オプションを追加したり、 CakePHP が提供するオプションを上書きすることが可能です。
214- ``Bake\Shell\Task\BakeTask `` を継承することで、bake は、あなたの新しいタスクを見つけて
239+ あなたのアプリケーションやプラグインで 、新しい bake コマンドのオプションを追加したり、
240+ CakePHP が提供するオプションを上書きすることが可能です。
241+ ``Bake\Command\BakeCommand `` を継承することで、bake は、あなたの新しいタスクを見つけて
215242bake の一部としてそれを含めます。
216243
217244例として、任意の foo クラスを作成するタスクを作ります。
218- まず、 **src/Shell/Task/FooTask .php ** タスクファイルを作成します 。
219- 私たちのシェルタスクが単純になるように 、 ``SimpleBakeTask `` を継承します。
220- ``SimpleBakeTask `` は抽象クラスで、どのタスクが呼ばれるか、どこにファイルを生成するか、
245+ まず、 **src/Command/Bake/FooCommand .php ** コマンドファイルを作成します 。
246+ 私たちのコマンドが単純になるように 、 ``SimpleBakeCommand `` を継承します。
247+ ``SimpleBakeCommand `` は抽象クラスで、どのタスクが呼ばれるか、どこにファイルを生成するか、
221248どのテンプレートを使用するかを bake に伝える3つのメソッドを定義することが必要です。
222- FooTask .php ファイルは次のようになります。 ::
249+ FooCommand .php ファイルは次のようになります。 ::
223250
224251 <?php
225- namespace App\Shell\Task;
252+ declare(strict_types=1);
253+
254+ namespace App\Command\Bake;
226255
227- use Bake\Shell\Task\SimpleBakeTask ;
256+ use Bake\Command\SimpleBakeCommand ;
228257
229- class FooTask extends SimpleBakeTask
258+ class FooCommand extends SimpleBakeCommand
230259 {
231- public $pathFragment = 'Foo /';
260+ public $pathFragment = 'FooPath /';
232261
233- public function name()
262+ public function name(): string
234263 {
235264 return 'foo';
236265 }
237266
238- public function fileName($name)
267+ public function template(): string
239268 {
240- return $name . 'Foo.php ';
269+ return 'fooTemplate ';
241270 }
242271
243- public function template()
272+ public function fileName(string $name): string
244273 {
245- return 'foo ';
274+ return $name . 'FooOut.php ';
246275 }
247-
248276 }
249277
250278このファイルが作成されたら、コードを生成する際に bake 使用することができるテンプレートを
251- 作成する必要があります。 **src/Template/Bake/foo .twig ** を作成してください。
279+ 作成する必要があります。 **templates/bake/foo_template .twig ** を作成してください。
252280このファイルに、以下の内容を追加します。 ::
253281
254282 <?php
255- namespace {{ namespace }}\Foo ;
283+ namespace {{ namespace }}\FooPath ;
256284
257285 /**
258- * {{ name }} foo
286+ * {{ name }} fooOut
259287 */
260- class {{ name }}Foo
288+ class {{ name }}FooOut
261289 {
262- // コードを追加。
290+ // Add code.
263291 }
264292
265- これで、``bin/cake bake `` の出力に新しいタスクが表示されるはずです 。
293+ これで、``bin/cake bake `` の出力に新しいコマンドが表示されるはずです 。
266294``bin/cake bake foo Example `` を実行して、新しいタスクを実行することができます。
267- これは、使用するアプリケーションの **src/Foo/ExampleFoo .php ** で
268- 新しい ``ExampleFoo `` クラスを生成します。
295+ これは、使用するアプリケーションの **src/FooPath/ExampleFooOut .php ** で
296+ 新しい ``ExampleFooOut `` クラスを生成します。
269297
270- また、 ``ExampleFoo `` クラスのテストファイルを作成するために ``bake `` を呼びたい場合は、
271- カスタムコマンド名のクラスサフィックスと名前空間を登録するために `` FooTask `` クラスの
298+ また、 ``ExampleFooOut `` クラスのテストファイルを作成するために ``bake `` を呼びたい場合は、
299+ カスタムコマンド名のクラスサフィックスと名前空間を登録するために `FooCommand `` クラスの
272300``bakeTest() `` メソッドをオーバーライドする必要があります。 ::
273301
274- public function bakeTest($className)
302+ use Cake\Console\Arguments;
303+ use Cake\Console\ConsoleIo;
304+
305+ public function bakeTest(string $className, Arguments $args, ConsoleIo $io): void
275306 {
276307 if (!isset($this->Test->classSuffixes[$this->name()])) {
277- $this->Test->classSuffixes[$this->name()] = 'Foo';
308+ $this->Test->classSuffixes[$this->name()] = 'Foo';
278309 }
279310
280311 $name = ucfirst($this->name());
281312 if (!isset($this->Test->classTypes[$name])) {
282- $this->Test->classTypes[$name] = 'Foo';
313+ $this->Test->classTypes[$name] = 'Foo';
283314 }
284315
285316 return parent::bakeTest($className);
@@ -291,6 +322,20 @@ FooTask.php ファイルは次のようになります。 ::
291322 あなたのファイルを導くために使用されるサブ名前空間です。
292323 前の例では、名前空間 ``App\Test\TestCase\Foo `` でテストを作成します。
293324
325+ BakeView クラスの設定
326+ ==============================
327+
328+ bake コマンドは ``BakeView `` クラスをテンプレートをレンダリングするために使います。 You can
329+ access the instance by listening to the ``Bake.initialize `` イベントを監視するためにこのインスタンスにアクセスできます。 例えば、以下の様にして独自のヘルパーを読み込みbakeテンプレートで使用できます::
330+
331+ <?php
332+ \Cake\Event\EventManager::instance()->on(
333+ 'Bake.initialize',
334+ function ($event, $view) {
335+ $view->loadHelper('Foo');
336+ }
337+ );
338+
294339.. meta ::
295340 :title lang=ja: Bake の拡張
296341 :keywords lang=ja: command line interface,development,bake view, bake template syntax,twig,erb tags,percent tags
0 commit comments