summaryrefslogtreecommitdiff
path: root/framework/Wsat/TWsatARGenerator.php
blob: 7b39fce14945b1d18204d6218b95cbfad30525b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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>
}

?>