summaryrefslogtreecommitdiff
path: root/framework/prado-cli.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/prado-cli.php')
-rwxr-xr-xframework/prado-cli.php612
1 files changed, 225 insertions, 387 deletions
diff --git a/framework/prado-cli.php b/framework/prado-cli.php
index 8921d25b..58e274fe 100755
--- a/framework/prado-cli.php
+++ b/framework/prado-cli.php
@@ -8,144 +8,183 @@
if(!isset($_SERVER['argv']))
die('Must be run from the command line');
-//command line options.
-$options['create'] = array('c:', 'create=', '<directory>', 'create a new project <directory>');
-$options['tests'] = array('t', 'create-tests', '', 'create unit and function test fixtures');
-
-$console = new ConsoleOptions($options, $_SERVER['argv']);
-
-if($console->hasOptions())
+require_once(dirname(__FILE__).'/prado.php');
+class PradoShellApplication extends TApplication
{
- if($dir = $console->getOption('create'))
- create_new_prado_project($dir);
- if($console->getOption('tests'))
- create_test_fixtures($dir);
+ public function run()
+ {
+ $this->initApplication();
+ }
}
-else
+class DummyHttpRequest extends THttpRequest
{
- $details = $console->renderOptions();
-echo <<<EOD
-Usage: php prado-cli.php [-c] <directory>
-Options:
-$details
-Example: php prado-cli.php -c ./example1
-
-EOD;
+ protected function resolveRequest()
+ {
+ }
}
-/**
- * Functions to create new prado project.
- */
-
-function create_new_prado_project($dir)
+function __shell_print_var($shell,$var)
{
- if(strlen(trim($dir)) == 0)
- return;
-
- $rootPath = realpath(dirname(trim($dir)));
-
- $basePath = $rootPath.'/'.basename($dir);
- $assetPath = $basePath.'/assets';
- $protectedPath = $basePath.'/protected';
- $runtimePath = $basePath.'/protected/runtime';
- $pagesPath = $protectedPath.'/pages';
-
- $indexFile = $basePath.'/index.php';
- $htaccessFile = $protectedPath.'/.htaccess';
- $defaultPageFile = $pagesPath.'/Home.page';
-
- $tests = $basePath.'/tests';
- $unit_tests = $tests.'/unit';
- $functional_tests = $tests.'/functional';
-
- create_directory($basePath, 0755);
- create_directory($assetPath,0777);
- create_directory($protectedPath,0755);
- create_directory($runtimePath,0777);
- create_directory($pagesPath,0755);
-
- create_file($indexFile, render_index_file());
- create_file($htaccessFile, render_htaccess_file());
- create_file($defaultPageFile, render_default_page());
+ if(!$shell->has_semicolon)
+ echo Prado::varDump($var);
}
-function create_test_fixtures($dir)
-{
- if(strlen(trim($dir)) == 0)
- return;
-
- $rootPath = realpath(dirname(trim($dir)));
- $basePath = $rootPath.'/'.basename($dir);
-
- $tests = $basePath.'/tests';
- $unit_tests = $tests.'/unit';
- $functional_tests = $tests.'/functional';
-
- create_directory($tests,0755);
- create_directory($unit_tests,0755);
- create_directory($functional_tests,0755);
+restore_exception_handler();
- $unit_test_index = $tests.'/unit.php';
- $functional_test_index = $tests.'/functional.php';
+//run it;
+PradoCommandLineInterpreter::run($_SERVER['argv']);
+if(strtolower($_SERVER['argv'][1])==='shell')
+ include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
- create_file($unit_test_index, render_unit_test_fixture());
- create_file($functional_test_index, render_functional_test_fixture());
-}
-
-function render_unit_test_fixture()
+class PradoCommandLineInterpreter
{
- $tester = realpath(dirname(__FILE__).'/../tests/test_tools/unit_tests.php');
-return '<?php
+ protected $_actions=array(
+ 'PradoCommandLineCreateProject' => null,
+ 'PradoCommandLineCreateTests' => null,
+ 'PradoCommandLinePhpShell' => null
+ );
-include_once \''.$tester.'\';
+ public function __construct()
+ {
+ foreach(array_keys($this->_actions) as $class)
+ $this->_actions[$class] = new $class;
+ }
-$app_directory = "../protected";
-$test_cases = dirname(__FILE__)."/unit";
+ public static function run($args)
+ {
+ echo "Command line tools for Prado ".Prado::getVersion().".\n";
-$tester = new PradoUnitTester($test_cases, $app_directory);
-$tester->run(new HtmlReporter());
+ if(count($args) > 1)
+ array_shift($args);
+ static $int;
+ if(is_null($int))
+ $int = new self;
+ $valid = false;
+ foreach($int->_actions as $class => $action)
+ {
+ if($action->isValidAction($args))
+ {
+ $valid |= $action->performAction($args);
+ break;
+ }
+ else
+ {
+ $valid = false;
+ }
+ }
+ if(!$valid)
+ $int->printHelp();
+ }
-?>';
+ public function printHelp()
+ {
+ echo "usage: php prado-cli.php action <parameter> [optional]\n";
+ echo "example: php prado-cli.php -c mysite\n\n";
+ echo "actions:\n";
+ foreach($this->_actions as $action)
+ echo $action->renderHelp();
+ }
}
-function render_functional_test_fixture()
+abstract class PradoCommandLineAction
{
- $tester = realpath(dirname(__FILE__).'/../tests/test_tools/functional_tests.php');
-return '<?php
+ abstract public function performAction($args);
-include_once \''.$tester.'\';
+ protected function createDirectory($dir, $mask)
+ {
+ if(!is_dir($dir))
+ {
+ mkdir($dir);
+ echo "creating $dir\n";
+ }
+ if(is_dir($dir))
+ chmod($dir, $mask);
+ }
-$test_cases = dirname(__FILE__)."/functional";
+ protected function createFile($filename, $content)
+ {
+ if(!is_file($filename))
+ {
+ file_put_contents($filename, $content);
+ echo "creating $filename\n";
+ }
+ }
-$tester=new PradoFunctionalTester($test_cases);
-$tester->run(new SimpleReporter());
+ public function isValidAction($args)
+ {
+ return strtolower($args[0]) === $this->action &&
+ count($args)-1 >= count($this->parameters);
+ }
-?>';
+ public function renderHelp()
+ {
+ $params = array();
+ foreach($this->parameters as $v)
+ $params[] = '<'.$v.'>';
+ $parameters = join($params, ' ');
+ $options = array();
+ foreach($this->optional as $v)
+ $options[] = '['.$v.']';
+ $optional = (strlen($parameters) ? ' ' : ''). join($options, ' ');
+ $description='';
+ foreach(explode("\n", wordwrap($this->description,65)) as $line)
+ $description .= ' '.$line."\n";
+ return <<<EOD
+ {$this->action} {$parameters}{$optional}
+{$description}
+
+EOD;
+ }
}
-function create_directory($dir, $mask)
+
+class PradoCommandLineCreateProject extends PradoCommandLineAction
{
- if(!is_dir($dir))
+ protected $action = '-c';
+ protected $parameters = array('directory');
+ protected $optional = array();
+ protected $description = 'Creates a Prado project skeleton for the given <directory>.';
+
+ public function performAction($args)
{
- mkdir($dir);
- echo "creating $dir\n";
+ $this->createNewPradoProject($args[1]);
+ return true;
}
- if(is_dir($dir))
- chmod($dir, $mask);
-}
-function create_file($filename, $content)
-{
- if(!is_file($filename))
+ /**
+ * Functions to create new prado project.
+ */
+ protected function createNewPradoProject($dir)
{
- file_put_contents($filename, $content);
- echo "creating $filename\n";
+ if(strlen(trim($dir)) == 0)
+ return;
+
+ $rootPath = realpath(dirname(trim($dir)));
+
+ $basePath = $rootPath.'/'.basename($dir);
+ $assetPath = $basePath.'/assets';
+ $protectedPath = $basePath.'/protected';
+ $runtimePath = $basePath.'/protected/runtime';
+ $pagesPath = $protectedPath.'/pages';
+
+ $indexFile = $basePath.'/index.php';
+ $htaccessFile = $protectedPath.'/.htaccess';
+ $defaultPageFile = $pagesPath.'/Home.page';
+
+ $this->createDirectory($basePath, 0755);
+ $this->createDirectory($assetPath,0777);
+ $this->createDirectory($protectedPath,0755);
+ $this->createDirectory($runtimePath,0777);
+ $this->createDirectory($pagesPath,0755);
+
+ $this->createFile($indexFile, $this->renderIndexFile());
+ $this->createFile($htaccessFile, $this->renderHtaccessFile());
+ $this->createFile($defaultPageFile, $this->renderDefaultPage());
}
-}
-function render_index_file()
-{
- $framework = realpath(dirname(__FILE__)).'/prado.php';
+ protected function renderIndexFile()
+ {
+ $framework = realpath(dirname(__FILE__)).'/prado.php';
return '<?php
$frameworkPath=\''.$framework.'\';
@@ -168,323 +207,122 @@ $application=new TApplication;
$application->run();
?>';
-}
+ }
-function render_htaccess_file()
-{
- return 'deny from all';
-}
+ protected function renderHtaccessFile()
+ {
+ return 'deny from all';
+ }
-function render_default_page()
-{
+ protected function renderDefaultPage()
+ {
return <<<EOD
<h1>Welcome to Prado!</h1>
EOD;
+ }
}
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-// +----------------------------------------------------------------------+
-// | PHP Version 4 |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 1997-2003 The PHP Group |
-// +----------------------------------------------------------------------+
-// | This source file is subject to version 3.0 of the PHP license, |
-// | that is bundled with this package in the file LICENSE, and is |
-// | available through the world-wide-web at the following url: |
-// | http://www.php.net/license/3_0.txt. |
-// | If you did not receive a copy of the PHP license and are unable to |
-// | obtain it through the world-wide-web, please send a note to |
-// | license@php.net so we can mail you a copy immediately. |
-// +----------------------------------------------------------------------+
-// | Author: Andrei Zmievski <andrei@php.net> |
-// +----------------------------------------------------------------------+
-//
-// $Id$
-
-/**
- * Command-line options parsing class.
- *
- * @author Andrei Zmievski <andrei@php.net>
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- */
-class ConsoleOptions
+class PradoCommandLineCreateTests extends PradoCommandLineAction
{
- private $_short_options;
- private $_long_options;
- private $_options;
- private $_args;
- private $_values;
+ protected $action = '-t';
+ protected $parameters = array('directory');
+ protected $optional = array();
+ protected $description = 'Create test fixtures in the given <directory>.';
- /**
- * @param array list of options with the following format
- * <tt>option['name'] = array('short', 'long', 'parameter', 'description')</tt>
- * see @link setOptions} for details on 'short' and 'long' string details.
- */
- public function __construct($options, $args)
+ public function performAction($args)
{
- $short = '';
- $long = array();
- foreach($options as $option)
- {
- $short .= $option[0];
- $long[] = $option[1];
- }
- $this->setOptions($short,$long);
- $this->_options = $options;
- $this->_args = $args;
+ $this->createTestFixtures($args[1]);
+ return true;
}
- /**
- * The second parameter is a string of allowed short options. Each of the
- * option letters can be followed by a colon ':' to specify that the option
- * requires an argument, or a double colon '::' to specify that the option
- * takes an optional argument.
- *
- * The third argument is an optional array of allowed long options. The
- * leading '--' should not be included in the option name. Options that
- * require an argument should be followed by '=', and options that take an
- * option argument should be followed by '=='.
- *
- * @param string $short_options specifies the list of allowed short options
- * @param array $long_options specifies the list of allowed long options
- */
- protected function setOptions($short_options, $long_options=null)
+ protected function createTestFixtures($dir)
{
- $this->_short_options = $short_options;
- $this->_long_options = $long_options;
- }
+ if(strlen(trim($dir)) == 0)
+ return;
- /**
- * @return string list of options and its descriptions
- */
- public function renderOptions()
- {
- $options = array();
- $descriptions = array();
- $max = 0;
- foreach($this->_options as $option)
- {
- $short = str_replace(':','',$option[0]);
- $long = str_replace('=','',$option[1]);
- $desc = $option[2];
- $details = " -{$short}, --{$long} {$desc}";
- if(($len = strlen($details)) > $max)
- $max = $len;
- $options[] = $details;
- $descriptions[] = $option[3];
- }
+ $rootPath = realpath(dirname(trim($dir)));
+ $basePath = $rootPath.'/'.basename($dir);
- $content = '';
- for($i = 0, $k = count($options); $i < $k; $i++)
- $content .= str_pad($options[$i],$max+3).$descriptions[$i]."\n";
- return $content;
- }
+ $tests = $basePath.'/tests';
+ $unit_tests = $tests.'/unit';
+ $functional_tests = $tests.'/functional';
- /**
- * @param string argument name
- * @return string argument value
- */
- public function getOption($name)
- {
- if(is_null($this->_values))
- $this->_values = $this->getNamedOptions();
+ $this->createDirectory($tests,0755);
+ $this->createDirectory($unit_tests,0755);
+ $this->createDirectory($functional_tests,0755);
- return isset($this->_values[$name]) ? $this->_values[$name] : null;
- }
+ $unit_test_index = $tests.'/unit.php';
+ $functional_test_index = $tests.'/functional.php';
- /**
- * @return array list of all options given.
- */
- public function getOptions()
- {
- if(is_null($this->_values))
- $this->_values = $this->getNamedOptions();
- return $this->_values;
+ $this->createFile($unit_test_index, $this->renderUnitTestFixture());
+ $this->createFile($functional_test_index, $this->renderFunctionalTestFixture());
}
- /**
- * @return boolean true if one or more options are given.
- */
- public function hasOptions()
+ protected function renderUnitTestFixture()
{
- if(is_null($this->_values))
- $this->_values = $this->getNamedOptions();
- return count($this->_values) > 0;
- }
+ $tester = realpath(dirname(__FILE__).'/../tests/test_tools/unit_tests.php');
+return '<?php
- /**
- * Parse the options from args into named arguements.
- */
- protected function getNamedOptions()
- {
- $options = array();
- $values = $this->parseOptions($this->_args);
- foreach($values[0] as $value)
- {
- foreach($this->_options as $name => $option)
- {
- $short = str_replace(':', '', $option[0]);
- $long = str_replace('=', '', $option[1]);
- if($short == $value[0] || $long == $value[0])
- $options[$name] = is_null($value[1]) ? true : $value[1];
- }
- }
- return $options;
- }
+include_once \''.$tester.'\';
- /**
- * Gets the command-line options.
- *
- * The parameter to this function should be the list of command-line
- * arguments without the leading reference to the running program.
- *
- * The return value is an array of two elements: the list of parsed
- * options and the list of non-option command-line arguments. Each entry in
- * the list of parsed options is a pair of elements - the first one
- * specifies the option, and the second one specifies the option argument,
- * if there was one.
- *
- * Long and short options can be mixed.
- *
- * @param array $args an array of command-line arguments
- * @return array two-element array containing the list of parsed options and
- * the non-option arguments
- */
- public function parseOptions($args)
- {
- if (empty($args))
- return array(array(), array());
-
- $short_options = $this->_short_options;
- $long_options = $this->_long_options;
-
- $opts = array();
- $non_opts = array();
-
- settype($args, 'array');
-
- if ($long_options)
- sort($long_options);
-
- if (isset($args[0]{0}) && $args[0]{0} != '-')
- array_shift($args);
+$app_directory = "../protected";
+$test_cases = dirname(__FILE__)."/unit";
- reset($args);
+$tester = new PradoUnitTester($test_cases, $app_directory);
+$tester->run(new HtmlReporter());
- while (list($i, $arg) = each($args))
- {
+?>';
+ }
- /* The special element '--' means explicit end of
- options. Treat the rest of the arguments as non-options
- and end the loop. */
- if ($arg == '--') {
- $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
- break;
- }
+ protected function renderFunctionalTestFixture()
+ {
+ $tester = realpath(dirname(__FILE__).'/../tests/test_tools/functional_tests.php');
+return '<?php
- if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options))
- {
- $non_opts = array_merge($non_opts, array_slice($args, $i));
- break;
- }
- elseif (strlen($arg) > 1 && $arg{1} == '-')
- $this->parseLongOption(substr($arg, 2), $long_options, $opts, $args);
- else
- $this->parseShortOption(substr($arg, 1), $short_options, $opts, $args);
- }
+include_once \''.$tester.'\';
- return array($opts, $non_opts);
- }
+$test_cases = dirname(__FILE__)."/functional";
- private function parseShortOption($arg, $short_options, &$opts, &$args)
- {
- for ($i = 0; $i < strlen($arg); $i++)
- {
- $opt = $arg{$i};
- $opt_arg = null;
+$tester=new PradoFunctionalTester($test_cases);
+$tester->run(new SimpleReporter());
- /* Try to find the short option in the specifier string. */
- if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
- throw new Exception("Console_Getopt: unrecognized option -- $opt");
+?>';
+ }
- if (strlen($spec) > 1 && $spec{1} == ':')
- {
- if (strlen($spec) > 2 && $spec{2} == ':')
- {
- if ($i + 1 < strlen($arg))
- {
- /* Option takes an optional argument. Use the remainder of
- the arg string if there is anything left. */
- $opts[] = array($opt, substr($arg, $i + 1));
- break;
- }
- }
- else
- {
- /* Option requires an argument. Use the remainder of the arg
- string if there is anything left. */
- if ($i + 1 < strlen($arg))
- {
- $opts[] = array($opt, substr($arg, $i + 1));
- break;
- }
- else if (list(, $opt_arg) = each($args))
- /* Else use the next argument. */;
- else
- throw new Exception("Console_Getopt: option requires an argument -- $opt");
- }
- }
-
- $opts[] = array($opt, $opt_arg);
- }
- }
-
- private function parseLongOption($arg, $long_options, &$opts, &$args)
- {
- @list($opt, $opt_arg) = explode('=', $arg);
- $opt_len = strlen($opt);
-
- for ($i = 0; $i < count($long_options); $i++)
- {
- $long_opt = $long_options[$i];
- $opt_start = substr($long_opt, 0, $opt_len);
+}
- /* Option doesn't match. Go on to the next one. */
- if ($opt_start != $opt)
- continue;
+class PradoCommandLinePhpShell extends PradoCommandLineAction
+{
+ protected $action = 'shell';
+ protected $parameters = array();
+ protected $optional = array('directory');
+ protected $description = 'Runs a PHP interactive interpreter. Initializes the Prado application in the given [directory].';
- $opt_rest = substr($long_opt, $opt_len);
+ public function performAction($args)
+ {
+ if(count($args) > 1)
+ $this->createAndRunPradoApp($args);
+ return true;
+ }
- /* Check that the options uniquely matches one of the allowed
- options. */
- if ($opt_rest != '' && $opt{0} != '=' &&
- $i + 1 < count($long_options) &&
- $opt == substr($long_options[$i+1], 0, $opt_len))
- {
- throw new Exception("Console_Getopt: option --$opt is ambiguous");
- }
+ protected function createAndRunPradoApp($args)
+ {
+ $app_dir = realpath($args[1].'/protected/');
+ if($app_dir !== false)
+ {
+ $app = new PradoShellApplication($app_dir);
+ $app->setRequest(new DummyHttpRequest());
+ $app->run();
+ $dir = substr(str_replace(realpath('./'),'',$app_dir),1);
- if (substr($long_opt, -1) == '=')
- {
- if (substr($long_opt, -2) != '==')
- {
- /* Long option requires an argument.
- Take the next argument if one wasn't specified. */;
- if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args)))
- throw new Exception("Console_Getopt: option --$opt requires an argument");
- }
- }
- else if ($opt_arg)
- throw new Exception("Console_Getopt: option --$opt doesn't allow an argument");
-
- $opts[] = array($opt, $opt_arg);
- return;
- }
-
- throw new Exception("Console_Getopt: unrecognized option --$opt");
- }
+ echo '** Loaded Prado appplication in directory "'.$dir."\".\n";
+ }
+ else
+ echo '** Unable to load Prado application in directory "'.$args[1]."\".\n";
+ }
}
+
?> \ No newline at end of file