2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 // {{{ Ethna_Plugin_Generator
16 * @author Masaki Fujimoto <fujimoto@php.net>
20 class Ethna_Plugin_Generator extends Ethna_Plugin_Abstract
22 /** @var object Ethna_Controller スケルトン生成に使うコントローラ */
31 function Ethna_Plugin_Generator(&$controller, $type, $name)
33 // Ethna_Generatorでpluginを取得するときに使ったコントローラ
34 // ex, add-projectではEthna_Controller, add-actionではApp_Controller
35 $this->ctl =& $controller;
43 * @param string $skel スケルトンファイル
45 function _resolveSkelfile($skel)
47 $file = realpath($skel);
48 if (file_exists($file)) {
53 $base = $this->ctl->getBasedir();
54 $file = "$base/skel/$skel";
55 if (file_exists($file)) {
59 // Ethna本体の skel ディレクトリ
60 $base = dirname(dirname(dirname(__FILE__)));
61 $file = "$base/skel/$skel";
62 if (file_exists($file)) {
70 * スケルトンファイルにマクロを適用してファイルを生成する
73 * @param string $skel スケルトンファイル
74 * @param string $entity 生成ファイル名
75 * @param array $macro 置換マクロ
76 * @param bool $overwrite 上書きフラグ
77 * @return bool true:正常終了 false:エラー
79 function _generateFile($skel, $entity, $macro, $overwrite = false)
81 if (file_exists($entity)) {
82 if ($overwrite === false) {
83 printf("file [%s] already exists -> skip\n", $entity);
86 printf("file [%s] already exists, to be overwriten.\n", $entity);
90 $resolved = $this->_resolveSkelfile($skel);
91 if ($resolved === false) {
92 printf("skelton file [%s] not found.\n", $skel);
98 $rfp = fopen($skel, "r");
102 $wfp = fopen($entity, "w");
109 $s = fread($rfp, 4096);
110 if (strlen($s) == 0) {
114 foreach ($macro as $k => $v) {
115 $s = preg_replace("/{\\\$$k}/", $v, $s);
124 if (chmod($entity, $st[2]) == false) {
128 printf("file generated [%s -> %s]\n", $skel, $entity);
134 * ユーザ定義のマクロを設定する(~/.ethna)
138 function _getUserMacro()
140 if (isset($_SERVER['USERPROFILE']) && is_dir($_SERVER['USERPROFILE'])) {
141 $home = $_SERVER['USERPROFILE'];
143 $home = $_SERVER['HOME'];
146 if (is_file("$home/.ethna") == false) {
150 $user_macro = parse_ini_file("$home/.ethna");