diff options
author | Daniel <darthdaniel85@gmail.com> | 2013-11-17 10:17:44 -0800 |
---|---|---|
committer | Daniel <darthdaniel85@gmail.com> | 2013-11-17 10:17:44 -0800 |
commit | b0c2fecd42bd8b95f2535a525b035f27aebde287 (patch) | |
tree | 9a709073895f4f52713ed0e83241278c4f6f6e57 /framework/Wsat/TWsatARGenerator.php | |
parent | 4cdac46fcc8411607352defeb962602f93a5b26b (diff) |
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.
Diffstat (limited to 'framework/Wsat/TWsatARGenerator.php')
-rw-r--r-- | framework/Wsat/TWsatARGenerator.php | 136 |
1 files changed, 136 insertions, 0 deletions
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 @@ +<?php
+
+/**
+ * Description of TWsatARGenerator
+ *
+ * @author Daniel
+ */
+class TWsatARGenerator {
+
+ /**
+ * @return TActiveRecordManager static instance of record manager.
+ */
+ private $_ar_manager;
+
+ /**
+ * Output folder (by namespace) where AR classes will be generated.
+ */
+ private $_opnamespace;
+
+ function __construct($_opnamespace = "Application.App_Data.AR_Classes") {
+ $this->_ar_manager = TActiveRecordManager::getInstance();
+ $this->_opnamespace = $_opnamespace;
+ }
+
+ // <editor-fold defaultstate="collapsed" desc="Main APIs">
+ 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);
+ }
+ }
+
+// </editor-fold>
+//-----------------------------------------------------------------------------
+ // <editor-fold defaultstate="collapsed" desc="Common Methods">
+ 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 . "<br>";
+
+ $tableInfo = $gateway->getTableInfo($conn, $table_name);
+ echo "Table info:" . "<br>";
+ echo "<pre>";
+ var_dump($tableInfo);
+ echo "</pre>";
+ }
+ }
+
+//-----------------------------------------------------------------------------
+
+ 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
+<?php
+/**
+ * Auto generated by prado-cli.php on $date.
+ */
+class $class extends TActiveRecord
+{
+ const TABLE='$tablename';
+
+$props
+
+ public static function finder(\$className=__CLASS__)
+ {
+ return parent::finder(\$className);
+ }
+}
+?>
+EOD;
+ }
+
+// </editor-fold>
+}
+
+?>
|