OSDN Git Service

4242bed481ad07a865caec205c91953c07abb575
[ethna/ethna.git] / class / Plugin / Handle / AddProject.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  AddProject.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_Handle_AddProject
13 /**
14  *  add-project handler
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_Plugin_Handle_AddProject extends Ethna_Plugin_Handle
21 {
22     /**
23      *  add project:)
24      *
25      *  @access public
26      */
27     function perform()
28     {
29         $r = $this->_getopt(array('basedir=', 'skeldir=', 'locale=', 'encoding='));
30         if (Ethna::isError($r)) {
31             return $r;
32         }
33         list($opt_list, $arg_list) = $r;
34
35         // app_id
36         $app_id = array_shift($arg_list);
37         if ($app_id == null) {
38             return Ethna::raiseError('Application id isn\'t set.', 'usage');
39         }
40         $r = Ethna_Controller::checkAppId($app_id);
41         if (Ethna::isError($r)) {
42             return $r;
43         }
44
45         // basedir
46         if (isset($opt_list['basedir'])) {
47             $dir = end($opt_list['basedir']);
48             $basedir = realpath($dir);
49             if ($basedir === false) {  //  e.x file does not exist
50                 $basedir = $dir;
51             }
52         } else {
53             $basedir = sprintf("%s/%s", getcwd(), strtolower($app_id));
54         }
55
56         // skeldir
57         if (isset($opt_list['skeldir'])) {
58             $selected_dir = end($opt_list['skeldir']);
59             $skeldir = realpath($selected_dir);
60             if ($skeldir == false || is_dir($skeldir) == false || file_exists($skeldir) == false) {
61                 return Ethna::raiseError("You specified skeldir, but invalid : $selected_dir", 'usage');
62             }
63         } else {
64             $skeldir = null;
65         }
66
67         // locale
68         if (isset($opt_list['locale'])) {
69             $locale = end($opt_list['locale']);
70             if (!preg_match('/^[A-Za-z_]+$/', $locale)) {
71                 return Ethna::raiseError("You specified locale, but invalid : $locale", 'usage');
72             }
73         } else {
74             $locale = 'ja_JP';  //  default locale. 
75         }
76
77         // encoding
78         if (isset($opt_list['encoding'])) {
79             $encoding = end($opt_list['encoding']);
80             if (function_exists('mb_list_encodings')) {
81                 $supported_enc = mb_list_encodings();
82                 if (!in_array($encoding, $supported_enc)) {
83                     return Ethna::raiseError("Unknown Encoding : $encoding", 'usage');
84                 }
85             }
86         } else {
87             $encoding = 'UTF-8';  //  default encoding. 
88         }
89
90         $r = Ethna_Generator::generate('Project', null, $app_id, $basedir, $skeldir, $locale, $encoding);
91         if (Ethna::isError($r)) {
92             printf("error occurred while generating skelton. please see also error messages given above\n\n");
93             return $r;
94         }
95
96         printf("\nproject skelton for [%s] is successfully generated at [%s]\n\n", $app_id, $basedir);
97         return true;
98     }
99
100     /**
101      *  get handler's description
102      *
103      *  @access public
104      */
105     function getDescription()
106     {
107         return <<<EOS
108 add new project:
109     {$this->id} [-b|--basedir=dir] [-s|--skeldir] [-l|--locale] [-e|--encoding] [Application id]
110
111 EOS;
112     }
113
114     /**
115      *  get usage
116      *
117      *  @access public
118      */
119     function getUsage()
120     {
121         return <<<EOS
122 ethna {$this->id} [-b|--basedir=dir] [-s|--skeldir] [-l|--locale] [-e|--encoding] [Application id]
123 EOS;
124     }
125 }
126 // }}}
127 ?>