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