phergie /Phergie/Plugin/Db.php

Language PHP Lines 234
MD5 Hash 383f62d0e4aa045a9072d6f2f7c4c031
Repository https://github.com/markizano/phergie.git View Raw File View Project SPDX
  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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * Phergie
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie_Plugin_Db
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie_Plugin_Db
 */

/**
 * TODO CREATE CLASS DESCRIPTION
 *
 * @category Phergie
 * @package  Phergie_Plugin_Db
 * @author   Jared Folkins <jfolkins@gmail.com>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie_Plugin_Db
 * @uses     Phergie_Plugin_UserInfo pear.phergie.org
 */

class Phergie_Plugin_Db extends Phergie_Plugin_Abstract
{
    const DEBUG = true;

    /**
     *  Checks to see if the root rbac user has been set in     *  
     *
     *  @return void
     */
    public function onLoad()
    {
        $this->doesPdoExist();        
    }

    /**
     *  Validates that the Pdo Extenstion Exists.
     *
     *  @return void
     */
    private function doesPdoExist()
    {
        if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
            $this->fail('PDO and pdo_sqlite extensions must be installed');
        }
    }
    
    /**
     *  Initializes database
     *
     *  @param string $directory  plugin name
     *  @param string $dbFile     database name
     *  @param string $schemaFile schema filename
     *
     *  @return object
     */
    public function init($directory, $dbFile, $schemaFile)
    {
        $this->isResourceDirectory($directory);        
        $doesDbFileExist = is_readable($dbFile);

        try {
            $db = new PDO('sqlite:' . $dbFile);
        } catch (PDO_Exception $e) {
            throw new Phergie_Plugin_Exception($e->getMessage());
        }

        if (!$doesDbFileExist) {
            $this->createTablesFromSchema($db, $schemaFile);
        }
        return $db;
    }

    /**
     * fully specified file name as string
     * used to check that the resource directory does exist
     * 
     * @param string $directory Directory to check
     *
     * @return void
     *
     */
    public function isResourceDirectory($directory)
    {
        if (!is_dir($directory)) {
            $this->fail('The Resource directory: ' . $directory . ' does not exist');
        }
    }

    /**
     * fully specified file name as string
     * used to check that the schema file does exist
     * 
     * @param string $file File to check
     *
     * @return void
     * 
     */
    public function isSchemaFile($file)
    {
        if (!is_readable($file)) {
            $this->fail(
                'The schema file: ' . $file
                . ' is not readable or does not exist'
            );
        }
    }

    /**
     * Supply sql statement and one word type parameter
     *  (IE, create, update, insert, delete)
     * and the method will validate that the sql contains that syntax
     *
     * @param string $sql  TODO Desc
     * @param string $type TODO Desc
     *
     * @return bool
     */
    public function validateSqlType($sql, $type)
    {        
        preg_match('/^'.strtolower($type).'/', strtolower($sql), $matches);
        return ($matches[0]) ? true : false;
    }
    
    /**
     *  Creates database table
     *
     *  @param String $db  database reference
     *  @param String $sql create table sql statement
     *
     *  @return void
     */
    public function createTable($db, $sql)
    {
        if (!$this->validateSqlType($sql, 'create')) {
            $this->fail('The SQL provided is not a create statement');
        }

        try {
            $db->exec($sql);
        } catch (PDO_Exception $e) {
            throw new Phergie_Plugin_Exception($e->getMessage());
        }
    }

    /**
     * Loads the schema file into array then searches
     * for each table and if not found creates the table
     *
     * @param String $db   Database Table
     * @param String $file Filename
     *
     * @return void
     */
    public function createTablesFromSchema($db, $file)
    {
        $this->isSchemaFile($file);
        $file = strtolower(file_get_contents($file));
        preg_match_all('/create\stable\s([a-z_]+).*;/', $file, $matches);

        if (count($matches[0]) != count($matches[1])) {
            $this->fail(
                'Schema array key value mismatch, '
                . 'the regular expression must not be working correctly'
            );
        }

        $tables = array_combine($matches[1], $matches[0]);

        foreach ($tables as $name => $sql) {
            if (!$this->hasTable($db, $name)) {
                $this->createTable($db, $sql);
            }
        }

    }
    
    /**
     *  Validates that database table exists
     *
     *  @param String $db   database name
     *  @param String $name table name
     *
     *  @return bool
     */
    public function hasTable($db, $name)
    {
        $sql = 'SELECT COUNT(*)
            FROM sqlite_master
            WHERE name = :tableName';

        $statement = $db->prepare($sql);
        $statement->execute(array(':tableName' => $db->quote($name)));
        return (bool) $statement->fetchColumn();
    }

    /**
     *  TODO Desc
     *
     *  @param String $name table name
     *
     *  @return bool
     */
    public function dropTable($name)
    {
    }
   
    /**
     * Jared's crap debug method
     *
     * @param String $message TODO Desc
     *
     * @return void
     */
    private function debug($message)
    {
        if (self::DEBUG) {
            echo 'DEBUG: ['. date('c') . '] - '. $message . "\n";
        }
    }
}
?>
Back to Top