From b0c2fecd42bd8b95f2535a525b035f27aebde287 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 17 Nov 2013 10:17:44 -0800 Subject: WSAT, similar to Microsoft - Web Site Administration Tool and Yii's Gii, will allow as to generate a lot code such as AR classes with the proper relationships... scaffolding... as well as to admin the app configuration and pages configurations in a GUI fashion. --- framework/Wsat/TWsatARGenerator.php | 136 ++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 framework/Wsat/TWsatARGenerator.php (limited to 'framework/Wsat/TWsatARGenerator.php') diff --git a/framework/Wsat/TWsatARGenerator.php b/framework/Wsat/TWsatARGenerator.php new file mode 100644 index 00000000..7b39fce1 --- /dev/null +++ b/framework/Wsat/TWsatARGenerator.php @@ -0,0 +1,136 @@ +_ar_manager = TActiveRecordManager::getInstance(); + $this->_opnamespace = $_opnamespace; + } + + // + public function generate($tableName, $clasName = '') { + $conn = $this->_ar_manager->getDbConnection(); + $gateway = $this->_ar_manager->getRecordGateway(); + $tableInfo = $gateway->getTableInfo($conn, $tableName); + + if (count($tableInfo->getColumns()) === 0) { + throw new TIOException("Unable to find table or view $tableName in " . $conn->getConnectionString() . "."); + } else { + $properties = array(); + foreach ($tableInfo->getColumns() as $field => $column) + $properties[] = $this->generateProperty($field, $column); + } + + $clasName = empty($clasName) ? $this->_getClassName($tableName) : $clasName; + $class = $this->generateClass($properties, $tableName, $clasName); + $op_file = Prado::getPathOfNamespace($this->_opnamespace); + if (!is_dir($op_file)) { + mkdir($op_file, 0777, true); + } + + $output = $op_file . DIRECTORY_SEPARATOR . $clasName . ".php"; + file_put_contents($output, $class); + } + + public function generateAll() { + foreach ($this->_getAllTableNames() as $tableName) { + $this->generate($tableName); + } + } + +// +//----------------------------------------------------------------------------- + // + private function getDbConnection() { + $con = $this->_ar_manager->getDbConnection(); + $con->Active = true; + return $con; + } + + private function _getAllTableNames() { + $con = $this->getDbConnection(); + $command = $con->createCommand("Show Tables"); + $dataReader = $command->query(); + $dataReader->bindColumn(1, $table); + $tables = array(); + while ($dataReader->read()) { + $tables[] = $table; + } + $con->setActive(false); + return $tables; + } + + private function _getClassName($tableName) { + return ucfirst($tableName); + } + + public function renderAllTablesInformation() { + $conn = $this->_ar_manager->getDbConnection(); + $gateway = $this->_ar_manager->getRecordGateway(); + + foreach ($this->_getAllTableNames() as $table_name) { + echo $table_name . "
"; + + $tableInfo = $gateway->getTableInfo($conn, $table_name); + echo "Table info:" . "
"; + echo "
";
+            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; + } + + protected function generateClass($properties, $tablename, $class) { + $props = implode("\n", $properties); + $date = date('Y-m-d h:i:s'); + return << +EOD; + } + +//
+} + +?> -- cgit v1.2.3