From 0269aefc30d65ddb5d197bac7819feed5bf78de8 Mon Sep 17 00:00:00 2001
From: Fabio Bas
@@ -141,5 +139,4 @@ At the prompt, enter the following two commands to create UserRecord an Here we used the namespace format again to specify the classes to be created. The path Application.database.UserRecord indicates that we want the UserRecord class file to be protected/database/UserRecord.php.
->>>>>>> ef81c44... Quickstart Doc overhaul, pt. 2: active controls + minor fixes diff --git a/framework/Wsat/TWsatARGenerator.php b/framework/Wsat/TWsatARGenerator.php new file mode 100644 index 00000000..7ae5e46e --- /dev/null +++ b/framework/Wsat/TWsatARGenerator.php @@ -0,0 +1,283 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2013 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @since 3.3 + * @package Wsat + */ + +Prado::using('System.Data.Common.TDbMetaData'); + +class TWsatARGenerator +{ + + /** + * @return TDbMetaData for retrieving metadata information, such as + * table and columns information, from a database connection. + */ + private $_dbMetaData; + + /** + * Output folder where AR classes will be saved. + */ + private $_opFile; + + /** + * Class name prefix + */ + private $_clasPrefix; + + /** + * Class name sufix + */ + private $_classSufix; + + /** + * all table relations array + */ + private $_relations; + + /** + * unquote chars + * @var array + */ + private $uqChars = array('[', ']', '"', '`', "'"); + + function __construct() + { + if(!class_exists("TActiveRecordManager", false)) + throw new Exception("You need to enable the ActiveRecord module in your application configuration file."); + $ar_manager = TActiveRecordManager::getInstance(); + $_conn = $ar_manager->getDbConnection(); + $_conn->Active = true; + $this->_dbMetaData = TDbMetaData::getInstance($_conn); + } + + public function setOpFile($op_file_namespace) + { + $op_file = Prado::getPathOfNamespace($op_file_namespace); + if (empty($op_file)) + throw new Exception("You need to fix your output folder namespace."); + if (!is_dir($op_file)) + mkdir($op_file, 0777, true); + $this->_opFile = $op_file; + } + + public function setClasPrefix($_clas_prefix) + { + $this->_clasPrefix = $_clas_prefix; + } + + public function setClassSufix($_clas_sufix) + { + $this->_classSufix = $_clas_sufix; + } + +//----------------------------------------------------------------------------- + //"; + var_dump($tableInfo); + echo ""; + } + } + +//----------------------------------------------------------------------------- + + protected function generateProperty($field, $column) + { + $prop = ''; + $name = '$' . $field; + + /* TODO use in version 2.0 */ + // $type = $column->getPHPType(); + + $prop .= "\tpublic $name;"; + return $prop; + } + + private function _renderRelations($tablename) + { + if (!isset($this->_relations[$tablename])) + return ""; + + $code = "\tpublic static \$RELATIONS = array ("; + foreach ($this->_relations[$tablename] as $rel_data) + $code .= "\n\t\t'" . $rel_data["prop_name"] . "' => array(" . $rel_data["rel_type"] . ", '" . $rel_data["ref_class_name"] . "', '" . $rel_data["prop_ref"] . "'),"; + + $code = substr($code, 0, -1); + $code .= "\n\t);"; + return $code; + } + + private function _buildSmartToString($tableInfo) + { + $code = "\tpublic function __toString() {"; + $property = "throw new THttpException(500, 'Not implemented yet.');"; + try + { + foreach ($tableInfo->getColumns() as $column) + { + if (isset($column->IsPrimaryKey) && $column->IsPrimaryKey) + $property = str_replace($this->uqChars, "", $column->ColumnName); + elseif ($column->PdoType == PDO::PARAM_STR && $column->DBType != "date") + { + $property = str_replace($this->uqChars, "", $column->ColumnName); + break; + } + } + } catch (Exception $ex) + { + Prado::trace($ex->getMessage()); + } + $code .= "\n\t\treturn \$this->$property;"; + $code .= "\n\t}"; + return $code; + } + + protected function generateClass($properties, $tablename, $classname, $toString) + { + $props = implode("\n", $properties); + $relations = $this->_renderRelations($tablename); + $date = date('Y-m-d h:i:s'); + $env_user = getenv("username"); + return <<
+ *
+ * ...
+ *
+ *
+ *
+ * ...and then you need to go to http://localhost/yoursite/index.php?wsat=TWsatLogin
+ * and generate code and configure your site.
+ *
+ * Warning: You should only use Wsat in development mode.
+ */
+class TWsatService extends TPageService
+{
+
+ private $_pass = '';
+
+ public function init($config)
+ {
+ if ($this->getApplication()->getMode() === TApplicationMode::Performance || $this->getApplication()->getMode() === TApplicationMode::Normal)
+ throw new TInvalidOperationException("You should not use Prado WSAT in any of the production modes.");
+
+ if (empty($this->_pass))
+ throw new TConfigurationException("You need to specify the Password attribute.");
+
+ $this->setDefaultPage("TWsatHome");
+ $this->_startThemeManager();
+ parent::init($config);
+ }
+
+ public function getBasePath()
+ {
+ $basePath = Prado::getPathOfNamespace("System.Wsat.pages");
+ return realpath($basePath);
+ }
+
+ private function _startThemeManager()
+ {
+ $themeManager = new TThemeManager;
+ $themeManager->BasePath = "System.Wsat.themes";
+ $url = Prado::getApplication()->getAssetManager()->publishFilePath(Prado::getPathOfNamespace('System.Wsat'));
+ $themeManager->BaseUrl = "$url/themes";
+
+ $themeManager->init(null);
+ $this->setThemeManager($themeManager);
+ }
+
+ public function getPassword()
+ {
+ return $this->_pass;
+ }
+
+ public function setPassword($_pass)
+ {
+ $this->_pass = $_pass;
+ }
+
+}
\ No newline at end of file
diff --git a/framework/Wsat/pages/TWsatGenerateAR.page b/framework/Wsat/pages/TWsatGenerateAR.page
new file mode 100644
index 00000000..926d6b0f
--- /dev/null
+++ b/framework/Wsat/pages/TWsatGenerateAR.page
@@ -0,0 +1,56 @@
+
+ |
+ + Enables you to generate all Active Record Classes with relations included. + | +