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(
29 // this is a flag argument
30 'pman-nodatabase' => array(
31 'desc' => 'Turn off database',
43 var $ff; // the Framework instance.
46 function HTML_FlexyFramework_Cli($ff)
51 * looks for Cli.php files and runs available() on them
52 * this should return a list of classes that can be used.
53 * - we should the load each one, and find the summary..
60 $fn = basename($_SERVER["SCRIPT_FILENAME"]);
62 echo "\n-------------------------------------------------
63 FlexyFramework Cli Application Usage:
65 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
67 #php $fn [COMMAND] --help
69 -------------------------------------------------
74 //$this->cliShortHelp('Database');
77 $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
78 $pr = $this->ff->project;
80 $this->cliHelpSearch($p,$pr);
86 function cliHelpSearch($p,$pr, $path=false) {
90 $full_path = array($p,$pr);
91 $class_path = array();
92 if ($path !== false) {
93 $full_path= array_merge($full_path, $path);
94 $class_path = array_merge($class_path, $path);
96 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
98 foreach(scandir(implode('/', $full_path)) as $d) {
100 if (!strlen($d) || $d[0] == '.') {
110 //print_r("CHK: " . implode('/', $chk)."\n");
111 // is it a file.. and .PHP...
112 if (!is_dir(implode('/', $chk))) {
113 if (!preg_match('/\.php$/',$d)) {
116 $clp[] = preg_replace('/\.php$/','', $d);
118 //print_r("CLP: " . implode('/', $clp)."\n");
119 $this->cliShortHelp(implode('/', $clp ));
122 // skip special directories..
123 if ($d == 'templates') {
126 if ($d == 'DataObjects') {
132 // otherwise recurse...
133 //print_r("RECURSE: " . implode('/', $clp)."\n");
135 $this->cliHelpSearch($p,$pr, $clp);
140 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
151 * creates an instance of all the CLI classes and prints out class + title..
154 function cliShortHelp($p) {
155 ////print_r("CHKFILE: $p\n ");
156 list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
157 //var_dump($classname);
158 // does it have a description.
160 $cls = new ReflectionClass($classname);
161 $val = $cls->getStaticPropertyValue('cli_desc');
162 } catch (Exception $e) {
168 echo str_pad($p,40," ") . $val ."\n";
175 * cliParse - parse command line arguments, and return the values.
176 * Will die with help message if --help or error is found..
178 * @param {String} $classname name of class - should be loaded..
179 * @return {Array|false} array of key=>value arguments.. or false if not parsed
182 function cliParse($classname)
185 // cli static $classname::$cli_opts
188 // look up the parent tree for core opts.
189 $cls = new ReflectionClass($classname);
190 if (method_exists($classname, 'cli_opts')) {
191 $val = $classname::cli_opts();
193 $val = $cls->getStaticPropertyValue('cli_opts');
196 $val = is_array($val) ? $val : array();
197 while ($cls = $cls->getParentClass()) {
202 if (method_exists($cls->name, 'cli_opts')) {
204 $vadd = $cn::cli_opts();
206 $vadd = $cls->getStaticPropertyValue('cli_opts') ;
209 $val = array_merge($val, is_array($vadd) ? $vadd : array() )
210 } catch (Exception $e) {
217 } catch (Exception $e) {
218 echo "Warning: {$e->getMessage()}\n";
224 $val = array_merge(self::$cli_opts, $val);
227 require_once 'Console/Getargs.php';
228 $ar = $_SERVER['argv'];
229 $call = array(array_shift($ar)); // remove index.php
230 $call[] = array_shift($ar); // remove our class...
233 $newargs = Console_Getargs::factory($val, $ar);
235 if (!is_a($newargs, 'PEAR_Error')) {
236 return $newargs->getValues();
239 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
241 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
242 $optional . ' ' . $required . ' ' . $params . "\n\n";
245 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
246 // User put illegal values on the command line.
247 echo Console_Getargs::getHelp($val,
248 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
251 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
253 echo Console_Getargs::getHelp($val,
254 $helpHeader, NULL, 78, 4)."\n\n";
258 die($newargs->getMessage());
266 * the framework can be run without a database even if it's configured.
267 * to support this, we need to handle things like
268 * --pman-nodatabase=1 on the command line.
271 * @returns array() - args, false - nothing matched / invalid, true = help!
275 function parseDefaultOpts()
277 require_once 'Console/Getargs.php';
278 $ar = $_SERVER['argv'];
279 $call = array(array_shift($ar)); // remove index.php
280 $call[] = array_shift($ar);
282 $val = self::$cli_opts;
284 $newargs = Console_Getargs::factory($val, $ar);
286 if (is_a($newargs, 'PEAR_Error')) {
290 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
291 // since we do not handle all the arguemnts here...
292 // skip errors if we find unknown arguments.
293 if (preg_match('/^Unknown argument/', $newargs->getMessage())) {
297 // User put illegal values on the command line.
298 echo Console_Getargs::getHelp($val,
299 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
302 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
311 // now handle real arguments..
314 $ret = $newargs->getValues();
315 foreach($ret as $k=>$v) {
317 case 'pman-nodatabase':
318 //echo "Turning off database";
319 $this->ff->nodatabase= true;
324 die("need to fix option $k");