Permalink
Browse files

added config, cache, dir classes

  • Loading branch information...
aaroniker committed Jan 15, 2018
1 parent 3a1a311 commit 8004e160b7b99e8ddcb711063316b65c853906c4
Showing with 200 additions and 0 deletions.
  1. +78 −0 gear/classes/cache.php
  2. +69 −0 gear/classes/config.php
  3. +53 −0 gear/classes/dir.php
  4. 0 tmp/cache/.gitkeep
@@ -0,0 +1,78 @@
<?php
class cache {
static protected $cache = false;
static protected $time = 100;
static public function setCache($bool) {
self::$cache = (bool)$bool;
}
static public function getFileName($id, $table) {
return md5($id.$table).'.cache';
}
static public function deleteFile($file) {
return unlink(dir::cache($file));
}
static public function exist($file, $time = false) {
if($time === false) {
$time = self::$time;
}
if(file_exists(dir::cache($file))) {
if((time() - filemtime(dir::cache($file))) >= $time) {
self::deleteFile($file);
clearstatcache();
return false;
}
clearstatcache();
return true;
}
return false;
}
static public function write($content, $file) {
if(self::$cache === true) {
if(!file_put_contents(dir::cache($file), $content, LOCK_EX)) {
return false;
}
}
return true;
}
static public function read($file) {
return file_get_contents(dir::cache($file));
}
static public function clear($folder = '') {
if($dir = opendir(dir::cache($folder))) {
while (($file = readdir($dir)) !== false) {
if(is_file($file)) {
self::deleteFile($file);
}
}
closedir($dir);
}
}
}
?>
@@ -0,0 +1,69 @@
<?php
class config {
protected $params = [];
protected $file = '';
protected $fileParams = [];
protected $isChange = false;
protected $newEntrys = [];
public function __construct($file) {
$this->file = $file;
$this->params = json_decode(file_get_contents($this->file), true);
$this->fileParams = $this->params;
}
public function has($name) {
return isset($this->params[$name]) || array_key_exists($name, $this->params);
}
public function get($name, $default = null) {
if($this->has($name)) {
return $this->params[$name];
}
return $default;
}
public function add($name, $value, $toSave = false) {
$this->params[$name] = $value;
if($toSave) {
$this->isChange = true;
$this->newEntrys[$name] = $value;
}
}
public function save() {
if(!$this->isChange) {
return true;
}
$newEntrys = array_merge($this->fileParams, $this->newEntrys);
return file_put_contents($this->file, json_encode($newEntrys, JSON_PRETTY_PRINT));
}
public static function getMySQLVersion() {
return sql::run(true)->query('select version()')->fetchColumn();
}
public static function getPHPVersion() {
return PHP_VERSION;
}
}
?>
@@ -0,0 +1,53 @@
<?php
class dir {
static $base = '';
public function __construct($dir = '') {
self::$base = realpath($dir);
}
public static function base($file = '') {
return self::$base.DIRECTORY_SEPARATOR.$file;
}
public static function gear($file = '') {
return self::base('gear'.DIRECTORY_SEPARATOR.$file);
}
public static function classes($file = '') {
return self::gear('classes'.DIRECTORY_SEPARATOR.$file);
}
public static function vendor($file = '') {
return self::gear('vendor'.DIRECTORY_SEPARATOR.$file);
}
public static function tmp($file = '') {
return self::base('tmp'.DIRECTORY_SEPARATOR.$file);
}
public static function cache($file = '') {
return self::tmp('cache'.DIRECTORY_SEPARATOR.$file);
}
public static function extensions($extension = false, $file = '') {
if($extension) {
return self::base('extensions'.DIRECTORY_SEPARATOR.$extension.DIRECTORY_SEPARATOR.$file);
} else {
return self::base('extensions'.DIRECTORY_SEPARATOR.$file);
}
}
public static function themes($theme = false, $file = '') {
if($theme) {
return self::base('themes'.DIRECTORY_SEPARATOR.$theme.DIRECTORY_SEPARATOR.$file);
} else {
return self::base('themes'.DIRECTORY_SEPARATOR.$file);
}
}
}
?>
No changes.

0 comments on commit 8004e16

Please sign in to comment.