From 2537f12c19647062b769be5cc64fabb81bffa3b4 Mon Sep 17 00:00:00 2001 From: RyanNaddy Date: Mon, 15 Jul 2013 17:18:14 -0500 Subject: [PATCH] Optimization, and start of Database Plugin --- examples/database/test.php | 7 ++ extensions/example.php | 10 +-- phpLive.php | 56 +++++++++------- plugins/Database/Database.plugin.php | 96 ++++++++++++++++++++++++++++ plugins/plugins.ini | 8 ++- plugins/plugins.php | 23 +++++++ 6 files changed, 170 insertions(+), 30 deletions(-) create mode 100644 examples/database/test.php create mode 100644 plugins/Database/Database.plugin.php create mode 100644 plugins/plugins.php diff --git a/examples/database/test.php b/examples/database/test.php new file mode 100644 index 0000000..f5bed85 --- /dev/null +++ b/examples/database/test.php @@ -0,0 +1,7 @@ +db->select("select * from users where fname in(?,?)", array("Ryan", "Jaimee"))->each(function($col, $name){ + echo "here"; +}); \ No newline at end of file diff --git a/extensions/example.php b/extensions/example.php index a56b5ae..ccfc390 100644 --- a/extensions/example.php +++ b/extensions/example.php @@ -2,21 +2,21 @@ $live->animal = ""; $live->favoriteAnimal = function($animal = null) use ($live){ if($animal == null) - $animal = $live->quickString; + $animal = $live->string; $live->animal = $animal; - $live->quickString = "Your favorite animal is a $live->animal."; + $live->string = "Your favorite animal is a $live->animal."; return $live; }; $live->animalNoise = function() use ($live){ switch($live->animal){ case "dog": - $live->quickString = "The {$live->animal} says Bark!"; + $live->string = "The {$live->animal} says Bark!"; break; case "cat": - $live->quickString = "The {$live->animal} says Meow!"; + $live->string = "The {$live->animal} says Meow!"; break; default: - $live->quickString = "$live->animal unknown sound."; + $live->string = "$live->animal unknown sound."; break; } return $live; diff --git a/phpLive.php b/phpLive.php index b41166b..4347c9f 100644 --- a/phpLive.php +++ b/phpLive.php @@ -106,21 +106,21 @@ class phpLive{ protected $tmpFile = ""; // Private Read-Only Properties private $url, $ch, $links, $cleanData, $info, $title, $endingUrl, $httpCode, $loadTime; - private $processing = false, $urlQuery; - private $extension = array(); + private $processing = false, $urlQuery; + private $extension = array(); // Public Properties - public $colors = array("#ffffff", "#eeeeee"); - public $coreLoaded = false; + public $colors = array("#ffffff", "#eeeeee"); + public $coreLoaded = false; public $thumbDir; public $content; - public $db = array(); - public $dbHostname, $dbUsername, $dbPassword, $dbDatabase, $dbPort, $dbResult, $dbRow, $dbQueries = 0; - public $port = 80; - public $host = 'localhost'; - public $list = array(); - public $post = array(); - public $functionName = null; - public $string = ""; + public $db = array(); + public $dbHostname, $dbUsername, $dbPassword, $dbDatabase, $dbPort, $dbResult, $dbRow, $dbQueries = 0; + public $port = 80; + public $host = 'localhost'; + public $list = array(); + public $post = array(); + public $functionName = null; + public $string = ""; public function __construct(){ $this->location = dirname(__FILE__); @@ -341,17 +341,19 @@ public function numMethods($class){ * $this->$instance = new $class(); */ public function loadPlugin($class, $info){ - $info = (object)$info; - $file = $this->location . "/plugins/" . $info->root . "/" . $info->fileName; + $this->functionName = __FUNCTION__; + $info = (object)$info; + $file = $this->location . "/plugins/" . $info->root . "/" . $info->fileName; if(is_file($file)){ require_once $file; $instance = (string)$info->instanceName; - $this->$instance = new $class(); - $this->functionName = __FUNCTION__; + $info = (isset($info->information)) ? $info->information : ""; + //$this->$instance = new $class($info); + $reflection = new ReflectionClass($class); + $this->$instance = $reflection->newInstanceArgs(array($info)); $this->extension[$instance] = $this->$instance; return $this->$instance; } - $this->functionName = __FUNCTION__; return false; } @@ -401,9 +403,13 @@ public function allPluginSettings(){ $this->functionName = __FUNCTION__; if(empty($this->location)) $this->location = dirname(__FILE__); - $file = $this->location . "/plugins/plugins.ini"; - if(is_file($file)) - return parse_ini_file($file, true); + + $file = $this->location . "/plugins/plugins.php"; + if(is_file($file)){ + //return parse_ini_file($file, true); + require_once $file; + return $plugins; + } else return false; } @@ -1369,12 +1375,14 @@ public function imageResize($save_name, $filename = null, $thumbWidth = 200, $qu public function imageTo($type, $filename, $new_filename = null){ if(!is_file($filename)) return $this; - $image = file_get_contents($filename); - $img = imagecreatefromstring($image); + $image = file_get_contents($filename); + $img = imagecreatefromstring($image); + if($new_filename == null) - $pi = (object)pathinfo($filename); + $pi = (object)pathinfo($filename); else - $pi = (object)pathinfo($new_filename); + $pi = (object)pathinfo($new_filename); + $new_filename = $pi->dirname . "/" . $pi->filename; switch($type){ case IMAGE_PNG: diff --git a/plugins/Database/Database.plugin.php b/plugins/Database/Database.plugin.php new file mode 100644 index 0000000..d1646f6 --- /dev/null +++ b/plugins/Database/Database.plugin.php @@ -0,0 +1,96 @@ +dbtype = $data["dbtype"]; + $this->database = $data["database"]; + $this->hostname = $data["hostname"]; + $this->username = $data["username"]; + $this->password = $data["password"]; + parent::__construct(); + } + + public function connect(){ + $this->db = new PDO("$this->dbtype:dbname=$this->database;host=$this->hostname;", $this->username, $this->password); + } + + private function isConnected(){ + if($this->db === null){ + $this->connect(); + } + } + + private function query($query, $args){ + $this->isConnected(); + $this->sql = $this->db->prepare($query, $args); + $this->sql->execute($args); + } + + private function queryinfo($args){ + $query = array_shift($args); + if(isset($args[0]) && is_array($args[0])){ + $args = $args[0]; + } + return (object)array("query" => $query, "args" => $args); + } + + public function select(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this; + return $this->sql->fetchAll(); + } + + public function insert(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this->db->lastInsertId(); + } + + public function update(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this->sql->rowCount(); + } + + public function delete(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this->sql->rowCount(); + } + + public function getFirst(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this->sql->fetchColumn(); + } + + public function getEntry(){ + $info = $this->queryinfo(func_get_args()); + $this->query($info->query, $info->args); + return $this->sql->fetch(); + } + +} \ No newline at end of file diff --git a/plugins/plugins.ini b/plugins/plugins.ini index 05e9eeb..d893c63 100644 --- a/plugins/plugins.ini +++ b/plugins/plugins.ini @@ -2,4 +2,10 @@ root = TwitterAPI fileName = Twitter.plugin.php instanceName = twitter -sessionRef = twitter \ No newline at end of file +sessionRef = twitter + +[Database] +root = Database +fileName = Database.plugin.php +instanceName = db +sessionRef = db \ No newline at end of file diff --git a/plugins/plugins.php b/plugins/plugins.php new file mode 100644 index 0000000..9a13e45 --- /dev/null +++ b/plugins/plugins.php @@ -0,0 +1,23 @@ + array( + "root" => "TwitterAPI", + "fileName" => "Twitter.plugin.php", + "instanceName" => "twitter", + "sessionRef" => "twitter", + ), + "Database" => array( + "root" => "Database", + "fileName" => "Database.plugin.php", + "instanceName" => "db", + "sessionRef" => "db", + "information" => array( + "dbtype" => "mysql", + "hostname" => "localhost", + "database" => "test", + "username" => "root", + "password" => "afrid123", + ) + ), +); \ No newline at end of file