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     
22     
23     
24     
25     
26     
27     
28     
29     var $ff; // the Framework instance.
30     
31     
32     function HTML_FlexyFramework_Cli($ff)
33     {
34         $this->ff = $ff;
35     }
36     /**
37       * looks for Cli.php files and runs available() on them
38      * this should return a list of classes that can be used.
39      * - we should the load each one, and find the summary..
40      *
41      *
42      */
43     function cliHelp()
44     {
45      
46         $fn = basename($_SERVER["SCRIPT_FILENAME"]);
47       
48         echo "\n-------------------------------------------------
49 FlexyFramework Cli Application Usage:
50
51 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
52 or
53 #php  $fn [COMMAND] --help
54
55 -------------------------------------------------
56 Available commands:
57
58 ";
59         // add cli files..
60         //$this->cliShortHelp('Database');
61         
62         
63         $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"])); 
64         $pr = $this->ff->project;
65         
66         $this->cliHelpSearch($p,$pr);
67         echo "\n\n";
68         exit;
69     }
70     
71     
72     function cliHelpSearch($p,$pr, $path=false) {
73         
74         
75         
76         $full_path = array($p,$pr);
77         $class_path = array();
78         if ($path !== false)  {
79             $full_path= array_merge($full_path, $path);
80             $class_path = array_merge($class_path, $path);
81         }
82         //print_r("CHKDIR:    ". implode('/', $full_path)."\n");
83         
84         foreach(scandir(implode('/', $full_path)) as $d) {
85             
86             if (!strlen($d) || $d[0] == '.') {
87                 continue;
88             }
89             $chk = $full_path;
90             $chk[] = $d;
91             
92             $clp = $class_path;
93             
94             
95             
96             //print_r("CHK:          " . implode('/', $chk)."\n");
97             // is it a file.. and .PHP...
98             if (!is_dir(implode('/', $chk))) {
99                 if (!preg_match('/\.php$/',$d)) {
100                     continue;
101                 }
102                 $clp[] = preg_replace('/\.php$/','', $d);
103                 
104                 //print_r("CLP:          " . implode('/', $clp)."\n");
105                 $this->cliShortHelp(implode('/', $clp ));
106                 continue;
107             }
108             // skip special directories..
109             if ($d == 'templates') {
110                 continue;
111             }
112             if ($d == 'DataObjects') {
113                 continue;
114             }
115             
116             
117             $clp[] = $d;
118             // otherwise recurse...
119             //print_r("RECURSE:        " . implode('/', $clp)."\n");
120             
121             $this->cliHelpSearch($p,$pr, $clp);
122             
123             
124         }
125         
126         //print_r("COMPLETE:    ". implode('/', $full_path)."\n");
127         
128         
129         
130         
131     }
132     
133     
134     
135     
136     /**
137      * creates an instance of all the CLI classes and prints out class + title..
138      *
139      */
140     function cliShortHelp($p) { 
141         ////print_r("CHKFILE:         $p\n ");
142         list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
143         //var_dump($classname);
144         // does it have a description.
145         try { 
146             $cls = new ReflectionClass($classname);        
147             $val = $cls->getStaticPropertyValue('cli_desc');
148         } catch (Exception $e) {
149             return;
150         }
151         if (empty($val)) {
152             return;
153         }
154         echo str_pad($p,40," ") . $val ."\n";
155         
156         
157          
158     }
159      
160     /**
161     * cliParse - parse command line arguments, and return the values.
162     *  Will die with help message if --help or error is found..
163     * 
164     * @param {String} $classname name of class - should be loaded..
165     * @return {Array|false} array of key=>value arguments.. or false if not parsed
166     * 
167     */
168     function cliParse($classname)
169     {
170     
171     // cli static $classname::$cli_opts
172        
173         try {
174             $cls = new ReflectionClass($classname);        
175             $val = $cls->getStaticPropertyValue('cli_opts');
176         } catch (Exception $e) {
177             return;
178         }
179         if (empty($val)) {
180             return false;
181         }
182         
183         require_once 'Console/Getargs.php';
184         $ar = $_SERVER['argv'];
185         $call = array(array_shift($ar)); // remove index.php
186         $call[] = array_shift($ar); // remove our class...
187         //var_dump($ar);
188         
189         $newargs = Console_Getargs::factory($val, $ar);
190         
191         if (!is_a($newargs, 'PEAR_Error')) {
192             return $newargs->getValues();
193         }
194         
195         list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
196         
197         $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '. 
198               $optional . ' ' . $required . ' ' . $params . "\n\n";           
199         
200         
201         if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
202             // User put illegal values on the command line.
203             echo Console_Getargs::getHelp($val,
204                     $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
205             exit;
206         }
207         if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
208             // User needs help.
209             echo Console_Getargs::getHelp($val,
210                     $helpHeader, NULL, 78, 4)."\n\n";
211             exit;
212         }
213         
214         die($newargs->getMessage()); 
215         
216             
217     }
218     
219     
220     
221     /**
222      * the framework can be run without a database even if it's configured.
223      * to support this, we need to handle things like
224      *  --pman-nodatabase=1 on the command line.
225      *
226      *  
227      *
228      *
229      */
230     
231     function parseDefaultOpts()
232     {
233         require_once 'Console/Getargs.php';
234         $ar = $_SERVER['argv'];
235         //var_dump($ar);
236         $val = $this->cli_opts;
237         
238         $newargs = Console_Getargs::factory($val, $ar);
239         
240         if (!is_a($newargs, 'PEAR_Error')) {
241             return $newargs->getValues();
242         }
243         return false;
244         
245     }
246     
247     
248     
249 }