HTML/FlexyFramework/Cli.php
[pear] / HTML / FlexyFramework / Cli.php
1 <?php
2
3 /**
4  *
5  * handles the cli features of Flexy Framework.
6  *
7  *
8  * usage :
9  *
10  *   $x = new HTML_FlexyFramework_Cli($ff);
11  *   $x->cliHelp(); // summary of all classes which can be run with cli.
12  *             (contain static $cli_desc)
13  *   $x->cliParse($classname);
14  *
15  *
16  */
17 class HTML_FlexyFramework_Cli
18 {
19     
20     /**
21      * Default options that allow modification of the framewoek behaviour
22      * 
23      *
24      */
25     
26       
27     static $cli_opts = array(
28         'pman-nodatabase' => array(
29             'desc' => 'Turn off database',
30             //'default' => 0,
31             //'short' => 'v',
32             'min' => 0,
33             'max' => 1,
34         ),
35         
36         'source' => array(
37             'desc' => 'Source directory for json files.',
38             'short' => 'f',
39             'default' => '',
40             'min' => 1,
41             'max' => 1,
42         ),
43          'no-cache' => array(
44             'desc' => 'Do not cache.',
45             'short' => 'n',
46             'default' => 0,
47             'min' => 1,
48             'max' => 1,
49         ),
50         'list-order' => array(
51             'desc' => 'List Order.',
52             'short' => 'l',
53             'default' => 0,
54             'min' => 1,
55             'max' => 1,
56         ),
57          'table' => array(
58             'desc' => 'Only a specific table, eg. -t Netsuite_SalesOrder',
59             'short' => 't',
60             
61             'max' => 1,
62         ),
63          'year' => array(
64             'desc' => 'Only a specific year, eg. -y 2009',
65             'short' => 'y',
66             'default' => 2009,
67             'min' => 1,
68             'max' => 1,
69         ),
70     );
71     
72     
73     
74     
75     
76     
77     
78     var $ff; // the Framework instance.
79     
80     
81     function HTML_FlexyFramework_Cli($ff)
82     {
83         $this->ff = $ff;
84     }
85     /**
86       * looks for Cli.php files and runs available() on them
87      * this should return a list of classes that can be used.
88      * - we should the load each one, and find the summary..
89      *
90      *
91      */
92     function cliHelp()
93     {
94      
95         $fn = basename($_SERVER["SCRIPT_FILENAME"]);
96       
97         echo "\n-------------------------------------------------
98 FlexyFramework Cli Application Usage:
99
100 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
101 or
102 #php  $fn [COMMAND] --help
103
104 -------------------------------------------------
105 Available commands:
106
107 ";
108         // add cli files..
109         //$this->cliShortHelp('Database');
110         
111         
112         $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"])); 
113         $pr = $this->ff->project;
114         
115         $this->cliHelpSearch($p,$pr);
116         echo "\n\n";
117         exit;
118     }
119     
120     
121     function cliHelpSearch($p,$pr, $path=false) {
122         
123         
124         
125         $full_path = array($p,$pr);
126         $class_path = array();
127         if ($path !== false)  {
128             $full_path= array_merge($full_path, $path);
129             $class_path = array_merge($class_path, $path);
130         }
131         //print_r("CHKDIR:    ". implode('/', $full_path)."\n");
132         
133         foreach(scandir(implode('/', $full_path)) as $d) {
134             
135             if (!strlen($d) || $d[0] == '.') {
136                 continue;
137             }
138             $chk = $full_path;
139             $chk[] = $d;
140             
141             $clp = $class_path;
142             
143             
144             
145             //print_r("CHK:          " . implode('/', $chk)."\n");
146             // is it a file.. and .PHP...
147             if (!is_dir(implode('/', $chk))) {
148                 if (!preg_match('/\.php$/',$d)) {
149                     continue;
150                 }
151                 $clp[] = preg_replace('/\.php$/','', $d);
152                 
153                 //print_r("CLP:          " . implode('/', $clp)."\n");
154                 $this->cliShortHelp(implode('/', $clp ));
155                 continue;
156             }
157             // skip special directories..
158             if ($d == 'templates') {
159                 continue;
160             }
161             if ($d == 'DataObjects') {
162                 continue;
163             }
164             
165             
166             $clp[] = $d;
167             // otherwise recurse...
168             //print_r("RECURSE:        " . implode('/', $clp)."\n");
169             
170             $this->cliHelpSearch($p,$pr, $clp);
171             
172             
173         }
174         
175         //print_r("COMPLETE:    ". implode('/', $full_path)."\n");
176         
177         
178         
179         
180     }
181     
182     
183     
184     
185     /**
186      * creates an instance of all the CLI classes and prints out class + title..
187      *
188      */
189     function cliShortHelp($p) { 
190         ////print_r("CHKFILE:         $p\n ");
191         list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
192         //var_dump($classname);
193         // does it have a description.
194         try { 
195             $cls = new ReflectionClass($classname);        
196             $val = $cls->getStaticPropertyValue('cli_desc');
197         } catch (Exception $e) {
198             return;
199         }
200         if (empty($val)) {
201             return;
202         }
203         echo str_pad($p,40," ") . $val ."\n";
204         
205         
206          
207     }
208      
209     /**
210     * cliParse - parse command line arguments, and return the values.
211     *  Will die with help message if --help or error is found..
212     * 
213     * @param {String} $classname name of class - should be loaded..
214     * @return {Array|false} array of key=>value arguments.. or false if not parsed
215     * 
216     */
217     function cliParse($classname)
218     {
219     
220     // cli static $classname::$cli_opts
221        
222         try {
223             $cls = new ReflectionClass($classname);        
224             $val = $cls->getStaticPropertyValue('cli_opts');
225         } catch (Exception $e) {
226             return;
227         }
228         if (empty($val)) {
229             return false;
230         }
231         
232         require_once 'Console/Getargs.php';
233         $ar = $_SERVER['argv'];
234         $call = array(array_shift($ar)); // remove index.php
235         $call[] = array_shift($ar); // remove our class...
236         //var_dump($ar);
237         
238         $newargs = Console_Getargs::factory($val, $ar);
239         
240         if (!is_a($newargs, 'PEAR_Error')) {
241             return $newargs->getValues();
242         }
243         
244         list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
245         
246         $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '. 
247               $optional . ' ' . $required . ' ' . $params . "\n\n";           
248         
249         
250         if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
251             // User put illegal values on the command line.
252             echo Console_Getargs::getHelp($val,
253                     $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
254             exit;
255         }
256         if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
257             // User needs help.
258             echo Console_Getargs::getHelp($val,
259                     $helpHeader, NULL, 78, 4)."\n\n";
260             exit;
261         }
262         
263         die($newargs->getMessage()); 
264         
265             
266     }
267     
268     
269     
270     /**
271      * the framework can be run without a database even if it's configured.
272      * to support this, we need to handle things like
273      *  --pman-nodatabase=1 on the command line.
274      *
275      *  
276      *
277      *
278      */
279     
280     function parseDefaultOpts()
281     {
282         require_once 'Console/Getargs.php';
283         $ar = $_SERVER['argv'];
284         //var_dump($ar);
285         $val = $this->cli_opts;
286         
287         $newargs = Console_Getargs::factory($val, $ar);
288         
289         if (!is_a($newargs, 'PEAR_Error')) {
290             return $newargs->getValues();
291         }
292         return false;
293         
294     }
295     
296     
297     
298 }