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);
81 var_dump($this->ff->projectExtends );
82 foreach($this->ff->projectExtends as $pr) {
83 $this->cliHelpSearch($p,$pr);
91 function cliHelpSearch($p,$pr, $path=false) {
95 $full_path = array($p,$pr);
96 $class_path = array();
97 if ($path !== false) {
98 $full_path= array_merge($full_path, $path);
99 $class_path = array_merge($class_path, $path);
101 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
103 foreach(scandir(implode('/', $full_path)) as $d) {
105 if (!strlen($d) || $d[0] == '.') {
115 //print_r("CHK: " . implode('/', $chk)."\n");
116 // is it a file.. and .PHP...
117 if (!is_dir(implode('/', $chk))) {
118 if (!preg_match('/\.php$/',$d)) {
121 $clp[] = preg_replace('/\.php$/','', $d);
123 //print_r("CLP: " . implode('/', $clp)."\n");
124 $this->cliShortHelp(implode('/', $clp ));
127 // skip special directories..
128 if ($d == 'templates') {
131 if ($d == 'DataObjects') {
137 // otherwise recurse...
138 //print_r("RECURSE: " . implode('/', $clp)."\n");
140 $this->cliHelpSearch($p,$pr, $clp);
145 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
156 * creates an instance of all the CLI classes and prints out class + title..
159 function cliShortHelp($p) {
160 ////print_r("CHKFILE: $p\n ");
161 list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
162 //var_dump($classname);
163 // does it have a description.
165 $cls = new ReflectionClass($classname);
166 $val = $cls->getStaticPropertyValue('cli_desc');
167 } catch (Exception $e) {
173 echo str_pad($p,40," ") . $val ."\n";
180 * cliParse - parse command line arguments, and return the values.
181 * Will die with help message if --help or error is found..
183 * @param {String} $classname name of class - should be loaded..
184 * @return {Array|false} array of key=>value arguments.. or false if not parsed
187 function cliParse($classname)
190 // cli static $classname::$cli_opts
193 // look up the parent tree for core opts.
194 $cls = new ReflectionClass($classname);
195 if (method_exists($classname, 'cli_opts')) {
196 $val = $classname::cli_opts();
198 $val = $cls->getStaticPropertyValue('cli_opts');
201 $val = is_array($val) ? $val : array();
202 while ($cls = $cls->getParentClass()) {
207 if (method_exists($cls->name, 'cli_opts')) {
209 $vadd = $cn::cli_opts();
211 $vadd = $cls->getStaticPropertyValue('cli_opts') ;
214 $val = array_merge($val, is_array($vadd) ? $vadd : array() );
215 } catch (Exception $e) {
222 } catch (Exception $e) {
223 echo "Warning: {$e->getMessage()}\n";
229 $val = array_merge(self::$cli_opts, $val);
232 require_once 'Console/Getargs.php';
233 $ar = $_SERVER['argv'];
234 $call = array(array_shift($ar)); // remove index.php
235 $call[] = array_shift($ar); // remove our class...
238 $newargs = Console_Getargs::factory($val, $ar);
240 if (!is_a($newargs, 'PEAR_Error')) {
241 return $newargs->getValues();
244 list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
246 $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '.
247 $optional . ' ' . $required . ' ' . $params . "\n\n";
250 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
251 // User put illegal values on the command line.
252 echo Console_Getargs::getHelp($val,
253 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
256 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
258 echo Console_Getargs::getHelp($val,
259 $helpHeader, NULL, 78, 4)."\n\n";
263 die($newargs->getMessage());
271 * the framework can be run without a database even if it's configured.
272 * to support this, we need to handle things like
273 * --pman-nodatabase=1 on the command line.
276 * @returns array() - args, false - nothing matched / invalid, true = help!
280 function parseDefaultOpts()
282 require_once 'Console/Getargs.php';
283 $ar = $_SERVER['argv'];
284 $call = array(array_shift($ar)); // remove index.php
285 $call[] = array_shift($ar);
287 $val = self::$cli_opts;
289 $newargs = Console_Getargs::factory($val, $ar);
291 if (is_a($newargs, 'PEAR_Error')) {
295 if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
296 // since we do not handle all the arguemnts here...
297 // skip errors if we find unknown arguments.
298 if (preg_match('/^Unknown argument/', $newargs->getMessage())) {
302 // User put illegal values on the command line.
303 echo Console_Getargs::getHelp($val,
304 $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
307 if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
316 // now handle real arguments..
319 $ret = $newargs->getValues();
320 foreach($ret as $k=>$v) {
322 case 'pman-nodatabase':
323 //echo "Turning off database";
324 $this->ff->nodatabase= true;
329 die("need to fix option $k");