OSDN Git Service

Abstract の追加にしたがってプラグインの基底を変更
[ethna/ethna.git] / class / Plugin / Generator.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Generator.php
5  *
6  *  @author     Masaki Fujimoto <fujimoto@php.net>
7  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
8  *  @package    Ethna
9  *  @version    $Id$
10  */
11
12 // {{{ Ethna_Plugin_Generator
13 /**
14  *  スケルトン生成プラグイン
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_Plugin_Generator extends Ethna_Plugin_Abstract
21 {
22     /** @var    object  Ethna_Controller    スケルトン生成に使うコントローラ */
23     //var $ctl;
24
25     /**
26      *  コンストラクタ
27      *
28      *  @access public
29      */
30     /*
31     function Ethna_Plugin_Generator(&$controller, $type, $name)
32     {
33         // Ethna_Generatorでpluginを取得するときに使ったコントローラ
34         // ex, add-projectではEthna_Controller, add-actionではApp_Controller
35         $this->ctl =& $controller;
36     }
37     */
38
39     /**
40      *  スケルトンファイルの絶対パスを解決する
41      *
42      *  @access private
43      *  @param  string  $skel   スケルトンファイル
44      */
45     function _resolveSkelfile($skel)
46     {
47         $file = realpath($skel);
48         if (file_exists($file)) {
49             return $file;
50         }
51
52         // アプリの skel ディレクトリ
53         $base = $this->ctl->getBasedir();
54         $file = "$base/skel/$skel";
55         if (file_exists($file)) {
56             return $file;
57         }
58
59         // Ethna本体の skel ディレクトリ
60         $base = dirname(dirname(dirname(__FILE__)));
61         $file = "$base/skel/$skel";
62         if (file_exists($file)) {
63             return $file;
64         }
65
66         return false;
67     }
68
69     /**
70      *  スケルトンファイルにマクロを適用してファイルを生成する
71      *
72      *  @access private
73      *  @param  string  $skel       スケルトンファイル
74      *  @param  string  $entity     生成ファイル名
75      *  @param  array   $macro      置換マクロ
76      *  @param  bool    $overwrite  上書きフラグ
77      *  @return bool    true:正常終了 false:エラー
78      */
79     function _generateFile($skel, $entity, $macro, $overwrite = false)
80     {
81         if (file_exists($entity)) {
82             if ($overwrite === false) {
83                 printf("file [%s] already exists -> skip\n", $entity);
84                 return true;
85             } else {
86                 printf("file [%s] already exists, to be overwriten.\n", $entity);
87             }
88         }
89
90         $resolved = $this->_resolveSkelfile($skel);
91         if ($resolved === false) {
92             printf("skelton file [%s] not found.\n", $skel);
93             return false;
94         } else {
95             $skel = $resolved;
96         }
97
98         $rfp = fopen($skel, "r");
99         if ($rfp == null) {
100             return false;
101         }
102         $wfp = fopen($entity, "w");
103         if ($wfp == null) {
104             fclose($rfp);
105             return false;
106         }
107
108         for (;;) {
109             $s = fread($rfp, 4096);
110             if (strlen($s) == 0) {
111                 break;
112             }
113
114             foreach ($macro as $k => $v) {
115                 $s = preg_replace("/{\\\$$k}/", $v, $s);
116             }
117             fwrite($wfp, $s);
118         }
119
120         fclose($wfp);
121         fclose($rfp);
122
123         $st = stat($skel);
124         if (chmod($entity, $st[2]) == false) {
125             return false;
126         }
127
128         printf("file generated [%s -> %s]\n", $skel, $entity);
129
130         return true;
131     }
132
133     /**
134      *  ユーザ定義のマクロを設定する(~/.ethna)
135      *
136      *  @access private
137      */
138     function _getUserMacro()
139     {
140         if (isset($_SERVER['USERPROFILE']) && is_dir($_SERVER['USERPROFILE'])) {
141             $home = $_SERVER['USERPROFILE'];
142         } else {
143             $home = $_SERVER['HOME'];
144         }
145
146         if (is_file("$home/.ethna") == false) {
147             return array();
148         }
149
150         $user_macro = parse_ini_file("$home/.ethna");
151         return $user_macro;
152     }
153 }
154 // }}}
155 ?>