Created
October 7, 2013 15:13
-
-
Save iamacarpet/6869643 to your computer and use it in GitHub Desktop.
FuelPHP Database Config Driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fuel\Core; | |
/** | |
* JSON Config file parser | |
*/ | |
class Config_Db extends \Config_File | |
{ | |
protected $ext = '.db'; | |
/** | |
* Loads in the given "file" from the DB and parses it. | |
* | |
* @param string $file File to load | |
* @return array | |
*/ | |
protected function load_file($file){ | |
if (!self::checkdb()){ | |
die('Unable to Contact Database'); | |
return false; | |
} | |
$result = \DB::select('data')->from('site_config')->where('path', $file)->as_assoc()->execute(); | |
if (count($result) > 0){ | |
$contents = $this->parse_vars($result[0]['data']); | |
return json_decode($contents, true); | |
} else { | |
\Log::error('0 Config DB Results for ' . $file . ' - ' . var_export($result, true)); | |
return array(); | |
} | |
} | |
protected function find_file($cache = true){ | |
return array($this->file); | |
} | |
/** | |
* Returns the formatted config file contents. | |
* | |
* @param array $contents config array | |
* @return string formatted config file contents | |
*/ | |
protected function export_format($contents){ | |
$this->prep_vars($contents); | |
return \Format::forge()->to_json($contents, true); | |
} | |
/** | |
* Take over the "save" function of the parent class | |
* so we save to the DB rather than file. | |
* | |
* @param string $identifier "file" name / path | |
* @param array $contents config file contents | |
* @return null | |
*/ | |
public function save($identifier, $contents){ | |
// get the formatted output | |
$output = $this->export_format($contents); | |
if (!$output){ | |
return false; | |
} | |
$file = str_replace('.db', '', $identifier); | |
$path = \DB::escape($file); | |
$data = \DB::escape($output); | |
$oldID = self::pathExists($file); | |
if ($oldID > 0){ | |
$sql = "UPDATE site_config SET path = " . $path . ", data = " . $data . " WHERE ID = " . \DB::escape($oldID) . ";"; | |
} else { | |
$sql = "INSERT INTO site_config (path, data) VALUES ( " . $path . ", " . $data . " );"; | |
} | |
//\Log::error($sql); | |
$result = \DB::query($sql)->execute(); | |
} | |
private function pathExists($file){ | |
$result = \DB::select('id')->from('site_config')->where('path', $file)->as_assoc()->execute(); | |
if (count($result) > 0){ | |
return $result[0]['id']; | |
} else { | |
return false; | |
} | |
} | |
/* Check we've got database connectivity */ | |
/* CREATE TABLE site_config ( | |
ID int PRIMARY KEY AUTO_INCREMENT NOT NULL, | |
path varchar(500) NOT NULL, | |
data LONGTEXT NOT NULL, | |
UNIQUE (path) | |
) engine = innodb; | |
*/ | |
private function checkdb(){ | |
if (\DBUtil::table_exists('site_config')){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment