diff options
author | wei <> | 2007-01-17 08:01:40 +0000 |
---|---|---|
committer | wei <> | 2007-01-17 08:01:40 +0000 |
commit | 3dc598bc7c2604e24b9e0be1189d9d78b43737ea (patch) | |
tree | fa3bc532607b6c70af3737fec80ebfed62debc5a | |
parent | d919dfa1674ddd5226834b81728e1772917d6240 (diff) |
Add active record generator.
17 files changed, 485 insertions, 217 deletions
diff --git a/.gitattributes b/.gitattributes index 0d02fbc9..7d86a321 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1364,6 +1364,7 @@ framework/3rdParty/PhpShell/PHP/Shell/Extensions/VerbosePrint.php -text framework/3rdParty/PhpShell/PHP/Shell/Options.php -text framework/3rdParty/PhpShell/README -text framework/3rdParty/PhpShell/php-shell-cmd.php -text +framework/3rdParty/PhpShell/php-shell-init.php -text framework/3rdParty/SafeHtml/HTMLSax3.php -text framework/3rdParty/SafeHtml/HTMLSax3/Decorators.php -text framework/3rdParty/SafeHtml/HTMLSax3/States.php -text diff --git a/framework/3rdParty/PhpShell/PHP/Shell.php b/framework/3rdParty/PhpShell/PHP/Shell.php index 87dc7e67..bf8c86c3 100644 --- a/framework/3rdParty/PhpShell/PHP/Shell.php +++ b/framework/3rdParty/PhpShell/PHP/Shell.php @@ -1,5 +1,5 @@ <?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/*
(c) 2006 Jan Kneschke <jan@kneschke.de>
@@ -25,13 +25,13 @@ SOFTWARE. /**
* A interactive PHP Shell
-*
+*
* The more I work with other languages like python and ruby I like their way how they
* work on problems. While PHP is very forgiving on errors, it is weak on the debugging
* side. It was missing a simple to use interactive shell for years. Python and Ruby have
* their ipython and iruby shell which give you a direct way to interact with the objects.
* No need to write a script and execute it afterwards.
-*
+*
* Starting the Shell:
*
* The package contains a shell wrapper for windows and unix:
@@ -39,20 +39,20 @@ SOFTWARE. * sh> php-shell.sh
* win> php-shell
* </pre>
-*
+*
* Both are calling the wrapper script <code>php -q php-shell-cmd.php</code>
-*
+*
* Inline Help
*
* <pre>
* PHP-Shell - Version 0.2.0, with readline() support
* (c) 2006, Jan Kneschke <jan@kneschke.de>
-*
+*
* >> use '?' to open the inline help
-*
+*
* >> ?
* "inline help for the PHP-shell
-*
+*
* >> ?
* print this help
* >> ? <topic>
@@ -65,11 +65,11 @@ SOFTWARE. * >> ? PHP_Shell
* </pre>
* Alternatives
-*
+*
* - http://david.acz.org/phpa/
* - http://www.hping.org/phpinteractive/
-* - the embedded interactive php-shell: $ php -a
-*
+* - the embedded interactive php-shell: $ php -a
+*
* @package PHP
*/
@@ -92,20 +92,20 @@ require_once(dirname(__FILE__)."/Shell/Commands.php"); require_once(dirname(__FILE__)."/Shell/Options.php"); /* for the tab-complete */
class PHP_Shell {
- /**
+ /**
* current code-buffer
* @var string
*/
- protected $code;
+ protected $code;
- /**
- * set if readline support is enabled
+ /**
+ * set if readline support is enabled
* @var bool
*/
- protected $have_readline;
+ protected $have_readline;
- /**
- * current version of the class
+ /**
+ * current version of the class
* @var string
*/
protected $version = '0.3.1';
@@ -121,7 +121,7 @@ class PHP_Shell { /**
* init the shell and change if readline support is available
- */
+ */
public function __construct() {
$this->code = '';
@@ -142,7 +142,7 @@ class PHP_Shell { $cmd->registerCommand('#^\?\s+license$#', $this, 'cmdLicense', '? license', 'show license of the shell');
}
-
+
/**
* parse the PHP code
*
@@ -157,7 +157,7 @@ class PHP_Shell { if (trim($this->code) == '') return 1;
$t = token_get_all('<?php '.$this->code.' ?>');
-
+
$need_semicolon = 1; /* do we need a semicolon to complete the statement ? */
$need_return = 1; /* can we prepend a return to the eval-string ? */
$open_comment = 0; /* a open multi-line comment */
@@ -170,7 +170,7 @@ class PHP_Shell { foreach ($t as $ndx => $token) {
if (is_array($token)) {
$ignore = 0;
-
+
switch($token[0]) {
case T_WHITESPACE:
case T_OPEN_TAG:
@@ -184,7 +184,7 @@ class PHP_Shell { case T_IF:
case T_RETURN:
-
+
case T_CLASS:
case T_FUNCTION:
case T_INTERFACE:
@@ -253,7 +253,7 @@ class PHP_Shell { case T_IS_IDENTICAL:
case T_IS_GREATER_OR_EQUAL:
case T_IS_SMALLER_OR_EQUAL:
-
+
case T_BOOLEAN_OR:
case T_LOGICAL_OR:
case T_BOOLEAN_AND:
@@ -282,7 +282,7 @@ class PHP_Shell { default:
/* debug unknown tags*/
error_log(sprintf("unknown tag: %d (%s): %s".PHP_EOL, $token[0], token_name($token[0]), $token[1]));
-
+
break;
}
if (!$ignore) {
@@ -303,10 +303,10 @@ class PHP_Shell { $ts[$last - 2]['token'] == T_OBJECT_OPERATOR &&
$ts[$last - 3]['token'] == ')' ) {
/* func()->method()
- *
- * we can't know what func() is return, so we can't
+ *
+ * we can't know what func() is return, so we can't
* say if the method() exists or not
- *
+ *
*/
} else if ($last >= 3 &&
$ts[0]['token'] != T_CLASS && /* if we are not in a class definition */
@@ -330,22 +330,22 @@ class PHP_Shell { if (!$in_catch) {
/* $object has to exist and has to be a object */
$objname = $ts[$last - 3]['value'];
-
+
if (!isset($GLOBALS[ltrim($objname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
$object = $GLOBALS[ltrim($objname, '$')];
-
+
if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
-
+
$method = $ts[$last - 1]['value'];
/* obj */
-
+
if (!method_exists($object, $method)) {
- throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
+ throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
}
@@ -359,7 +359,7 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$objname = $ts[$last - 3]['value'];
-
+
if (!isset($GLOBALS[ltrim($objname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
@@ -368,7 +368,7 @@ class PHP_Shell { if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
-
+
$methodname = $ts[$last - 1]['value'];
if (!isset($GLOBALS[ltrim($methodname, '$')])) {
@@ -377,9 +377,9 @@ class PHP_Shell { $method = $GLOBALS[ltrim($methodname, '$')];
/* obj */
-
+
if (!method_exists($object, $method)) {
- throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
+ throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
@@ -396,7 +396,7 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$objname = $ts[$last - 6]['value'];
-
+
if (!isset($GLOBALS[ltrim($objname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
@@ -417,13 +417,13 @@ class PHP_Shell { if (!is_object($object)) {
throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
}
-
+
$method = $ts[$last - 1]['value'];
/* obj */
-
+
if (!method_exists($object, $method)) {
- throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
+ throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
$objname, get_class($object), $method));
}
@@ -437,15 +437,15 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$classname = $ts[$last - 3]['value'];
-
+
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
-
+
$method = $ts[$last - 1]['value'];
if (!in_array($method, get_class_methods($classname))) {
- throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
+ throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
$classname, $method));
}
} else if ($last >= 3 &&
@@ -458,11 +458,11 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$classname = $ts[$last - 3]['value'];
-
+
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
-
+
$methodname = $ts[$last - 1]['value'];
if (!isset($GLOBALS[ltrim($methodname, '$')])) {
@@ -471,7 +471,7 @@ class PHP_Shell { $method = $GLOBALS[ltrim($methodname, '$')];
if (!in_array($method, get_class_methods($classname))) {
- throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
+ throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
$classname, $method));
}
@@ -540,17 +540,17 @@ class PHP_Shell { $ts[$last - 1]['token'] == T_STRING ) {
/* func() */
$funcname = $ts[$last - 1]['value'];
-
+
if (!function_exists($funcname)) {
throw new Exception(sprintf("Function %s() doesn't exist", $funcname));
}
} else if ($last >= 1 &&
$ts[0]['token'] != T_CLASS && /* if we are not in a class definition */
$ts[$last - 1]['token'] == T_VARIABLE ) {
-
+
/* $object has to exist and has to be a object */
$funcname = $ts[$last - 1]['value'];
-
+
if (!isset($GLOBALS[ltrim($funcname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $funcname));
}
@@ -561,7 +561,7 @@ class PHP_Shell { }
}
-
+
array_push($braces, $token);
break;
case '{':
@@ -590,11 +590,11 @@ class PHP_Shell { $extendsname = $ts[$last - 1]['value'];
if (class_exists($classname, false)) {
- throw new Exception(sprintf("Class '%s' can't be redeclared",
+ throw new Exception(sprintf("Class '%s' can't be redeclared",
$classname));
}
if (!class_exists($extendsname, true)) {
- throw new Exception(sprintf("Can't extend '%s' ... from not existing Class '%s'",
+ throw new Exception(sprintf("Can't extend '%s' ... from not existing Class '%s'",
$classname, $extendsname));
}
} else if ($last >= 4 &&
@@ -609,11 +609,11 @@ class PHP_Shell { $implements = $ts[$last - 1]['value'];
if (class_exists($classname, false)) {
- throw new Exception(sprintf("Class '%s' can't be redeclared",
+ throw new Exception(sprintf("Class '%s' can't be redeclared",
$classname));
}
if (!interface_exists($implements, false)) {
- throw new Exception(sprintf("Can't implement not existing Interface '%s' for Class '%s'",
+ throw new Exception(sprintf("Can't implement not existing Interface '%s' for Class '%s'",
$implements, $classname));
}
}
@@ -634,7 +634,7 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$objname = $ts[$last - 1]['value'];
-
+
if (!isset($GLOBALS[ltrim($objname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
}
@@ -646,9 +646,9 @@ class PHP_Shell { }
break;
}
-
+
$eval .= $token;
- }
+ }
}
$last = count($ts) - 1;
@@ -661,16 +661,16 @@ class PHP_Shell { /* $object has to exist and has to be a object */
$classname = $ts[$last - 2]['value'];
-
+
if (!class_exists($classname)) {
throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
}
-
+
$constname = $ts[$last - 0]['value'];
$c = new ReflectionClass($classname);
if (!$c->hasConstant($constname)) {
- throw new Exception(sprintf("Class '%s' doesn't have a constant named '%s'",
+ throw new Exception(sprintf("Class '%s' doesn't have a constant named '%s'",
$classname, $constname));
}
} else if ($last == 0 &&
@@ -679,7 +679,7 @@ class PHP_Shell { /* $var */
$varname = $ts[$last - 0]['value'];
-
+
if (!isset($GLOBALS[ltrim($varname, '$')])) {
throw new Exception(sprintf('Variable \'%s\' is not set', $varname));
}
@@ -690,31 +690,31 @@ class PHP_Shell { if ($need_more || ';' === $token) {
$need_semicolon = 0;
- }
-
+ }
+
if ($need_return) {
$eval = "return ".$eval;
}
-
- /* add a traling ; if necessary */
- if ($need_semicolon)
+
+ /* add a traling ; if necessary */
+ if ($need_semicolon)
{
$this->has_semicolon = preg_match('/;\s*$/', $eval);
$eval .= ';';
}
-
+
if (!$need_more) {
$this->code = $eval;
}
-
+
return $need_more;
}
-
+
/**
* show the prompt and fetch a single line
- *
+ *
* uses readline() if avaialbe
- *
+ *
* @return string a input-line
*/
public function readline() {
@@ -752,27 +752,28 @@ class PHP_Shell { /**
* get the inline help
*
- * @return string the inline help as string
+ * @return string the inline help as string
*/
public function cmdHelp($l) {
$o = 'Inline Help:'.PHP_EOL;
$cmds = PHP_Shell_Commands::getInstance()->getCommands();
+ $help = array();
foreach ($cmds as $cmd) {
- $o .= sprintf(' >> %s'.PHP_EOL.' %s'.PHP_EOL,
+ $help[] = sprintf(' >> %s'.PHP_EOL.' %s'.PHP_EOL,
$cmd['command'],
$cmd['description']
);
}
- return var_export($o, 1);
+ return var_export(implode("\n", $help), 1);
}
/**
* get the license string
*
- * @return string the inline help as string
+ * @return string the inline help as string
*/
public function cmdLicense($l) {
$o = <<<EOF
@@ -824,7 +825,7 @@ EOF; if (false === $l) return false;
$l = trim($l);
-
+
if (empty($this->code)) {
$this->verbose = 0;
@@ -848,22 +849,22 @@ EOF; }
}
}
-
- $this->appendCode($l);
+
+ $this->appendCode($l);
return true;
}
-
+
/**
* get the code-buffer
- *
+ *
* @return string the code-buffer
*/
public function getCode() {
return $this->code;
return $code;
}
-
+
/**
* reset the code-buffer
*/
@@ -871,7 +872,7 @@ EOF; $this->has_semicolon=false;
$this->code = '';
}
-
+
/**
* append code to the code-buffer
*
@@ -882,7 +883,7 @@ EOF; $this->code .= $code;
}
-
+
/**
* check if readline support is enabled
*
@@ -913,9 +914,9 @@ function __shell_readline_complete($str, $pos) { $in = readline_info('line_buffer');
/**
- * parse the line-buffer backwards to see if we have a
+ * parse the line-buffer backwards to see if we have a
* - constant
- * - function
+ * - function
* - variable
*/
@@ -943,7 +944,7 @@ function __shell_readline_complete($str, $pos) { /* check for $o[...]->... */
$name = $a[1];
- if (isset($GLOBALS[$name]) &&
+ if (isset($GLOBALS[$name]) &&
is_array($GLOBALS[$name]) &&
isset($GLOBALS[$name][$a[2]])) {
@@ -986,7 +987,7 @@ function __shell_readline_complete($str, $pos) { return $m;
} else if (preg_match('#new #', $in)) {
$c = get_declared_classes();
-
+
foreach ($c as $v) {
$m[] = $v.'(';
}
@@ -1009,7 +1010,7 @@ function __shell_readline_complete($str, $pos) { foreach ($f['user'] as $v) {
$m[] = $v.'(';
}
-
+
$c = get_declared_classes();
foreach ($c as $v) {
diff --git a/framework/3rdParty/PhpShell/PHP/Shell/Commands.php b/framework/3rdParty/PhpShell/PHP/Shell/Commands.php index 5a5e7e9b..3d4bf5b2 100644 --- a/framework/3rdParty/PhpShell/PHP/Shell/Commands.php +++ b/framework/3rdParty/PhpShell/PHP/Shell/Commands.php @@ -3,14 +3,14 @@ /** * Commands for the PHP_Shell * -* Extensions can register their own commands for the shell like the +* Extensions can register their own commands for the shell like the * InlineHelp Extension which provides inline help for all functions * -* It uses the pattern '? <string>' to catch the cmdline strings. +* It uses the pattern '? <string>' to catch the cmdline strings. * -* registerCommand() should be called by the extensions in the register() +* registerCommand() should be called by the extensions in the register() * method. Its parameters are -* - the regex which matches the command +* - the regex which matches the command * - the object and the method to call if the command is matched * - the human readable command string and the description for the help */ @@ -23,7 +23,7 @@ class PHP_Shell_Commands { static protected $instance; /** - * registered commands + * registered commands * * array('quit' => ... ) * diff --git a/framework/3rdParty/PhpShell/PHP/Shell/Extensions/VerbosePrint.php b/framework/3rdParty/PhpShell/PHP/Shell/Extensions/VerbosePrint.php index 843292b0..425937c1 100644 --- a/framework/3rdParty/PhpShell/PHP/Shell/Extensions/VerbosePrint.php +++ b/framework/3rdParty/PhpShell/PHP/Shell/Extensions/VerbosePrint.php @@ -4,12 +4,12 @@ class PHP_Shell_Extensions_VerbosePrint implements PHP_Shell_Extension { protected $oneshot_verbose = false; public function register() { - $cmd = PHP_Shell_Commands::getInstance(); +/* $cmd = PHP_Shell_Commands::getInstance(); $cmd->registerCommand('#^p #', $this, 'cmdPrint', 'p <var>', 'print the variable verbosly'); $opt = PHP_Shell_Options::getInstance(); $opt->registerOption('verboseprint', $this, 'optSetVerbose'); - +*/ } /** @@ -48,7 +48,7 @@ class PHP_Shell_Extensions_VerbosePrint implements PHP_Shell_Extension { $v = $this->opt_verbose || $this->oneshot_verbose; $this->oneshot_verbose = false; - + return $v; } } diff --git a/framework/3rdParty/PhpShell/PHP/Shell/Options.php b/framework/3rdParty/PhpShell/PHP/Shell/Options.php index 649d6727..8f5e57d1 100644 --- a/framework/3rdParty/PhpShell/PHP/Shell/Options.php +++ b/framework/3rdParty/PhpShell/PHP/Shell/Options.php @@ -1,7 +1,7 @@ <?php require_once(dirname(__FILE__)."/Extensions.php"); /* for the PHP_Shell_Interface */ - + /** * */ @@ -28,10 +28,10 @@ class PHP_Shell_Options implements PHP_Shell_Extension { * @see registerOptionAlias */ protected $option_aliases = array(); - + public function register() { - $cmd = PHP_Shell_Commands::getInstance(); - $cmd->registerCommand('#^:set #', $this, 'cmdSet', ':set <var>', 'set a shell variable'); +// $cmd = PHP_Shell_Commands::getInstance(); + // $cmd->registerCommand('#^:set #', $this, 'cmdSet', ':set <var>', 'set a shell variable'); } /** @@ -41,7 +41,7 @@ class PHP_Shell_Options implements PHP_Shell_Extension { * @param object a object handle * @param string method-name of the setor in the object * @param string (unused) - */ + */ public function registerOption($option, $obj, $setor, $getor = null) { if (!method_exists($obj, $setor)) { throw new Exception(sprintf("setor %s doesn't exist on class %s", $setor, get_class($obj))); @@ -88,15 +88,15 @@ class PHP_Shell_Options implements PHP_Shell_Extension { } $this->option_aliases[trim($alias)] = trim($option); - + } /** * execute a :set command * * calls the setor for the :set <option> - * - * + * + * */ private function execute($key, $value) { /* did we hit a alias (bg for backgroud) ? */ @@ -110,7 +110,7 @@ class PHP_Shell_Options implements PHP_Shell_Extension { print (':set '.$key.' failed: unknown key'); return; } - + if (!isset($this->options[$opt_key]["setor"])) { return; } diff --git a/framework/3rdParty/PhpShell/php-shell-cmd.php b/framework/3rdParty/PhpShell/php-shell-cmd.php index 968a3369..aec6d6ed 100644 --- a/framework/3rdParty/PhpShell/php-shell-cmd.php +++ b/framework/3rdParty/PhpShell/php-shell-cmd.php @@ -1,89 +1,4 @@ <?php -@ob_end_clean(); -error_reporting(E_ALL); -set_time_limit(0); - -/** -* the wrapper around the PHP_Shell class -* -* - load extensions -* - set default error-handler -* - add exec-hooks for the extensions -* -* To keep the namespace clashing between shell and your program -* as small as possible all public variables and functions from -* the shell are prefixed with __shell: -* -* - $__shell is the object of the shell -* can be read, this is the shell object itself, don't touch it -* - $__shell_retval is the return value of the eval() before -* it is printed -* can't be read, but overwrites existing vars with this name -* - $__shell_exception is the catched Exception on Warnings, Notices, .. -* can't be read, but overwrites existing vars with this name -*/ - -//try loading it from PEAR -@require_once('PHP/Shell.php'); -if(class_exists('PHP_Shell', false)) -{ - require_once "PHP/Shell/Extensions/Autoload.php"; - require_once "PHP/Shell/Extensions/AutoloadDebug.php"; - require_once "PHP/Shell/Extensions/Colour.php"; - require_once "PHP/Shell/Extensions/ExecutionTime.php"; - require_once "PHP/Shell/Extensions/InlineHelp.php"; - require_once "PHP/Shell/Extensions/VerbosePrint.php"; - require_once "PHP/Shell/Extensions/LoadScript.php"; -} -else -{ - require_once(dirname(__FILE__)."/PHP/Shell.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/Autoload.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/AutoloadDebug.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/Colour.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/ExecutionTime.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/InlineHelp.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/VerbosePrint.php"); - require_once(dirname(__FILE__)."/PHP/Shell/Extensions/LoadScript.php"); -} - -/** -* default error-handler -* -* Instead of printing the NOTICE or WARNING from php we wan't the turn non-FATAL -* messages into exceptions and handle them in our own way. -* -* you can set your own error-handler by createing a function named -* __shell_error_handler -* -* @param integer $errno Error-Number -* @param string $errstr Error-Message -* @param string $errfile Filename where the error was raised -* @param interger $errline Line-Number in the File -* @param mixed $errctx ... -*/ -function __shell_default_error_handler($errno, $errstr, $errfile, $errline, $errctx) { - ## ... what is this errno again ? - if ($errno == 2048) return; - - throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr)); -} - -set_error_handler("__shell_default_error_handler"); - -$__shell = new PHP_Shell(); -$__shell_exts = PHP_Shell_Extensions::getInstance(); -$__shell_exts->registerExtensions(array( - "options" => PHP_Shell_Options::getInstance(), /* the :set command */ - - "autoload" => new PHP_Shell_Extensions_Autoload(), - "autoload_debug" => new PHP_Shell_Extensions_AutoloadDebug(), - "colour" => new PHP_Shell_Extensions_Colour(), - "exectime" => new PHP_Shell_Extensions_ExecutionTime(), - "inlinehelp" => new PHP_Shell_Extensions_InlineHelp(), - "verboseprint" => new PHP_Shell_Extensions_VerbosePrint(), - "loadscript" => new PHP_Shell_Extensions_LoadScript() -)); $f = <<<EOF PHP-Shell - Version %s%s diff --git a/framework/3rdParty/PhpShell/php-shell-init.php b/framework/3rdParty/PhpShell/php-shell-init.php new file mode 100644 index 00000000..e8da5a7d --- /dev/null +++ b/framework/3rdParty/PhpShell/php-shell-init.php @@ -0,0 +1,88 @@ +<?php
+@ob_end_clean();
+error_reporting(E_ALL);
+set_time_limit(0);
+
+/**
+* the wrapper around the PHP_Shell class
+*
+* - load extensions
+* - set default error-handler
+* - add exec-hooks for the extensions
+*
+* To keep the namespace clashing between shell and your program
+* as small as possible all public variables and functions from
+* the shell are prefixed with __shell:
+*
+* - $__shell is the object of the shell
+* can be read, this is the shell object itself, don't touch it
+* - $__shell_retval is the return value of the eval() before
+* it is printed
+* can't be read, but overwrites existing vars with this name
+* - $__shell_exception is the catched Exception on Warnings, Notices, ..
+* can't be read, but overwrites existing vars with this name
+*/
+
+//try loading it from PEAR
+@require_once('PHP/Shell.php');
+if(class_exists('PHP_Shell', false))
+{
+ require_once "PHP/Shell/Extensions/Autoload.php";
+ require_once "PHP/Shell/Extensions/AutoloadDebug.php";
+ require_once "PHP/Shell/Extensions/Colour.php";
+ require_once "PHP/Shell/Extensions/ExecutionTime.php";
+ require_once "PHP/Shell/Extensions/InlineHelp.php";
+ require_once "PHP/Shell/Extensions/VerbosePrint.php";
+ require_once "PHP/Shell/Extensions/LoadScript.php";
+}
+else
+{
+ require_once(dirname(__FILE__)."/PHP/Shell.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/Autoload.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/AutoloadDebug.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/Colour.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/ExecutionTime.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/InlineHelp.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/VerbosePrint.php");
+ require_once(dirname(__FILE__)."/PHP/Shell/Extensions/LoadScript.php");
+}
+
+/**
+* default error-handler
+*
+* Instead of printing the NOTICE or WARNING from php we wan't the turn non-FATAL
+* messages into exceptions and handle them in our own way.
+*
+* you can set your own error-handler by createing a function named
+* __shell_error_handler
+*
+* @param integer $errno Error-Number
+* @param string $errstr Error-Message
+* @param string $errfile Filename where the error was raised
+* @param interger $errline Line-Number in the File
+* @param mixed $errctx ...
+*/
+function __shell_default_error_handler($errno, $errstr, $errfile, $errline, $errctx) {
+ ## ... what is this errno again ?
+ if ($errno == 2048) return;
+
+ throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr));
+}
+
+set_error_handler("__shell_default_error_handler");
+
+$__shell = new PHP_Shell();
+$__shell_exts = PHP_Shell_Extensions::getInstance();
+$__shell_exts->registerExtensions(array(
+ "options" => PHP_Shell_Options::getInstance(), /* the :set command */
+
+ "autoload" => new PHP_Shell_Extensions_Autoload(),
+ "autoload_debug" => new PHP_Shell_Extensions_AutoloadDebug(),
+ "colour" => new PHP_Shell_Extensions_Colour(),
+ "exectime" => new PHP_Shell_Extensions_ExecutionTime(),
+ "inlinehelp" => new PHP_Shell_Extensions_InlineHelp(),
+ "verboseprint" => new PHP_Shell_Extensions_VerbosePrint()
+ // "loadscript" => new PHP_Shell_Extensions_LoadScript()
+));
+
+?>
\ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php index d3e25dcf..9272e1b9 100644 --- a/framework/Data/ActiveRecord/TActiveRecord.php +++ b/framework/Data/ActiveRecord/TActiveRecord.php @@ -97,13 +97,27 @@ abstract class TActiveRecord extends TComponent */
public function __construct($data=array(), $connection=null)
{
- foreach($data as $name=>$value)
- $this->$name = $value;
+ $this->copyFrom($data);
if($connection!==null)
$this->_connection=$connection;
}
/**
+ * Copies data from an array or another object.
+ * @throws TActiveRecordException if data is not array or not object.
+ * @return TActiveRecord current instance.
+ */
+ public function copyFrom($data)
+ {
+ $data = is_object($data) ? get_object_vars($data) : $data;
+ if(!is_array($data))
+ throw new TActiveRecordException('ar_must_copy_from_array_or_object', get_class($this));
+ foreach($data as $name=>$value)
+ $this->$name = $value;
+ return $this;
+ }
+
+ /**
* Gets the current Db connection, the connection object is obtained from
* the TActiveRecordManager if connection is currently null.
* @return TDbConnection current db connection for this object.
@@ -212,7 +226,7 @@ abstract class TActiveRecord extends TComponent /**
- * Delete multiple records using a criteria.
+ * Delete multiple records using a criteria.
* @param string|TActiveRecordCriteria SQL condition or criteria object.
* @param mixed parameter values.
* @return int number of records deleted.
diff --git a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php index 7f7dad8b..6e157785 100644 --- a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php +++ b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php @@ -67,7 +67,7 @@ abstract class TDbMetaDataCommon extends TDbMetaData public function getFindByCriteriaCommand($conn, $criteria=null)
{
$columns = $this->getSelectionColumns();
- $conditions = $criteria!==null?$this->getSqlFromCriteria($conn,$criteria) : '';
+ $conditions = $this->getSqlFromCriteria($conn,$criteria);
$table = $this->getTableName();
$sql = "SELECT {$columns} FROM {$table} {$conditions}";
return $this->createCriteriaBindedCommand($conn,$sql, $criteria);
@@ -88,7 +88,7 @@ abstract class TDbMetaDataCommon extends TDbMetaData return $this->createCriteriaBindedCommand($conn,$sql, $criteria);
}
- abstract protected function getSqlFromCriteria($conn,TActiveRecordCriteria $criteria);
+ abstract protected function getSqlFromCriteria($conn, $criteria);
/**
* Sql command with parameters binded.
diff --git a/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php index b5bd4050..e7e7ceb8 100644 --- a/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php @@ -56,6 +56,21 @@ class TMysqlColumnMetaData extends TComponent return $this->_name;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'tinyint': case 'smallint': case 'mediumint': case 'int': case 'year':
+ return 'integer';
+ case 'bool':
+ return 'boolean';
+ case 'bigint': case 'float': case 'double': case 'decimal':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return boolean true if column is a sequence, false otherwise.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php b/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php index b0b2ef87..0cbd0b7c 100644 --- a/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php @@ -28,8 +28,9 @@ class TMysqlMetaData extends TDbMetaDataCommon * @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php index d6ea8ca7..59a2d43e 100644 --- a/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php @@ -57,6 +57,21 @@ class TPgsqlColumnMetaData extends TComponent return $this->_name;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'bit': case 'bit varying': case 'real': case 'serial':
+ return 'integer';
+ case 'boolean':
+ return 'boolean';
+ case 'bigint': case 'bigserial': case 'double precision': case 'money': case 'numeric':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return boolean true if column is a sequence, false otherwise.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php b/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php index ffb2fc31..d968267a 100644 --- a/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php @@ -30,8 +30,9 @@ class TPgsqlMetaData extends TDbMetaDataCommon * @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php index 366bfce2..abb50caf 100644 --- a/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php @@ -39,6 +39,21 @@ class TSqliteColumnMetaData extends TComponent $this->_primary=$primary;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'int': case 'integer': case 'mediumint': case 'smallint': case 'tinyint': case 'year':
+ return 'integer';
+ case 'boolean':
+ return 'boolean';
+ case 'decimal': case 'double': case 'float': case 'bigint':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return string quoted column name.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php b/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php index 129a0acf..c82a99ad 100644 --- a/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php @@ -28,8 +28,9 @@ class TSqliteMetaData extends TDbMetaDataCommon * @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/TDbCommand.php b/framework/Data/TDbCommand.php index 054aa653..5c824b8f 100644 --- a/framework/Data/TDbCommand.php +++ b/framework/Data/TDbCommand.php @@ -200,7 +200,9 @@ class TDbCommand extends TComponent protected function getDebugStatementText()
{
if(Prado::getApplication()->getMode() === TApplicationMode::Debug)
- return $this->_statement instanceof PDOStatement ? $this->getConnection()->getPdoInstance().$this->_statement->queryString : $this->getText();
+ return $this->_statement instanceof PDOStatement ?
+ $this->_statement->queryString
+ : $this->getText();
}
/**
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 |