Skip to content

Commit

Permalink
Optimization, and start of Database Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanNaddy authored and RyanNaddy committed Jul 15, 2013
1 parent 24434c4 commit 2537f12
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 30 deletions.
7 changes: 7 additions & 0 deletions examples/database/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once '../../phpLive.php';

$live->db->select("select * from users where fname in(?,?)", array("Ryan", "Jaimee"))->each(function($col, $name){
echo "here";
});
10 changes: 5 additions & 5 deletions extensions/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 32 additions & 24 deletions phpLive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
96 changes: 96 additions & 0 deletions plugins/Database/Database.plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
define("PDO_DATABASE", "mysql");
define("PDO_DBNAME", "test");
define("PDO_HOST", "localhost");
define("PDO_USER", "root");
define("PDO_PASSWD", "afrid123");
*/

/**
* @property PDO $db PDO Class
* @property PDOStatement $sql PDO Statement
*/
class Database extends phpLive{

private $db = null,
$dbtype = null,
$database = null,
$hostname = null,
$username = null,
$password = null

;

public function __construct($data){
$this->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();
}

}
8 changes: 7 additions & 1 deletion plugins/plugins.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
root = TwitterAPI
fileName = Twitter.plugin.php
instanceName = twitter
sessionRef = twitter
sessionRef = twitter

[Database]
root = Database
fileName = Database.plugin.php
instanceName = db
sessionRef = db
23 changes: 23 additions & 0 deletions plugins/plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$plugins = array(
"Twitter" => 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",
)
),
);

0 comments on commit 2537f12

Please sign in to comment.