OSDN Git Service

Remove PHP closing tag
[ethna/ethna.git] / class / Plugin / Handle / AddView.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  AddView.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 require_once 'Ethna/class/Plugin/Handle/AddAction.php';
13
14 // {{{ Ethna_Plugin_Handle_AddView
15 /**
16  *  add-view handler
17  *
18  *  @author     Masaki Fujimoto <fujimoto@php.net>
19  *  @access     public
20  *  @package    Ethna
21  */
22 class Ethna_Plugin_Handle_AddView extends Ethna_Plugin_Handle_AddAction
23 {
24     /**
25      *  add view
26      *
27      *  @access public
28      */
29     function perform()
30     {
31         //
32         //  '-w[with-unittest]' and '-u[unittestskel]' option
33         //  are not intuisive, but I dare to define them because
34         //  -t and -s option are reserved by add-[action|view] handle
35         //  and Ethna_Getopt cannot interpret two-character option.
36         //
37         $r =& $this->_getopt(
38                   array('basedir=',
39                         'skelfile=',
40                         'with-unittest',
41                         'unittestskel=',
42                         'template',
43                         'locale=',
44                         'encoding=',
45                   )
46               );
47         if (Ethna::isError($r)) {
48             return $r;
49         }
50         list($opt_list, $arg_list) = $r;
51
52         // view_name
53         $view_name = array_shift($arg_list);
54         if ($view_name == null) {
55             return Ethna::raiseError('view name isn\'t set.', 'usage');
56         }
57         $r =& Ethna_Controller::checkViewName($view_name);
58         if (Ethna::isError($r)) {
59             return $r;
60         }
61
62         // add view(invoke parent class method)
63         $ret =& $this->_perform('View', $view_name, $opt_list);
64         if (Ethna::isError($ret) || $ret === false) { 
65             return $ret;
66         }
67
68         // add template
69         if (isset($opt_list['template'])) {
70             $ret =& $this->_performTemplate($view_name, $opt_list);
71             if (Ethna::isError($ret) || $ret === false) { 
72                 return $ret;
73             }
74         }
75
76         return true;
77     }
78
79     /**
80      *  Special Function for generating template.
81      *
82      *  @param  string $target_name Template Name
83      *  @param  array  $opt_list    Option List.
84      *  @access protected
85      */
86     function &_performTemplate($target_name, $opt_list)
87     {
88         // basedir
89         if (isset($opt_list['basedir'])) {
90             $basedir = realpath(end($opt_list['basedir']));
91         } else {
92             $basedir = getcwd();
93         }
94
95         // skelfile
96         if (isset($opt_list['skelfile'])) {
97             $skelfile = end($opt_list['skelfile']);
98         } else {
99             $skelfile = null;
100         }
101
102         // locale
103         $ctl =& Ethna_Handle::getAppController(getcwd());
104         if (isset($opt_list['locale'])) {
105             $locale = end($opt_list['locale']);
106             if (!preg_match('/^[A-Za-z_]+$/', $locale)) {
107                 return Ethna::raiseError("You specified locale, but invalid : $locale", 'usage');
108             }
109         } else {
110             if (Ethna::isError($ctl)) {
111                 $locale = 'ja_JP';
112             } else {
113                 $locale = $ctl->getLocale();
114             }
115         }
116
117         // encoding
118         if (isset($opt_list['encoding'])) {
119             $encoding = end($opt_list['encoding']);
120             if (function_exists('mb_list_encodings')) {
121                 $supported_enc = mb_list_encodings();
122                 if (!in_array($encoding, $supported_enc)) {
123                     return Ethna::raiseError("Unknown Encoding : $encoding", 'usage');
124                 }
125             }
126         } else {
127             if (Ethna::isError($ctl)) {
128                 $encoding = 'UTF-8';
129             } else {
130                 $encoding = $ctl->getClientEncoding();
131             }
132         }
133
134         $r =& Ethna_Generator::generate('Template', $basedir,
135                                         $target_name, $skelfile, $locale, $encoding);
136         if (Ethna::isError($r)) {
137             printf("error occurred while generating skelton. please see also following error message(s)\n\n");
138             return $r;
139         }
140
141         $true = true;
142         return $true;
143     }
144
145     /**
146      *  get handler's description
147      *
148      *  @access public
149      */
150     function getDescription()
151     {
152         return <<<EOS
153 add new view to project:
154     {$this->id} [options... ] [view name]
155     [options ...] are as follows.
156         [-b|--basedir=dir] [-s|--skelfile=file]
157         [-w|--with-unittest] [-u|--unittestskel=file]
158         [-t|--template] [-l|--locale] [-e|--encoding]
159     NOTICE: "-w" and "-u" options are ignored when you specify -t option.
160             "-l" and "-e" options are enabled when you specify -t option.
161
162 EOS;
163     }
164
165     /**
166      *  @access public
167      */
168     function getUsage()
169     {
170         return <<<EOS
171 ethna {$this->id} [options... ] [view name]
172     [options ...] are as follows.
173         [-b|--basedir=dir] [-s|--skelfile=file]
174         [-w|--with-unittest] [-u|--unittestskel=file]
175         [-t|--template] [-l|--locale] [-e|--encoding]
176     NOTICE: "-w" and "-u" options are ignored when you specify -t option.
177             "-l" and "-e" options are enabled when you specify -t option.
178 EOS;
179     }
180 }
181 // }}}