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
29 var $ff; // the Framework instance.
32 function HTML_FlexyFramework_Cli($ff)
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..
46 $fn = basename($_SERVER["SCRIPT_FILENAME"]);
48 echo "\n-------------------------------------------------
49 FlexyFramework Cli Application Usage:
51 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
53 #php $fn [COMMAND] --help
55 -------------------------------------------------
60 //$this->cliShortHelp('Database');
63 $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
64 $pr = $this->ff->project;
66 $this->cliHelpSearch($p,$pr);
72 function cliHelpSearch($p,$pr, $path=false) {
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);
82 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
84 foreach(scandir(implode('/', $full_path)) as $d) {
86 if (!strlen($d) || $d[0] == '.') {
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)) {
102 $clp[] = preg_replace('/\.php$/','', $d);
104 //print_r("CLP: " . implode('/', $clp)."\n");
105 $this->cliShortHelp(implode('/', $clp ));
108 // skip special directories..
109 if ($d == 'templates') {
112 if ($d == 'DataObjects') {
118 // otherwise recurse...
119 //print_r("RECURSE: " . implode('/', $clp)."\n");
121 $this->cliHelpSearch($p,$pr, $clp);
126 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
137 * creates an instance of all the CLI classes and prints out class + title..
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.
146 $cls = new ReflectionClass($classname);
147 $val = $cls->getStaticPropertyValue('cli_desc');
148 } catch (Exception $e) {
154 echo str_pad($p,40," ") . $val ."\n";
161 * cliParse - parse command line arguments, and return the values.
162 * Will die with help message if --help or error is found..
164 * @param {String} $classname name of class - should be loaded..
165 * @return {Array|false} array of key=>value arguments.. or false if not parsed
168 function cliParse($classname)
171 // cli static $classname::$cli_opts
174 $cls = new ReflectionClass($classname);
175 $val = $cls->getStaticPropertyValue('cli_opts');
176 } catch (Exception $e) {
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...
189 $newargs = Console_Getargs::factory($val, $ar);
191 if (!is_a($newargs, 'PEAR_Error')) {
192 return $newargs->getValues();
195 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
197 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
198 $optional . ' ' . $required . ' ' . $params . "\n\n";
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";
207 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
209 echo Console_Getargs::getHelp($val,
210 $helpHeader, NULL, 78, 4)."\n\n";
214 die($newargs->getMessage());
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.
231 function parseDefaultOpts()
233 require_once 'Console/Getargs.php';
234 $ar = $_SERVER['argv'];
236 $val = $this->cli_opts;
238 $newargs = Console_Getargs::factory($val, $ar);
240 if (!is_a($newargs, 'PEAR_Error')) {
241 return $newargs->getValues();