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 __construct($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);
81 if (!empty($this->ff->projectExtends)) {
82 foreach($this->ff->projectExtends as $pr) {
83 $this->cliHelpSearch($p,$pr);
92 function cliHelpSearch($p,$pr, $path=false) {
96 $full_path = array($p,$pr);
97 $class_path = array();
98 if ($path !== false) {
99 $full_path= array_merge($full_path, $path);
100 $class_path = array_merge($class_path, $path);
102 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
104 foreach(scandir(implode('/', $full_path)) as $d) {
106 if (!strlen($d) || $d[0] == '.') {
116 //print_r("CHK: " . implode('/', $chk)."\n");
117 // is it a file.. and .PHP...
118 if (!is_dir(implode('/', $chk))) {
119 if (!preg_match('/\.php$/',$d)) {
122 $clp[] = preg_replace('/\.php$/','', $d);
124 //print_r("CLP: " . implode('/', $clp)."\n");
125 $this->cliShortHelp(implode('/', $clp ));
128 // skip special directories..
129 if ($d == 'templates') {
132 if ($d == 'DataObjects') {
138 // otherwise recurse...
139 //print_r("RECURSE: " . implode('/', $clp)."\n");
141 $this->cliHelpSearch($p,$pr, $clp);
146 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
157 * creates an instance of all the CLI classes and prints out class + title..
160 function cliShortHelp($p) {
161 ////print_r("CHKFILE: $p\n ");
162 list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
163 //var_dump($classname);
164 // does it have a description.
166 $cls = new ReflectionClass($classname);
167 $val = $cls->getStaticPropertyValue('cli_desc');
168 } catch (Exception $e) {
174 echo str_pad($p,40," ") . $val ."\n";
181 * cliParse - parse command line arguments, and return the values.
182 * Will die with help message if --help or error is found..
184 * @param {String} $classname name of class - should be loaded..
185 * @return {Array|false} array of key=>value arguments.. or false if not parsed
188 function cliParse($classname)
191 // cli static $classname::$cli_opts
194 // look up the parent tree for core opts.
196 $cls = new ReflectionClass($classname);
197 if (method_exists($classname, 'cli_opts')) {
198 $val = $classname::cli_opts();
200 $ar = $cls->getStaticProperties();
201 if (isset($ar['cli_opts'])) {
202 $val = $cls->getStaticPropertyValue('cli_opts');
206 $val = is_array($val) ? $val : array();
207 while ($cls = $cls->getParentClass()) {
212 if (method_exists($cls->name, 'cli_opts')) {
214 $vadd = $cn::cli_opts();
216 $vadd = $cls->getStaticPropertyValue('cli_opts') ;
219 $val = array_merge($val, is_array($vadd) ? $vadd : array() );
220 } catch (ReflectionException $e) {
227 } catch (ReflectionException $e) {
229 echo "cliParse:Warning: {$e->getMessage()}\n";
235 $val = array_merge(self::$cli_opts, $val);
238 require_once 'Console/Getargs.php';
239 $ar = $_SERVER['argv'];
240 $call = array(array_shift($ar)); // remove index.php
241 $call[] = array_shift($ar); // remove our class...
244 $newargs = Console_Getargs::factory($val, $ar);
246 if (!is_a($newargs, 'PEAR_Error')) {
247 return $newargs->getValues();
250 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
252 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
253 $optional . ' ' . $required . ' ' . $params . "\n\n";
256 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
257 // User put illegal values on the command line.
258 echo Console_Getargs::getHelp($val,
259 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
262 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
264 echo Console_Getargs::getHelp($val,
265 $helpHeader, NULL, 78, 4)."\n\n";
269 die($newargs->getMessage());
277 * the framework can be run without a database even if it's configured.
278 * to support this, we need to handle things like
279 * --pman-nodatabase=1 on the command line.
282 * @returns array() - args, false - nothing matched / invalid, true = help!
286 function parseDefaultOpts()
288 require_once 'Console/Getargs.php';
289 $ar = $_SERVER['argv'];
290 $call = array(array_shift($ar)); // remove index.php
291 $call[] = array_shift($ar);
293 $val = self::$cli_opts;
295 $newargs = Console_Getargs::factory($val, $ar);
297 if (is_a($newargs, 'PEAR_Error')) {
301 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
302 // since we do not handle all the arguemnts here...
303 // skip errors if we find unknown arguments.
304 if (preg_match('/^Unknown argument/', $newargs->getMessage())) {
308 // User put illegal values on the command line.
309 echo Console_Getargs::getHelp($val,
310 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
313 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
322 // now handle real arguments..
325 $ret = $newargs->getValues();
326 foreach($ret as $k=>$v) {
328 case 'pman-nodatabase':
329 //echo "Turning off database";
330 $this->ff->nodatabase= true;
335 die("need to fix option $k");