From 703a7d5bbdb73ad38d5f1f56808cbd68fcb0d292 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Thu, 17 Apr 2014 22:05:44 -0500 Subject: [PATCH] First commit --- .htaccess | 2 + index.php | 10 ++++ system/engine/core.php | 63 ++++++++++++++++++++ system/vendor/DB.php | 129 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 .htaccess create mode 100644 index.php create mode 100644 system/engine/core.php create mode 100644 system/vendor/DB.php diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..6571402 --- /dev/null +++ b/.htaccess @@ -0,0 +1,2 @@ +RewriteEngine on +RewriteRule ^(.*)$ /index.php/$1 [L] \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..be3ecd8 --- /dev/null +++ b/index.php @@ -0,0 +1,10 @@ +run(); \ No newline at end of file diff --git a/system/engine/core.php b/system/engine/core.php new file mode 100644 index 0000000..23a1a66 --- /dev/null +++ b/system/engine/core.php @@ -0,0 +1,63 @@ +findController(); + } + + private function findController() + { + $request = $_SERVER["REQUEST_URI"]; + if ($request == "" || $request == "/") + { + require("../../application/controllers/main.php"); + $this->$class = new main(); + $this->$method = "index"; + $this->$classname = "main"; + return; + } + $arr = explode("/", $request); + $path = "../../application/controllers/"; + for($i = 0; $i < count($arr); $i++) + { + if ($is_file($path . $arr[$i] . ".php")) // found the controller + { + include($path . $arr[$i] . ".php"); + $this->$class = new $arr[$i]; + + if ($i + 1 != count($arr)) // if there is a define after the controller name - this would be the method name + { + $this->$method = $arr[$i+1]; + $this->$args = array_slice ($arr, 2); + $this->$classname = $arr[$i]; + } else { // call index + $this->$method = "index"; + $this->$classname = $arr[$i]; + } + return; + } + + if (is_dir($path . $arr[$i])) // controller is hidden deeper + { + $path = $path . $arr[$i] . "/"; + continue; + } + + // throw exception controller not found + } + + } + + public function run() + { + $call = new ReflectionFunction($this->$classname, $this->$method); + $call->invokeArgs($this->$class, $this->$args); + } +} \ No newline at end of file diff --git a/system/vendor/DB.php b/system/vendor/DB.php new file mode 100644 index 0000000..e28d4f7 --- /dev/null +++ b/system/vendor/DB.php @@ -0,0 +1,129 @@ + + ********************************** 80 Columns ********************************* + */ +class DB +{ + static $q,$c,$p,$i = '`'; + + /** + * Fetch a column offset from the result set (COUNT() queries) + * + * @param string $query query string + * @param array $params query parameters + * @param integer $key index of column offset + * @return array|null + */ + static function column($query, $params = NULL, $key = 0) + { + if($statement = DB::query($query, $params)) + return $statement->fetchColumn($key); + } + + /** + * Fetch a single query result row + * + * @param string $query query string + * @param array $params query parameters + * @return mixed + */ + static function row($query, $params = NULL) + { + if($statement = DB::query($query, $params)) + return $statement->fetch(); + } + + /** + * Fetches an associative array of all rows as key-value pairs (first + * column is the key, second column is the value). + * + * @param string $query query string + * @param array $params query parameters + * @return array + */ + static function pairs($query, $params = NULL) + { + $data = array(); + + if($statement = DB::query($query, $params)) + while($row = $statement->fetch(\PDO::FETCH_NUM)) + $data[$row[0]] = $row[1]; + + return $data; + } + + /** + * Fetch all query result rows + * + * @param string $query query string + * @param array $params query parameters + * @param int $column the optional column to return + * @return array + */ + static function fetch($query, $params = NULL, $column = NULL) + { + if( ! $statement = DB::query($query, $params)) return; + + // Return an array of records + if($column === NULL) return $statement->fetchAll(); + + // Fetch a certain column from all rows + return $statement->fetchAll(\PDO::FETCH_COLUMN, $column); + } + + /** + * Prepare and send a query returning the PDOStatement + * + * @param string $query query string + * @param array $params query parameters + * @return object|null + */ + static function query($query, $params = NULL) + { + $statement = static::$c->prepare(DB::$q[] = strtr($query, '`', DB::$i)); + $statement->execute($params); + return $statement; + } + + /** + * Insert a row into the database + * + * @param string $table name + * @param array $data + * @return integer|null + */ + static function insert($table, array $data) + { + $query = "INSERT INTO`$table`(`" . implode('`,`', array_keys($data)) + . '`)VALUES(' . rtrim(str_repeat('?,', count($data = array_values($data))), ',') . ')'; + return DB::$p + ? DB::column($query . 'RETURNING`id`', $data) + : (DB::query($query, $data) ? static::$c->lastInsertId() : NULL); + } + + /** + * Update a database row + * + * @param string $table name + * @param array $data + * @param array $w where conditions + * @return integer|null + */ + static function update($table, $data, $value, $column = 'id') + { + $keys = implode('`=?,`', array_keys($data)); + if($statement = DB::query( + "UPDATE`$table`SET`$keys`=? WHERE`$column`=?", + array_values($data + array($value)) + )) + return $statement->rowCount(); + } +} \ No newline at end of file