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