@liuximing
2017-12-15T15:52:15.000000Z
字数 9052
阅读 112
PHP
Siaf: Siaf is another framework.
Siaf
<?php/*** 入口文件* 1.定义常量* 2.加载函数库* 3.自动加载类库* 4.启动框架*///1.定义常量define('ROOT', dirname(__FILE__));define('CORE', ROOT.'/core');define('APP', ROOT.'/app');//2.加载函数库include CORE.'/common/function.php';process(ROOT);include CORE.'/base.php';//3.自动加载类库spl_autoload_register('\core\base::load');//4.启动框架\core\base::run();
自动加载类库方法、启动方法(路由和加载控制器)
<?phpnamespace core;class base{static public $classMap = array();static public function load($class){/*** 自动加载类库* new \core\route();* $class = 'core\route()';* ROOT.'/'.'core/route.php';*/if (isset($classMap[$class])) {return true;} else {$temp = str_replace('\\', '/', $class);$file = ROOT.'/'.$temp.'.php';if(is_file($file)) {include $file;self::$classMap[$class] = $class;} else {return false;}}}static public function run(){//process('siaf is running...');\core\lib\route::routing();$controllerClass = \core\lib\route::controller . 'Controller';$action = \core\lib\route::action;$controllerFile = APP . '/controller/' . $controllerClass . '.php';if (is_file($controllerFile)) {include $controllerFile;$controllerClass = '\app\controller\\' . $controllerClass;$controller = new $controllerClass();$controller->$action();} else {throw new \Exception('找不到控制器' . $controllerClass);}//process('siaf is stop');}}
<?phpnamespace core\lib;class route{static public $controller = 'index';static public $action = 'index';static public function routing(){//process('route start...');//xxx.com/controller/action/*** 1.隐藏index.php:* xxx.com/index.php/controller/action* -> xxx.com/controller/action* 2.获取URL参数部分* 3.返回对应的控制器和方法*///process($_SERVER);if (isset($_SERVER['REQUEST_URI'])) {$path = explode('?', $_SERVER['REQUEST_URI'])[0];if ($path != '/') {$pathArray = explode('/', trim($path, '/'));if (isset($pathArray[2])) {$this->controller = $pathArray[2];}if (isset($pathArray[3])) {$this->action = $pathArray[3];}}}//process('route end');}}
<?phpnamespace core\lib;class config{static public $configCache = array();static public function getConfig($selector, $item=''){switch ($selector) {case 'php':return self::getFromPhp($item);case 'ini':return self::getFromIni($item);case 'xml':return self::getFromXml($item);case 'json':return self::getFromJson($item);}}static protected function getFromPhp($item=''){if (empty(self::$configCache)) {$configFile = ROOT . '/core/config/config.php';if (is_file($configFile)) {self::$configCache = include $configFile;//process(self::$configCache);exit();} else {throw new \Exception('找不到配置文件');}}if (empty($item)) {return self::$configCache;} else {if (isset(self::$configCache[$item])) {return self::$configCache[$item];} else {throw new \Exception('找不到配置项' . $item);}}}static protected function getFromIni($item=''){if (empty(self::$configCache)) {$configFile = ROOT . '/core/config/config.ini';if (is_file($configFile)) {self::$configCache = parse_ini_file($configFile, true);//process(self::$configCache);exit();} else {throw new \Exception('找不到配置文件');}}if (empty($item)) {return self::$configCache;} else {if (isset(self::$configCache[$item])) {return self::$configCache[$item];} else {throw new \Exception('找不到配置项' . $item);}}}static protected function getFromXml($item=''){if (empty(self::$configCache)) {$configFile = ROOT . '/core/config/config.xml';if (is_file($configFile)) {self::$configCache = simplexml_load_file($configFile);self::$configCache = self::objectToArray(self::$configCache);//process(self::$configCache);exit();} else {throw new \Exception('找不到配置文件');}}if (empty($item)) {return self::$configCache;} else {if (isset(self::$configCache[$item])) {return self::$configCache[$item];} else {throw new \Exception('找不到配置项' . $item);}}}static protected function getFromJson($item=''){if (empty(self::$configCache)) {$configFile = ROOT . '/core/config/config.json';if (is_file($configFile)) {self::$configCache = file_get_contents($configFile);self::$configCache = json_decode(self::$configCache);self::$configCache = self::objectToArray(self::$configCache);//process(self::$configCache);exit();} else {throw new \Exception('找不到配置文件');}}if (empty($item)) {return self::$configCache;} else {if (isset(self::$configCache[$item])) {return self::$configCache[$item];} else {throw new \Exception('找不到配置项' . $item);}}}static protected function objectToArray($obj) {$_arr = is_object($obj) ? get_object_vars($obj) : $obj;foreach ($_arr as $key => $val){$val = is_object($val) ? self::objectToArray($val) : $val;$arr[$key] = $val;}return $arr;}}
<?phpnamespace core\lib;use core\lib\config;class log{static public $config = '';static public $path = '';static public function init(){self::$config = config::getConfig('ini', 'log');if (isset(self::$config['path'])) {self::$path = self::$config['path'];}}static public function writeLog($content, $type){date_default_timezone_set('Asia/Shanghai');if (!is_dir(self::$path . date('Ymd'))) {mkdir(self::$path . date('Ymd'), '0777', true);}$content = date('Y-m-d H:i:s') . ' | ' . json_encode($content) . PHP_EOL;return file_put_contents(self::$path . date('Ymd') . '/' . $type . '.php', $content, FILE_APPEND);}}
<?phpnamespace core\lib;use core\lib\config;class model extends \PDO{public $config = '';public $dsn = '';public $username = '';public $passwd = '';public $database = '';public function __construct(){$this->config = config::getConfig('ini', 'database');if (isset($this->config['dsn'])) {$this->dsn = $this->config['dsn'];}if (isset($this->config['username'])) {$this->username = $this->config['username'];}if (isset($this->config['passwd'])) {$this->passwd = $this->config['passwd'];}}public function connect(){try {$this->database = new \PDO($this->dsn, $this->username, $this->passwd);} catch (\PDOException $e) {process('Connection failed: ' . $e->getMessage());}}public function insert($table, $data){$keyStr = '(';$valueStr = '(';foreach ($data as $key => $value) {$keyStr .= $key . ',';$valueStr .= '\'' . $value . '\'' . ',';}$keyStr = substr_replace($keyStr, ')', -1);$valueStr = substr_replace($valueStr, ')', -1);$sql = 'INSERT INTO ' . $table . ' ' . $keyStr . ' VALUES ' . $valueStr;//process($sql);exit();$count = $this->database->exec($sql);if ($count === 'false') {process('Insert failed: ' . $this->database->errorInfo());} else {//process('Inserted ' . $count . ' rows.');}}public function delete($table, $where){$sql = 'DELETE FROM ' . $table . ' WHERE ' . $where;//process($sql);exit();$count = $this->database->exec($sql);if ($count === 'false') {process('Delete failed: ' . $this->database->errorInfo());} else {//process('Deleted ' . $count . ' rows.');}}public function update($table, $data, $where){$dataStr = '';foreach ($data as $key => $value) {$dataStr .= $key . '=' . '\'' . $value . '\',';}$dataStr = substr_replace($dataStr, '', -1);$sql = 'UPDATE ' . $table . ' SET ' . $dataStr . ' WHERE ' . $where;//process($sql);exit();$count = $this->database->exec($sql);if ($count === 'false') {process('Update failed: ' . $this->database->errorInfo());} else {//process('Updated ' . $count . ' rows.');}}public function select($table, $field, $where=''){$fieldStr = '*';if ($field != '*') {$fieldStr = implode($field, ',');}if (!empty($where)) {$sql = 'SELECT ' . $fieldStr . ' FROM ' . $table . ' WHERE ' . $where;} else {$sql = 'SELECT ' . $fieldStr . ' FROM ' . $table;}//process($sql);exit();$result = $this->database->query($sql);if ($result === 'false') {process('Select failed: ' . $this->database->errorInfo());} else {/*process($result);foreach ($result as $row) {process($row);}*/return $result;}}}
<?phpnamespace core\lib;class controller{public $assignArray = array();public function assign($key, $value){$this->assignArray[$key] = $value;}public function display($file){$file = APP . '/views/' . $file;if (is_file($file)) {extract($this->assignArray);include $file;}exit();}}
<?phpnamespace core\lib;class apiController{public $output = array('code' => 200,'message' => 'success','data' => '');public function return(){echo json_encode($this->output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);exit();}}