5 * handles the cli features of Flexy Framework.
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);
17 class HTML_FlexyFramework_Cli
19 var $ff; // the Framework instance.
22 function HTML_FlexyFramework_Cli($ff)
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..
36 $fn = basename($_SERVER["SCRIPT_FILENAME"]);
38 echo "\n-------------------------------------------------
39 FlexyFramework Cli Application Usage:
41 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
43 #php $fn [COMMAND] --help
45 -------------------------------------------------
50 //$this->cliShortHelp('Database');
53 $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
54 $pr = $this->ff->project;
56 $this->cliHelpSearch($p,$pr);
62 function cliHelpSearch($p,$pr, $path=false) {
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);
72 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
74 foreach(scandir(implode('/', $full_path)) as $d) {
76 if (!strlen($d) || $d[0] == '.') {
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)) {
92 $clp[] = preg_replace('/\.php$/','', $d);
94 //print_r("CLP: " . implode('/', $clp)."\n");
95 $this->cliShortHelp(implode('/', $clp ));
98 // skip special directories..
99 if ($d == 'templates') {
102 if ($d == 'DataObjects') {
108 // otherwise recurse...
109 //print_r("RECURSE: " . implode('/', $clp)."\n");
111 $this->cliHelpSearch($p,$pr, $clp);
116 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
127 * creates an instance of all the CLI classes and prints out class + title..
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.
136 $cls = new ReflectionClass($classname);
137 $val = $cls->getStaticPropertyValue('cli_desc');
138 } catch (Exception $e) {
144 echo str_pad($p,40," ") . $val ."\n";
151 * cliParse - parse command line arguments, and return the values.
152 * Will die with help message if --help or error is found..
154 * @param {String} $classname name of class - should be loaded..
155 * @return {Array|false} array of key=>value arguments.. or false if not parsed
158 function cliParse($classname)
161 // cli static $classname::$cli_opts
164 $cls = new ReflectionClass($classname);
165 $val = $cls->getStaticPropertyValue('cli_opts');
166 } catch (Exception $e) {
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...
179 $newargs = Console_Getargs::factory($val, $ar);
181 if (!is_a($newargs, 'PEAR_Error')) {
182 return $newargs->getValues();
185 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
187 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
188 $optional . ' ' . $required . ' ' . $params . "\n\n";
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";
197 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
199 echo Console_Getargs::getHelp($val,
200 $helpHeader, NULL, 78, 4)."\n\n";
204 die($newargs->getMessage());
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.
218 function parseDefaultOpts()
220 require_once 'Console/Getargs.php';
221 $ar = $_SERVER['argv'];
223 $val = $this->cli_opts;
225 $newargs = Console_Getargs::factory($val, $ar);
227 if (!is_a($newargs, 'PEAR_Error')) {
228 return $newargs->getValues();