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',
37 'desc' => 'Source directory for json files.',
44 'desc' => 'Do not cache.',
50 'list-order' => array(
51 'desc' => 'List Order.',
58 'desc' => 'Only a specific table, eg. -t Netsuite_SalesOrder',
64 'desc' => 'Only a specific year, eg. -y 2009',
78 var $ff; // the Framework instance.
81 function HTML_FlexyFramework_Cli($ff)
86 * looks for Cli.php files and runs available() on them
87 * this should return a list of classes that can be used.
88 * - we should the load each one, and find the summary..
95 $fn = basename($_SERVER["SCRIPT_FILENAME"]);
97 echo "\n-------------------------------------------------
98 FlexyFramework Cli Application Usage:
100 #php -d include_path=.:/var/www/pear $fn [COMMAND] --help
102 #php $fn [COMMAND] --help
104 -------------------------------------------------
109 //$this->cliShortHelp('Database');
112 $p = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
113 $pr = $this->ff->project;
115 $this->cliHelpSearch($p,$pr);
121 function cliHelpSearch($p,$pr, $path=false) {
125 $full_path = array($p,$pr);
126 $class_path = array();
127 if ($path !== false) {
128 $full_path= array_merge($full_path, $path);
129 $class_path = array_merge($class_path, $path);
131 //print_r("CHKDIR: ". implode('/', $full_path)."\n");
133 foreach(scandir(implode('/', $full_path)) as $d) {
135 if (!strlen($d) || $d[0] == '.') {
145 //print_r("CHK: " . implode('/', $chk)."\n");
146 // is it a file.. and .PHP...
147 if (!is_dir(implode('/', $chk))) {
148 if (!preg_match('/\.php$/',$d)) {
151 $clp[] = preg_replace('/\.php$/','', $d);
153 //print_r("CLP: " . implode('/', $clp)."\n");
154 $this->cliShortHelp(implode('/', $clp ));
157 // skip special directories..
158 if ($d == 'templates') {
161 if ($d == 'DataObjects') {
167 // otherwise recurse...
168 //print_r("RECURSE: " . implode('/', $clp)."\n");
170 $this->cliHelpSearch($p,$pr, $clp);
175 //print_r("COMPLETE: ". implode('/', $full_path)."\n");
186 * creates an instance of all the CLI classes and prints out class + title..
189 function cliShortHelp($p) {
190 ////print_r("CHKFILE: $p\n ");
191 list($classname,$subRequest) = $this->ff->requestToClassName($p,FALSE);
192 //var_dump($classname);
193 // does it have a description.
195 $cls = new ReflectionClass($classname);
196 $val = $cls->getStaticPropertyValue('cli_desc');
197 } catch (Exception $e) {
203 echo str_pad($p,40," ") . $val ."\n";
210 * cliParse - parse command line arguments, and return the values.
211 * Will die with help message if --help or error is found..
213 * @param {String} $classname name of class - should be loaded..
214 * @return {Array|false} array of key=>value arguments.. or false if not parsed
217 function cliParse($classname)
220 // cli static $classname::$cli_opts
223 $cls = new ReflectionClass($classname);
224 $val = $cls->getStaticPropertyValue('cli_opts');
225 } catch (Exception $e) {
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.
280 function parseDefaultOpts()
282 require_once 'Console/Getargs.php';
283 $ar = $_SERVER['argv'];
285 $val = $this->cli_opts;
287 $newargs = Console_Getargs::factory($val, $ar);
289 if (!is_a($newargs, 'PEAR_Error')) {
290 return $newargs->getValues();