Fuelphpのoil自動生成でphpdocを自動で書くようにする

fuelphpのoilでのモデル自動生成が便利なんですが、またまたちょっとだけ不満があって、 自動生成なのにphpdocを書いてくれない・・・・ それによって自動補完もされないので、使いづらい。 って思いが積もったので、oilの中身を改造してphpdocを書くようにしてみました。 oilのなかみはときどきviewをつかったり、時々model側(でいいのかな?)に直書きしたりと読むのが大変なコードでしたが、多分ここだけ追加すれば大丈夫だろうというところをメモっときます。 まず、packages/oli/classes/generate.php これはmodel関数のみ変更した

 public static function model($args, $build = true) { $singular = ¥Inflector::singularize(¥Str::lower(array_shift($args))); if (empty($singular) or strpos($singular, ':')) { throw new Exception("Command is invalid." . PHP_EOL . "¥tphp oil g model <modelname> [<fieldname1>:<type1> |<fieldname2>:<type2> |..]"); } if (empty($args)) { throw new Exception('No fields have been provided, the model will not know how to build the table.'); } $plural = ¥Inflector::pluralize($singular); $filename = trim(str_replace(array('_', '-'), DS, $singular), DS); $filepath = APPPATH . 'classes/model/' . $filename . '.php'; // Uppercase each part of the class name and remove hyphens $class_name = ¥Inflector::classify($singular, false); $contents = ''; $timestamp_properties = array(); if (!¥Cli::option('no-timestamp')) { $timestamp_properties = array('created_at:int', 'updated_at:int'); } // Turn foo:string into "id", "foo", $properties = implode(",¥n¥t¥t", array_map(function($field) { // Only take valid fields if (($field = strstr($field, ':', true))) { return "'" . $field . "'"; } }, array_merge(array('id:int'), $args, $timestamp_properties))); if (!¥Cli::option('no-properties')) { $contents .= <<<CONTENTS protected static ¥$_properties = array( {$properties} ); CONTENTS; } $phpdoc = ''; $phpdocProperties = implode("¥n", array_map(function($field) { // Only take valid fields if (($field = strstr($field, ':', true))) { return " * @property $" . $field ; } }, array_merge(array('id:int'), $args, $timestamp_properties))); if (!¥Cli::option('no-phpdoc')) { $phpdoc .= <<<CONTENTS /** {$phpdocProperties} */ CONTENTS; } if (¥Cli::option('crud')) { $contents .= <<<CONTENTS protected static ¥$_table_name = '{$plural}'; CONTENTS; $model = <<<MODEL <?php {$phpdoc} class Model_{$class_name} extends ¥Model_Crud { {$contents} } MODEL; } else { if (!¥Cli::option('no-timestamp')) { $contents .= <<<CONTENTS protected static ¥$_observers = array( 'Orm¥Observer_CreatedAt' => array( 'events' => array('before_insert'), 'mysql_timestamp' => false, ), 'Orm¥Observer_UpdatedAt' => array( 'events' => array('before_save'), 'mysql_timestamp' => false, ), ); CONTENTS; } $model = <<<MODEL <?php {$phpdoc} class Model_{$class_name} extends ¥Orm¥Model { {$contents} } MODEL; } // Build the model static::create($filepath, $model, 'model'); if (!empty($args)) { array_unshift($args, 'create_' . $plural); static::migration($args, false); } else { throw new Exception('Not enough arguments to create this migration.'); } $build and static::build(); } 

つぎにpackages/oil/views/scaffolding/orm/model.php こっちはviewとしてきちんとなっていたので変更が楽だった!

 use Orm¥Model; /** * @property $id <?php foreach ($fields as $field): ?> * @property $<?php echo $field['name']; ?> <?php endforeach; ?> <?php if ($include_timestamps): ?> * @property $created_at * @property $updated_at <?php endif; ?> **/ class Model_<?php echo $model_name; ?> extends Model { protected static $_properties = array( 'id', <?php foreach ($fields as $field): ?> (以下省略) 

最後にpackages/oil/views/admin/orm/model.php これもviewなのでらくらく。

 <?php echo '<?php' ?> /** * @property $id <?php foreach ($fields as $field): ?> * @property $<?php echo $field['name']; ?> <?php endforeach; ?> <?php if ($include_timestamps): ?> * @property $created_at * @property $updated_at <?php endif; ?> **/ class Model_<?php echo $model_name; ?> extends ¥Orm¥Model { 

この変更を適用すれば、modelを自動生成した時に

 /** * @property $id * @property $name **/ class Model_Name extends ¥Orm¥Model{ 

といった形で、@propertyが追加される。 これがあればnetbeansなどのIDEで補完が効く・・!! ほんとはtypeもきちんと指定してあげるのが正しいですが、めんどくさそうだったのでやめました。

img_show