OSDN Git Service

6a4d1b35cdd5cfb102e9182e3c6bc5f96f85e4f0
[ethna/ethna.git] / class / Ethna_SkeltonGenerator.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_SkeltonGenerator.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_SkeltonGenerator
13 /**
14  *  ¥¹¥±¥ë¥È¥óÀ¸À®¥¯¥é¥¹
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_SkeltonGenerator
21 {
22     /**
23      *  ¥×¥í¥¸¥§¥¯¥È¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
24      *
25      *  @access public
26      *  @param  string  $basedir    ¥×¥í¥¸¥§¥¯¥È¥Ù¡¼¥¹¥Ç¥£¥ì¥¯¥È¥ê
27      *  @param  string  $id         ¥×¥í¥¸¥§¥¯¥ÈID
28      *  @return bool    true:À®¸ù false:¼ºÇÔ
29      */
30     function generateProjectSkelton($basedir, $id)
31     {
32         $dir_list = array(
33             array("app", 0755),
34             array("app/action", 0755),
35             array("app/action_cli", 0755),
36             array("app/action_xmlrpc", 0755),
37             array("app/filter", 0755),
38             array("app/plugin", 0755),
39             array("app/plugin/Filter", 0755),
40             array("app/plugin/Validator", 0755),
41             array("app/view", 0755),
42             array("bin", 0755),
43             array("etc", 0755),
44             array("lib", 0755),
45             array("locale", 0755),
46             array("locale/ja", 0755),
47             array("locale/ja/LC_MESSAGES", 0755),
48             array("log", 0777),
49             array("schema", 0755),
50             array("skel", 0755),
51             array("template", 0755),
52             array("template/ja", 0755),
53             array("tmp", 0777),
54             array("www", 0755),
55             array("www/css", 0755),
56             array("www/js", 0755),
57         );
58
59         $r = Ethna_Controller::checkAppId($id);
60         if (Ethna::isError($r)) {
61             return $r;
62         }
63
64         $basedir = sprintf("%s/%s", $basedir, strtolower($id));
65
66         // ¥Ç¥£¥ì¥¯¥È¥êºîÀ®
67         if (is_dir($basedir) == false) {
68             // confirm
69             printf("creating directory ($basedir) [y/n]: ");
70             flush();
71             $fp = fopen("php://stdin", "r");
72             $r = trim(fgets($fp, 128));
73             fclose($fp);
74             if (strtolower($r) != 'y') {
75                 return Ethna::raiseError('aborted by user');
76             }
77
78             if (mkdir($basedir, 0775) == false) {
79                 return Ethna::raiseError('directory creation failed');
80             }
81         }
82         foreach ($dir_list as $dir) {
83             $mode = $dir[1];
84             $dir = $dir[0];
85             $target = "$basedir/$dir";
86             if (is_dir($target)) {
87                 printf("%s already exists -> skipping...\n", $target);
88                 continue;
89             }
90             if (mkdir($target, $mode) == false) {
91                 return Ethna::raiseError('directory creation failed');
92             } else {
93                 printf("project sub directory created [%s]\n", $target);
94             }
95             if (chmod($target, $mode) == false) {
96                 return Ethna::raiseError('chmod failed');
97             }
98         }
99
100         // ¥¹¥±¥ë¥È¥ó¥Õ¥¡¥¤¥ëºîÀ®
101         $macro['application_id'] = strtoupper($id);
102         $macro['project_id'] = ucfirst($id);
103         $macro['project_prefix'] = strtolower($id);
104         $macro['basedir'] = realpath($basedir);
105
106         $macro['action_class'] = '{$action_class}';
107         $macro['action_form'] = '{$action_form}';
108         $macro['action_name'] = '{$action_name}';
109         $macro['action_path'] = '{$action_path}';
110         $macro['forward_name'] = '{$forward_name}';
111         $macro['view_name'] = '{$view_name}';
112         $macro['view_path'] = '{$view_path}';
113
114         $user_macro = $this->_getUserMacro();
115         $default_macro = $macro;
116         $macro = array_merge($macro, $user_macro);
117
118         // the longest if? :)
119         if ($this->_generateFile("www.index.php", "$basedir/www/index.php", $macro) == false ||
120             $this->_generateFile("www.info.php", "$basedir/www/info.php", $macro) == false ||
121             $this->_generateFile("www.unittest.php", "$basedir/www/unittest.php", $macro) == false ||
122             $this->_generateFile("www.xmlrpc.php", "$basedir/www/xmlrpc.php", $macro) == false ||
123             $this->_generateFile("www.css.ethna.css", "$basedir/www/css/ethna.css", $macro) == false ||
124             $this->_generateFile("dot.ethna", "$basedir/.ethna", $macro) == false ||
125             $this->_generateFile("app.controller.php", sprintf("$basedir/app/%s_Controller.php", $macro['project_id']), $macro) == false ||
126             $this->_generateFile("app.error.php", sprintf("$basedir/app/%s_Error.php", $macro['project_id']), $macro) == false ||
127             $this->_generateFile("app.action.default.php", "$basedir/app/action/Index.php", $macro) == false ||
128             $this->_generateFile("app.plugin.filter.default.php", sprintf("$basedir/app/plugin/Filter/%s_Plugin_Filter_ExecutionTime.php", $macro['project_id']), $macro) == false ||
129             $this->_generateFile("app.view.default.php", "$basedir/app/view/Index.php", $macro) == false ||
130             $this->_generateFile("app.unittest.php", sprintf("$basedir/app/%s_UnitTestManager.php", $macro['project_id']), $macro) == false ||
131             $this->_generateFile("etc.ini.php", sprintf("$basedir/etc/%s-ini.php", $macro['project_prefix']), $macro) == false ||
132             $this->_generateFile("skel.action.php", sprintf("$basedir/skel/skel.action.php"), $default_macro) == false ||
133             $this->_generateFile("skel.action_cli.php", sprintf("$basedir/skel/skel.action_cli.php"), $default_macro) == false ||
134             $this->_generateFile("skel.action_test.php", sprintf("$basedir/skel/skel.action_test.php"), $default_macro) == false ||
135             $this->_generateFile("skel.app_object.php", sprintf("$basedir/skel/skel.app_object.php"), $default_macro) == false ||
136             $this->_generateFile("skel.cli.php", sprintf("$basedir/skel/skel.cli.php"), $default_macro) == false ||
137             $this->_generateFile("skel.view.php", sprintf("$basedir/skel/skel.view.php"), $default_macro) == false ||
138             $this->_generateFile("skel.template.tpl", sprintf("$basedir/skel/skel.template.tpl"), $default_macro) == false ||
139             $this->_generateFile("skel.view_test.php", sprintf("$basedir/skel/skel.view_test.php"), $default_macro) == false ||
140             $this->_generateFile("template.index.tpl", sprintf("$basedir/template/ja/index.tpl"), $default_macro) == false) {
141             return Ethna::raiseError('generating files failed');
142         }
143
144         return true;
145     }
146
147     /**
148      *  ¥¢¥¯¥·¥ç¥ó¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
149      *
150      *  @access public
151      *  @param  string  $action_name    ¥¢¥¯¥·¥ç¥ó̾
152      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
153      *  @param  int     $gateway        ¥²¡¼¥È¥¦¥§¥¤
154      *  @return bool    true:À®¸ù false:¼ºÇÔ
155      */
156     function generateActionSkelton($action_name, $app_dir, $gateway = GATEWAY_WWW)
157     {
158         // discover controller
159         $controller_class = $this->_discoverController($app_dir);
160         if (Ethna::isError($controller_class)) {
161             return $controller_class;
162         }
163
164         $c =& new $controller_class;
165         $c->setGateway(GATEWAY_CLI);
166
167         $action_dir = $c->getActiondir($gateway);
168         $action_class = $c->getDefaultActionClass($action_name, $gateway);
169         $action_form = $c->getDefaultFormClass($action_name, $gateway);
170         $action_path = $c->getDefaultActionPath($action_name);
171
172         $macro = array();
173         $macro['project_id'] = $c->getAppId();
174         $macro['action_name'] = $action_name;
175         $macro['action_class'] = $action_class;
176         $macro['action_form'] = $action_form;
177         $macro['action_path'] = $action_path;
178
179         $user_macro = $this->_getUserMacro();
180         $macro = array_merge($macro, $user_macro);
181
182         $this->_mkdir(dirname("$action_dir$action_path"), 0755);
183
184         switch ($gateway) {
185         case GATEWAY_WWW:
186             $skelton = "skel.action.php";
187             break;
188         case GATEWAY_CLI:
189             $skelton = "skel.action_cli.php";
190             break;
191         case GATEWAY_XMLRPC:
192             $skelton = "skel.action_xmlrpc.php";
193             break;
194         }
195
196         if (file_exists("$action_dir$action_path")) {
197             printf("file [%s] already exists -> skip\n", "$action_dir$action_path");
198         } else if ($this->_generateFile($skelton, "$action_dir$action_path", $macro) == false) {
199             printf("[warning] file creation failed [%s]\n", "$action_dir$action_path");
200         } else {
201             printf("action script(s) successfully created [%s]\n", "$action_dir$action_path");
202         }
203     }
204
205     /**
206      *  ¥Ó¥å¡¼¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
207      *
208      *  @access public
209      *  @param  string  $forward_name   ¥¢¥¯¥·¥ç¥ó̾
210      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
211      *  @return bool    true:À®¸ù false:¼ºÇÔ
212      */
213     function generateViewSkelton($forward_name, $app_dir)
214     {
215         // discover controller
216         $controller_class = $this->_discoverController($app_dir);
217         if (Ethna::isError($controller_class)) {
218             return $controller_class;
219         }
220
221         $c =& new $controller_class;
222         $c->setGateway(GATEWAY_CLI);
223
224         $view_dir = $c->getViewdir();
225         $view_class = $c->getDefaultViewClass($forward_name, false);
226         $view_path = $c->getDefaultViewPath($forward_name, false);
227
228         $macro = array();
229         $macro['project_id'] = $c->getAppId();
230         $macro['forward_name'] = $forward_name;
231         $macro['view_class'] = $view_class;
232         $macro['view_path'] = $view_path;
233
234         $user_macro = $this->_getUserMacro();
235         $macro = array_merge($macro, $user_macro);
236
237         $this->_mkdir(dirname("$view_dir/$view_path"), 0755);
238
239         if (file_exists("$view_dir$view_path")) {
240             printf("file [%s] already exists -> skip\n", "$view_dir$view_path");
241         } else if ($this->_generateFile("skel.view.php", "$view_dir$view_path", $macro) == false) {
242             printf("[warning] file creation failed [%s]\n", "$view_dir$view_path");
243         } else {
244             printf("view script(s) successfully created [%s]\n", "$view_dir$view_path");
245         }
246     }
247
248     /**
249      *  CLI¥¨¥ó¥È¥ê¥Ý¥¤¥ó¥È¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
250      *
251      *  @access public
252      *  @param  string  $forward_name   ¥¢¥¯¥·¥ç¥ó̾
253      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
254      *  @return bool    true:À®¸ù false:¼ºÇÔ
255      */
256     function generateCliSkelton($action_name, $app_dir)
257     {
258         // discover controller
259         $controller_class = $this->_discoverController($app_dir);
260         if (Ethna::isError($controller_class)) {
261             return $controller_class;
262         }
263
264         $c =& new $controller_class;
265         $c->setGateway(GATEWAY_CLI);
266
267         $app_dir = $c->getDirectory('app');
268         $bin_dir = $c->getDirectory('bin');
269         $cli_file = sprintf("%s/%s.%s", $bin_dir, $action_name, $c->getExt('php'));
270
271         $macro = array();
272         $macro['project_id'] = $c->getAppId();
273         $macro['action_name'] = $action_name;
274         $macro['dir_app'] = $app_dir;
275         $macro['dir_bin'] = $bin_dir;
276
277         $user_macro = $this->_getUserMacro();
278         $macro = array_merge($macro, $user_macro);
279
280         if (file_exists($cli_file)) {
281             printf("file [%s] already exists -> skip\n", $cli_file);
282         } else if ($this->_generateFile("skel.cli.php", $cli_file, $macro) == false) {
283             printf("[warning] file creation failed [%s]\n", $cli_file);
284         } else {
285             printf("action script(s) successfully created [%s]\n", $cli_file);
286         }
287     }
288
289     /**
290      *  ¥Æ¥ó¥×¥ì¡¼¥È¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
291      *
292      *  @access public
293      *  @param  string  $forward_name   ¥¢¥¯¥·¥ç¥ó̾
294      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
295      *  @return bool    true:À®¸ù false:¼ºÇÔ
296      */
297     function generateTemplateSkelton($forward_name, $app_dir)
298     {
299         // discover controller
300         $controller_class = $this->_discoverController($app_dir);
301         if (Ethna::isError($controller_class)) {
302             return $controller_class;
303         }
304
305         $c =& new $controller_class;
306         $c->setGateway(GATEWAY_CLI);
307
308         $tpl_dir = $c->getTemplatedir();
309         if ($tpl_dir{strlen($tpl_dir)-1} != '/') {
310             $tpl_dir .= '/';
311         }
312         $tpl_path = $c->getDefaultForwardPath($forward_name);
313
314         $macro = array();
315         // add '_' for tpl and no user macro for tpl
316         $macro['_project_id'] = $c->getAppId();
317
318         $this->_mkdir(dirname("$tpl_dir/$tpl_path"), 0755);
319
320         if (file_exists("$tpl_dir$tpl_path")) {
321             printf("file [%s] already exists -> skip\n", "$tpl_dir$tpl_path");
322         } else if ($this->_generateFile("skel.template.tpl", "$tpl_dir$tpl_path", $macro) == false) {
323             printf("[warning] file creation failed [%s]\n", "$tpl_dir$tpl_path");
324         } else {
325             printf("template file(s) successfully created [%s]\n", "$tpl_dir$tpl_path");
326         }
327     }
328
329     /**
330      *  ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
331      *
332      *  @access public
333      *  @param  string  $table_name     ¥Æ¡¼¥Ö¥ë̾
334      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
335      *  @return bool    true:À®¸ù false:¼ºÇÔ
336      */
337     function generateAppObjectSkelton($table_name, $app_dir)
338     {
339         // discover controller
340         $controller_class = $this->_discoverController($app_dir);
341         if (Ethna::isError($controller_class)) {
342             return $controller_class;
343         }
344
345         $c =& new $controller_class;
346         $c->setGateway(GATEWAY_CLI);
347
348         $table_id = preg_replace('/_(.)/e', "strtoupper('\$1')", ucfirst($table_name));
349
350         $app_dir = $c->getDirectory('app');
351         $app_path = ucfirst($c->getAppId()) . '_' . $table_id .'.php';
352
353         $macro = array();
354         $macro['project_id'] = $c->getAppId();
355         $macro['app_path'] = $app_path;
356         $macro['app_object'] = ucfirst($c->getAppId()) . '_' . $table_id;
357
358         $user_macro = $this->_getUserMacro();
359         $macro = array_merge($macro, $user_macro);
360
361         $path = "$app_dir/$app_path";
362         $this->_mkdir(dirname($path), 0755);
363         if (file_exists($path)) {
364             printf("file [%s] already exists -> skip\n", $path);
365         } else if ($this->_generateFile("skel.app_object.php", $path, $macro) == false) {
366             printf("[warning] file creation failed [%s]\n", $path);
367         } else {
368             printf("app-object script(s) successfully created [%s]\n", $path);
369         }
370     }
371
372     /**
373      *  ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥Þ¥Í¡¼¥¸¥ã¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
374      *
375      *  @access public
376      *  @param  string  $manager_name    ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥Þ¥Í¡¼¥¸Ì¾
377      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
378      *  @return bool    true:À®¸ù false:¼ºÇÔ
379      */
380     function generateAppManagerSkelton($manager_name, $app_dir)
381     {
382         // discover controller
383         $controller_class = $this->_discoverController($app_dir);
384         if (Ethna::isError($controller_class)) {
385             return $controller_class;
386         }
387
388         $c =& new $controller_class;
389         $c->setGateway(GATEWAY_CLI);
390
391         $manager_id = preg_replace('/_(.)/e', "strtoupper('\$1')", ucfirst($manager_name));
392
393         $app_dir = $c->getDirectory('app');
394         $app_path = ucfirst($c->getAppId()) . '_' . $manager_id .'Manager.php';
395
396         $macro = array();
397         $macro['project_id'] = $c->getAppId();
398         $macro['app_path'] = $app_path;
399         $macro['app_manager'] = ucfirst($c->getAppId()) . '_' . $manager_id;
400
401         $user_macro = $this->_getUserMacro();
402         $macro = array_merge($macro, $user_macro);
403
404         $path = "$app_dir/$app_path";
405         $this->_mkdir(dirname($path), 0755);
406         if (file_exists($path)) {
407             printf("file [%s] already exists -> skip\n", $path);
408         } else if ($this->_generateFile("skel.app_manager.php", $path, $macro) == false) {
409             printf("[warning] file creation failed [%s]\n", $path);
410         } else {
411             printf("app-manager script(s) successfully created [%s]\n", $path);
412         }
413     }
414
415     /**
416      *  ¥¢¥¯¥·¥ç¥óÍѥƥ¹¥È¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
417      *
418      *  @access public
419      *  @param  string  $action_name    ¥¢¥¯¥·¥ç¥ó̾
420      *  @param  string  $app_dir        ¥×¥í¥¸¥§¥¯¥È¥Ç¥£¥ì¥¯¥È¥ê
421      *  @return bool    true:À®¸ù false:¼ºÇÔ
422      */
423     function generateActionTestSkelton($action_name, $app_dir, $gateway = GATEWAY_WWW)
424     {
425         // discover controller
426         $controller_class = $this->_discoverController($app_dir);
427         if (Ethna::isError($controller_class)) {
428             return $controller_class;
429         }
430
431         $c =& new $controller_class;
432         $c->setGateway(GATEWAY_CLI);
433
434         $action_dir = $c->getActiondir($gateway);
435         $action_class = $c->getDefaultActionClass($action_name, false);
436         $action_form = $c->getDefaultFormClass($action_name, false);
437         $action_path = $c->getDefaultActionPath($action_name . "Test", false);
438
439         $macro = array();
440         $macro['project_id'] = $c->getAppId();
441         $macro['action_name'] = $action_name;
442         $macro['action_class'] = $action_class;
443         $macro['action_form'] = $action_form;
444         $macro['action_path'] = $action_path;
445
446         $user_macro = $this->_getUserMacro();
447         $macro = array_merge($macro, $user_macro);
448
449         $this->_mkdir(dirname("$action_dir$action_path"), 0755);
450
451         if (file_exists("$action_dir$action_path")) {
452             printf("file [%s] aleady exists -> skip\n", "$action_dir$action_path");
453         } else if ($this->_generateFile("skel.action_test.php", "$action_dir$action_path", $macro) == false) {
454             printf("[warning] file creation failed [%s]\n", "$action_dir$action_path");
455         } else {
456             printf("action test(s) successfully created [%s]\n", "$action_dir$action_path");
457         }
458     }
459
460     /**
461      *  ¥Ó¥å¡¼Íѥƥ¹¥È¤Î¥¹¥±¥ë¥È¥ó¤òÀ¸À®¤¹¤ë
462      *
463      *  @access public
464      *  @param  string  $forward_name   ¥¢¥¯¥·¥ç¥ó̾
465      *  @return bool    true:À®¸ù false:¼ºÇÔ
466      */
467     function generateViewTestSkelton($forward_name, $app_dir)
468     {
469         // discover controller
470         $controller_class = $this->_discoverController($app_dir);
471         if (Ethna::isError($controller_class)) {
472             return $controller_class;
473         }
474
475         $c =& new $controller_class;
476         $c->setGateway(GATEWAY_CLI);
477
478         $view_dir = $c->getViewdir();
479         $view_class = $c->getDefaultViewClass($forward_name, false);
480         $view_path = $c->getDefaultViewPath($forward_name . "Test", false);
481
482         $macro = array();
483         $macro['project_id'] = $c->getAppId();
484         $macro['forward_name'] = $forward_name;
485         $macro['view_class'] = $view_class;
486         $macro['view_path'] = $view_path;
487
488         $user_macro = $this->_getUserMacro();
489         $macro = array_merge($macro, $user_macro);
490
491         $this->_mkdir(dirname("$view_dir/$view_path"), 0755);
492
493         if (file_exists("$view_dir$view_path")) {
494             printf("file [%s] aleady exists -> skip\n", "$view_dir$view_path");
495         } else if ($this->_generateFile("skel.view_test.php", "$view_dir$view_path", $macro) == false) {
496             printf("[warning] file creation failed [%s]\n", "$view_dir$view_path");
497         } else {
498             printf("view test(s) successfully created [%s]\n", "$view_dir$view_path");
499         }
500     }
501
502     /**
503      *  mkdir -p
504      *
505      *  @access private
506      *  @param  string  $dir    ºîÀ®¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê
507      *  @param  int     $mode   ¥Ñ¡¼¥ß¥Ã¥·¥ç¥ó
508      *  @return bool    true:À®¸ù false:¼ºÇÔ
509      */
510     function _mkdir($dir, $mode)
511     {
512         if (@is_dir($dir)) {
513             return true;
514         }
515
516         $parent = dirname($dir);
517         if ($dir == $parent) {
518             return true;
519         }
520         if (is_dir($parent) == false) {
521             $this->_mkdir($parent, $mode);
522         }
523
524         return mkdir($dir, $mode);
525     }
526
527     /**
528      *  ¥¹¥±¥ë¥È¥ó¥Õ¥¡¥¤¥ë¤Ë¥Þ¥¯¥í¤òŬÍѤ·¤Æ¥Õ¥¡¥¤¥ë¤òÀ¸À®¤¹¤ë
529      *
530      *  ethna¥é¥¤¥Ö¥é¥ê¤Î¥Ç¥£¥ì¥¯¥È¥ê¹½Â¤¤¬Êѹ¹¤µ¤ì¤Æ¤¤¤Ê¤¤¤³¤È¤¬Á°Äó
531      *  ¤È¤Ê¤Ã¤Æ¤¤¤ëÅÀ¤ËÃí°Õ
532      *
533      *  @access private
534      *  @param  string  $skel       ¥¹¥±¥ë¥È¥ó¥Õ¥¡¥¤¥ë
535      *  @param  string  $entity     À¸À®¥Õ¥¡¥¤¥ë̾
536      *  @param  array   $macro      ÃÖ´¹¥Þ¥¯¥í
537      *  @return bool    true:Àµ¾ï½ªÎ» false:¥¨¥é¡¼
538      */
539     function _generateFile($skel, $entity, $macro)
540     {
541         $base = null;
542
543         if (file_exists($entity)) {
544             printf("file [%s] already exists -> skip\n", $entity);
545             return true;
546         }
547         $c =& Ethna_Controller::getInstance();
548         if (is_object($c)) {
549             $base = $c->getBasedir();
550             if (file_exists("$base/skel/$skel") == false) {
551                 $base = null;
552             }
553         }
554         if (is_null($base)) {
555             $base = dirname(dirname(__FILE__));
556         }
557
558         $rfp = fopen("$base/skel/$skel", "r");
559         if ($rfp == null) {
560             return false;
561         }
562         $wfp = fopen($entity, "w");
563         if ($wfp == null) {
564             fclose($rfp);
565             return false;
566         }
567
568         for (;;) {
569             $s = fread($rfp, 4096);
570             if (strlen($s) == 0) {
571                 break;
572             }
573
574             foreach ($macro as $k => $v) {
575                 $s = preg_replace("/{\\\$$k}/", $v, $s);
576             }
577             fwrite($wfp, $s);
578         }
579
580         fclose($wfp);
581         fclose($rfp);
582
583         $st = stat("$base/skel/$skel");
584         if (chmod($entity, $st[2]) == false) {
585             return false;
586         }
587
588         printf("file generated [%s -> %s]\n", $skel, $entity);
589
590         return true;
591     }
592
593     /**
594      *  ¥æ¡¼¥¶ÄêµÁ¤Î¥Þ¥¯¥í¤òÀßÄꤹ¤ë(~/.ethna)
595      *
596      *  @access private
597      */
598     function _getUserMacro()
599     {
600         if (isset($_SERVER['USERPROFILE']) && is_dir($_SERVER['USERPROFILE'])) {
601             $home = $_SERVER['USERPROFILE'];
602         } else {
603             $home = $_SERVER['HOME'];
604         }
605
606         if (is_file("$home/.ethna") == false) {
607             return array();
608         }
609
610         $user_macro = parse_ini_file("$home/.ethna");
611         return $user_macro;
612     }
613
614     /**
615      *  ¥³¥ó¥È¥í¡¼¥é¥Õ¥¡¥¤¥ë/¥¯¥é¥¹¤ò¸¡º÷¤¹¤ë
616      *
617      *  @access private
618      */
619     function _discoverController($app_dir)
620     {
621         $ini_file = null;
622         while (is_dir($app_dir) && $app_dir != "/") {
623             if (is_file("$app_dir/.ethna")) {
624                 $ini_file = "$app_dir/.ethna";
625                 break;
626             }
627             $app_dir = dirname($app_dir);
628         }
629
630         if ($ini_file === null) {
631             return Ethna::raiseError('no .ethna file found');
632         }
633         
634         $macro = parse_ini_file($ini_file);
635         if (isset($macro['controller_file']) == false || isset($macro['controller_class']) == false) {
636             return Ethna::raiseError('invalid .ethna file');
637         }
638         $file = $macro['controller_file'];
639         $class = $macro['controller_class'];
640
641         $controller_file = "$app_dir/$file";
642         if (is_file($controller_file) == false) {
643             return Ethna::raiseError("no such file $controller_file");
644         }
645
646         include_once($controller_file);
647         if (class_exists($class) == false) {
648             return Ethna::raiseError("no such class $class");
649         }
650
651         return $class;
652     }
653 }
654 // }}}
655 ?>