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
21 * Default options that allow modification of the framewoek behaviour
27 static $cli_opts = array(
28 'pman-nodatabase' => array(
29 'desc' => 'Turn off database',
44 var $ff; // the Framework instance.
47 function HTML_FlexyFramework_Cli($ff)
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..
61 $fn = basename($_SERVER["SCRIPT_FILENAME"]);
63 echo "\n-------------------------------------------------
64 FlexyFramework Cli Application Usage:
66 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
68 #php $fn [COMMAND] --help
70 -------------------------------------------------
75 //$this->cliShortHelp('Database');
78 $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
79 $pr = $this->ff->project;
81 $this->cliHelpSearch($p,$pr);
87 function cliHelpSearch($p,$pr, $path=false) {
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);
97 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
99 foreach(scandir(implode('/', $full_path)) as $d) {
101 if (!strlen($d) || $d[0] == '.') {
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)) {
117 $clp[] = preg_replace('/\.php$/','', $d);
119 //print_r("CLP: " . implode('/', $clp)."\n");
120 $this->cliShortHelp(implode('/', $clp ));
123 // skip special directories..
124 if ($d == 'templates') {
127 if ($d == 'DataObjects') {
133 // otherwise recurse...
134 //print_r("RECURSE: " . implode('/', $clp)."\n");
136 $this->cliHelpSearch($p,$pr, $clp);
141 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
152 * creates an instance of all the CLI classes and prints out class + title..
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.
161 $cls = new ReflectionClass($classname);
162 $val = $cls->getStaticPropertyValue('cli_desc');
163 } catch (Exception $e) {
169 echo str_pad($p,40," ") . $val ."\n";
176 * cliParse - parse command line arguments, and return the values.
177 * Will die with help message if --help or error is found..
179 * @param {String} $classname name of class - should be loaded..
180 * @return {Array|false} array of key=>value arguments.. or false if not parsed
183 function cliParse($classname)
186 // cli static $classname::$cli_opts
189 $cls = new ReflectionClass($classname);
190 $val = $cls->getStaticPropertyValue('cli_opts');
191 } catch (Exception $e) {
197 $val = array_merge(self::$cli_opts, $val);
200 require_once 'Console/Getargs.php';
201 $ar = $_SERVER['argv'];
202 $call = array(array_shift($ar)); // remove index.php
203 $call[] = array_shift($ar); // remove our class...
206 $newargs = Console_Getargs::factory($val, $ar);
208 if (!is_a($newargs, 'PEAR_Error')) {
209 return $newargs->getValues();
212 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
214 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
215 $optional . ' ' . $required . ' ' . $params . "\n\n";
218 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
219 // User put illegal values on the command line.
220 echo Console_Getargs::getHelp($val,
221 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
224 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
226 echo Console_Getargs::getHelp($val,
227 $helpHeader, NULL, 78, 4)."\n\n";
231 die($newargs->getMessage());
239 * the framework can be run without a database even if it's configured.
240 * to support this, we need to handle things like
241 * --pman-nodatabase=1 on the command line.
244 * @returns array() - args, false - nothing matched / invalid, true = help!
248 function parseDefaultOpts()
250 require_once 'Console/Getargs.php';
251 $ar = $_SERVER['argv'];
253 $val = self::$cli_opts;
255 $newargs = Console_Getargs::factory($val, $ar);
257 if (!is_a($newargs, 'PEAR_Error')) {
258 return $newargs->getValues();
261 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {