cakephp3 bakeによって生成されるファイル場所のカスタマイズ
●bakeによって生成されるmodelの出力場所を指定する(Taskのカスタマイズ)
1、src\Shellフォルダ直下にTaskフォルダを作成し、Bake\Shell\Task\ModelTaskを継承するTaskカスタマイズ用のファイルを作る(ここではExtModelTask.phpとする)
2、tableとentityの出力場所はbakeEntityメソッドとbakeTableメソッドなので、ここを修正する(修正箇所や修正内容は以下参照)
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Shell\Task;
use Bake\Shell\Task\ModelTask;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Database\Schema\TableSchema;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Validation\Validation;
/**
* Task class for generating model files.
*
* @property \Bake\Shell\Task\FixtureTask $Fixture
* @property \Bake\Shell\Task\BakeTemplateTask $BakeTemplate
* @property \Bake\Shell\Task\TestTask $Test
*/
class ExtModelTask extends ModelTask
{
/**
* Generate code for the given model name.
*
* @param string $name The model name to generate.
* @return void
*/
public function bake($name)
{
$table = $this->getTable($name);
$tableObject = $this->getTableObject($name, $table);
$data = $this->getTableContext($tableObject, $table, $name);
$this->bakeTable($tableObject, $data);
$this->bakeEntity($tableObject, $data);
$this->bakeFixture($tableObject->getAlias(), $tableObject->getTable());
$this->bakeTest($tableObject->getAlias());
}
/**
* Bake an entity class.
*
* @param \Cake\ORM\Table $model Model name or object
* @param array $data An array to use to generate the Table
* @return string|null
*/
public function bakeEntity($model, array $data = [])
{
if (!empty($this->params['no-entity'])) {
return null;
}
$name = $this->_entityName($model->getAlias());
$namespace = Configure::read('App.namespace');
$pluginPath = '';
if ($this->plugin) {
$namespace = $this->_pluginNamespace($this->plugin);
$pluginPath = $this->plugin . '.';
}
$data += [
'name' => $name,
'namespace' => $namespace,
'plugin' => $this->plugin,
'pluginPath' => $pluginPath,
'primaryKey' => [],
];
$this->BakeTemplate->set($data);
$out = $this->BakeTemplate->generate('Model/entity');
$path = $this->getPath();
$filename = $path . 'Entity' . DS . 'Baked' . DS . $name . '.php';
$this->out("\n" . sprintf('Baking entity class for %s...', $name), 1, Shell::QUIET);
$this->createFile($filename, $out);
$emptyFile = $path . 'Entity' . DS . 'empty';
$this->_deleteEmptyFile($emptyFile);
return $out;
}
/**
* Bake a table class.
*
* @param \Cake\ORM\Table $model Model name or object
* @param array $data An array to use to generate the Table
* @return string|null
*/
public function bakeTable($model, array $data = [])
{
if (!empty($this->params['no-table'])) {
return null;
}
$namespace = Configure::read('App.namespace');
$pluginPath = '';
if ($this->plugin) {
$namespace = $this->_pluginNamespace($this->plugin);
}
$name = $model->getAlias();
$entity = $this->_entityName($model->getAlias());
$data += [
'plugin' => $this->plugin,
'pluginPath' => $pluginPath,
'namespace' => $namespace,
'name' => $name,
'entity' => $entity,
'associations' => [],
'primaryKey' => 'id',
'displayField' => null,
'table' => null,
'validation' => [],
'rulesChecker' => [],
'behaviors' => [],
'connection' => $this->connection,
];
$this->BakeTemplate->set($data);
$out = $this->BakeTemplate->generate('Model/table');
$path = $this->getPath();
$filename = $path . 'Table' . DS . 'Baked' . DS . $name . 'Table.php';
$this->out("\n" . sprintf('Baking table class for %s...', $name), 1, Shell::QUIET);
$this->createFile($filename, $out);
// Work around composer caching that classes/files do not exist.
// Check for the file as it might not exist in tests.
if (file_exists($filename)) {
require_once $filename;
}
TableRegistry::getTableLocator()->clear();
$emptyFile = $path . 'Table' . DS . 'empty';
$this->_deleteEmptyFile($emptyFile);
return $out;
}
}