summaryrefslogtreecommitdiff
path: root/framework/prado-cli.php
diff options
context:
space:
mode:
authorwei <>2007-01-17 08:01:40 +0000
committerwei <>2007-01-17 08:01:40 +0000
commit3dc598bc7c2604e24b9e0be1189d9d78b43737ea (patch)
treefa3bc532607b6c70af3737fec80ebfed62debc5a /framework/prado-cli.php
parentd919dfa1674ddd5226834b81728e1772917d6240 (diff)
Add active record generator.
Diffstat (limited to 'framework/prado-cli.php')
-rwxr-xr-xframework/prado-cli.php233
1 files changed, 216 insertions, 17 deletions
diff --git a/framework/prado-cli.php b/framework/prado-cli.php
index 341ddb36..4c3e7c61 100755
--- a/framework/prado-cli.php
+++ b/framework/prado-cli.php
@@ -27,26 +27,27 @@ class PradoShellApplication extends TApplication
restore_exception_handler();
-//register action classes
-PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateProject');
-PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateTests');
-PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLinePhpShell');
-PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineUnitTest');
-
-//run it;
-PradoCommandLineInterpreter::getInstance()->run($_SERVER['argv']);
-
-//run PHP shell
+//config PHP shell
if(count($_SERVER['argv']) > 1 && strtolower($_SERVER['argv'][1])==='shell')
{
function __shell_print_var($shell,$var)
{
if(!$shell->has_semicolon) echo Prado::varDump($var);
}
- include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
+ include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-init.php');
}
+//register action classes
+PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateProject');
+PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateTests');
+PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLinePhpShell');
+PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineUnitTest');
+PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineActiveRecordGen');
+
+//run it;
+PradoCommandLineInterpreter::getInstance()->run($_SERVER['argv']);
+
/**************** END CONFIGURATION **********************/
/**
@@ -138,7 +139,7 @@ abstract class PradoCommandLineAction
* @param array command line parameters
* @return boolean true if action was handled
*/
- abstract public function performAction($args);
+ public abstract function performAction($args);
protected function createDirectory($dir, $mask)
{
@@ -191,14 +192,20 @@ EOD;
$app_dir = realpath($directory.'/protected/');
if($app_dir !== false)
{
- $app = new PradoShellApplication($app_dir);
- $app->run();
- $dir = substr(str_replace(realpath('./'),'',$app_dir),1);
+ if(Prado::getApplication()===null)
+ {
+ $app = new PradoShellApplication($app_dir);
+ $app->run();
+ $dir = substr(str_replace(realpath('./'),'',$app_dir),1);
+ $initialized=true;
+ echo '** Loaded Prado appplication in directory "'.$dir."\".\n";
+ }
- echo '** Loaded Prado appplication in directory "'.$dir."\".\n";
+ return Prado::getApplication();
}
else
echo '** Unable to load Prado application in directory "'.$directory."\".\n";
+ return false;
}
}
@@ -388,7 +395,7 @@ class PradoCommandLinePhpShell extends PradoCommandLineAction
public function performAction($args)
{
if(count($args) > 1)
- $this->initializePradoApplication($args[1]);
+ $app = $this->initializePradoApplication($args[1]);
return true;
}
}
@@ -509,4 +516,196 @@ class PradoCommandLineUnitTest extends PradoCommandLineAction
}
}
+/**
+ * Create active record skeleton
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @version $Id$
+ * @since 3.1
+ */
+class PradoCommandLineActiveRecordGen extends PradoCommandLineAction
+{
+ protected $action = 'generate';
+ protected $parameters = array('table', 'output');
+ protected $optional = array('directory', 'soap');
+ protected $description = 'Generate Active Record skeleton for <table> to <output> file using application.xml in [directory]. May also generate [soap] properties.';
+ private $_soap=false;
+
+ public function performAction($args)
+ {
+ $app_dir = count($args) > 3 ? $this->getAppDir($args[3]) : $this->getAppDir();
+ $this->_soap = count($args) > 4;
+ if($app_dir !== false)
+ {
+ $config = $this->getActiveRecordConfig($app_dir);
+ $output = $this->getOutputFile($app_dir, $args[2]);
+ if(is_file($output))
+ echo "** File $output already exists, skiping. \n";
+ else if($config !== false && $output !== false)
+ $this->generateActiveRecord($config, $args[1], $output);
+ }
+ return true;
+ }
+
+ protected function getAppDir($dir=".")
+ {
+ if(is_dir($dir))
+ return realpath($dir);
+ if(false !== ($app_dir = realpath($dir.'/protected/')))
+ return $app_dir;
+ echo '** Unable to find directory "'.$dir."\".\n";
+ return false;
+ }
+
+ protected function getXmlFile($app_dir)
+ {
+ if(false !== ($xml = realpath($app_dir.'/application.xml')))
+ return $xml;
+ if(false !== ($xml = realpath($app_dir.'/protected/application.xml')))
+ return $xml;
+ echo '** Unable to find application.xml in '.$app_dir."\n";
+ return false;
+ }
+
+ protected function getActiveRecordConfig($app_dir)
+ {
+ if(false === ($xml=$this->getXmlFile($app_dir)))
+ return false;
+ if(false !== ($app=$this->initializePradoApplication($app_dir)))
+ {
+ Prado::using('System.Data.ActiveRecord.TActiveRecordConfig');
+ foreach($app->getModules() as $module)
+ if($module instanceof TActiveRecordConfig)
+ return $module;
+ echo '** Unable to find TActiveRecordConfig module in '.$xml."\n";
+ }
+ return false;
+ }
+
+ protected function getOutputFile($app_dir, $namespace)
+ {
+ if(is_file($namespace) && strpos($namespace, $app_dir)===0)
+ return $namespace;
+ $file = Prado::getPathOfNamespace($namespace, ".php");
+ if($file !== null && false !== ($path = realpath(dirname($file))))
+ {
+ if(strpos($path, $app_dir)===0)
+ return $file;
+ }
+ echo '** Output file '.$file.' must be within directory '.$app_dir."\n";
+ return false;
+ }
+
+ protected function generateActiveRecord($config, $tablename, $output)
+ {
+ $manager = TActiveRecordManager::getInstance();
+ $inspector = $manager->getTableInspector($manager->getDbConnection());
+ $meta = $inspector->getTableMetaData($tablename);
+ if(count($meta->getColumns()) === 0)
+ {
+ echo '** Unable to find table or view "'.$tablename.'" in "'.$manager->getDbConnection()->getConnectionString()."\".\n";
+ return false;
+ }
+ else
+ {
+ $properties = array();
+ foreach($meta->getColumns() as $field=>$column)
+ $properties[] = $this->generateProperty($field,$column);
+ }
+
+ $classname = basename($output, '.php');
+ $class = $this->generateClass($properties, $tablename, $classname);
+ echo " Writing class $classname to file $output\n";
+ file_put_contents($output, $class);
+ }
+
+ protected function generateProperty($field,$column)
+ {
+ $prop = '';
+ $name = '$'.$field;
+ $type = $column->getPHPType();
+ if($this->_soap)
+ {
+ $prop .= <<<EOD
+ /**
+ * @var $type $name
+ * @soapproperty
+ */
+
+EOD;
+ }
+ $prop .= "\tpublic $name;\n";
+ return $prop;
+ }
+
+ protected function generateClass($properties, $tablename, $class)
+ {
+ $props = implode("\n", $properties);
+ $table = '$_tablename=\''.$tablename.'\'';
+ $date = date('Y-m-d h:i:s');
+return <<<EOD
+<?php
+/**
+ * Auto generated by prado-cli.php on $date.
+ */
+class $class extends TActiveRecord
+{
+ public static $table;
+
+$props
+ public static function finder()
+ {
+ return self::getRecordFinder('$class');
+ }
+}
+?>
+EOD;
+ }
+}
+
+//run PHP shell
+if(class_exists('PHP_Shell_Commands', false))
+{
+ class PHP_Shell_Extensions_ActiveRecord implements PHP_Shell_Extension {
+ public function register()
+ {
+
+ $cmd = PHP_Shell_Commands::getInstance();
+ if(Prado::getApplication() !== null)
+ {
+ $cmd->registerCommand('#^generate#', $this, 'generate', 'generate <table> <output> [soap]',
+ 'Generate Active Record skeleton for <table> to <output> file using'.PHP_EOL.
+ ' application.xml in [directory]. May also generate [soap] properties.');
+ }
+ }
+
+ public function generate($l)
+ {
+ $args = explode(" ", trim($l));
+ if(count($args) > 2)
+ {
+ if(count($args)>3)
+ {
+ $app_dir = '.';
+ if(Prado::getApplication()!==null)
+ $app_dir = dirname(Prado::getApplication()->getBasePath());
+ $args = array($args[0],$args[1], $args[2],$app_dir,'soap');
+ }
+ $cmd = new PradoCommandLineActiveRecordGen;
+ $cmd->performAction($args);
+ }
+ else
+ {
+ echo "\n Usage: generate table_name Application.pages.RecordClassName\n";
+ }
+ }
+ }
+
+ $__shell_exts = PHP_Shell_Extensions::getInstance();
+ $__shell_exts->registerExtensions(array(
+ "active-record" => new PHP_Shell_Extensions_ActiveRecord)); /* the :set command */
+
+ include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
+}
+
?> \ No newline at end of file