[关闭]
@liuximing 2017-12-15T15:52:15.000000Z 字数 9052 阅读 112

Siaf_MVC_V1.0

PHP


Siaf: Siaf is another framework.

框架运行流程

入口文件定义常量引入函数库自动加载类启动框架路由解析加载控制器返回结果

框架目录

Siaf

入口文件(/index.php)

  1. <?php
  2. /**
  3. * 入口文件
  4. * 1.定义常量
  5. * 2.加载函数库
  6. * 3.自动加载类库
  7. * 4.启动框架
  8. */
  9. //1.定义常量
  10. define('ROOT', dirname(__FILE__));
  11. define('CORE', ROOT.'/core');
  12. define('APP', ROOT.'/app');
  13. //2.加载函数库
  14. include CORE.'/common/function.php';
  15. process(ROOT);
  16. include CORE.'/base.php';
  17. //3.自动加载类库
  18. spl_autoload_register('\core\base::load');
  19. //4.启动框架
  20. \core\base::run();

基础类(/core/base.php)

自动加载类库方法、启动方法(路由和加载控制器)

  1. <?php
  2. namespace core;
  3. class base
  4. {
  5. static public $classMap = array();
  6. static public function load($class)
  7. {
  8. /**
  9. * 自动加载类库
  10. * new \core\route();
  11. * $class = 'core\route()';
  12. * ROOT.'/'.'core/route.php';
  13. */
  14. if (isset($classMap[$class])) {
  15. return true;
  16. } else {
  17. $temp = str_replace('\\', '/', $class);
  18. $file = ROOT.'/'.$temp.'.php';
  19. if(is_file($file)) {
  20. include $file;
  21. self::$classMap[$class] = $class;
  22. } else {
  23. return false;
  24. }
  25. }
  26. }
  27. static public function run()
  28. {
  29. //process('siaf is running...');
  30. \core\lib\route::routing();
  31. $controllerClass = \core\lib\route::controller . 'Controller';
  32. $action = \core\lib\route::action;
  33. $controllerFile = APP . '/controller/' . $controllerClass . '.php';
  34. if (is_file($controllerFile)) {
  35. include $controllerFile;
  36. $controllerClass = '\app\controller\\' . $controllerClass;
  37. $controller = new $controllerClass();
  38. $controller->$action();
  39. } else {
  40. throw new \Exception('找不到控制器' . $controllerClass);
  41. }
  42. //process('siaf is stop');
  43. }
  44. }

路由类(/core/lib/route.php)

  1. <?php
  2. namespace core\lib;
  3. class route
  4. {
  5. static public $controller = 'index';
  6. static public $action = 'index';
  7. static public function routing()
  8. {
  9. //process('route start...');
  10. //xxx.com/controller/action
  11. /**
  12. * 1.隐藏index.php:
  13. * xxx.com/index.php/controller/action
  14. * -> xxx.com/controller/action
  15. * 2.获取URL参数部分
  16. * 3.返回对应的控制器和方法
  17. */
  18. //process($_SERVER);
  19. if (isset($_SERVER['REQUEST_URI'])) {
  20. $path = explode('?', $_SERVER['REQUEST_URI'])[0];
  21. if ($path != '/') {
  22. $pathArray = explode('/', trim($path, '/'));
  23. if (isset($pathArray[2])) {
  24. $this->controller = $pathArray[2];
  25. }
  26. if (isset($pathArray[3])) {
  27. $this->action = $pathArray[3];
  28. }
  29. }
  30. }
  31. //process('route end');
  32. }
  33. }

配置类(/core/lib/config.php)

  1. <?php
  2. namespace core\lib;
  3. class config
  4. {
  5. static public $configCache = array();
  6. static public function getConfig($selector, $item='')
  7. {
  8. switch ($selector) {
  9. case 'php':
  10. return self::getFromPhp($item);
  11. case 'ini':
  12. return self::getFromIni($item);
  13. case 'xml':
  14. return self::getFromXml($item);
  15. case 'json':
  16. return self::getFromJson($item);
  17. }
  18. }
  19. static protected function getFromPhp($item='')
  20. {
  21. if (empty(self::$configCache)) {
  22. $configFile = ROOT . '/core/config/config.php';
  23. if (is_file($configFile)) {
  24. self::$configCache = include $configFile;
  25. //process(self::$configCache);exit();
  26. } else {
  27. throw new \Exception('找不到配置文件');
  28. }
  29. }
  30. if (empty($item)) {
  31. return self::$configCache;
  32. } else {
  33. if (isset(self::$configCache[$item])) {
  34. return self::$configCache[$item];
  35. } else {
  36. throw new \Exception('找不到配置项' . $item);
  37. }
  38. }
  39. }
  40. static protected function getFromIni($item='')
  41. {
  42. if (empty(self::$configCache)) {
  43. $configFile = ROOT . '/core/config/config.ini';
  44. if (is_file($configFile)) {
  45. self::$configCache = parse_ini_file($configFile, true);
  46. //process(self::$configCache);exit();
  47. } else {
  48. throw new \Exception('找不到配置文件');
  49. }
  50. }
  51. if (empty($item)) {
  52. return self::$configCache;
  53. } else {
  54. if (isset(self::$configCache[$item])) {
  55. return self::$configCache[$item];
  56. } else {
  57. throw new \Exception('找不到配置项' . $item);
  58. }
  59. }
  60. }
  61. static protected function getFromXml($item='')
  62. {
  63. if (empty(self::$configCache)) {
  64. $configFile = ROOT . '/core/config/config.xml';
  65. if (is_file($configFile)) {
  66. self::$configCache = simplexml_load_file($configFile);
  67. self::$configCache = self::objectToArray(self::$configCache);
  68. //process(self::$configCache);exit();
  69. } else {
  70. throw new \Exception('找不到配置文件');
  71. }
  72. }
  73. if (empty($item)) {
  74. return self::$configCache;
  75. } else {
  76. if (isset(self::$configCache[$item])) {
  77. return self::$configCache[$item];
  78. } else {
  79. throw new \Exception('找不到配置项' . $item);
  80. }
  81. }
  82. }
  83. static protected function getFromJson($item='')
  84. {
  85. if (empty(self::$configCache)) {
  86. $configFile = ROOT . '/core/config/config.json';
  87. if (is_file($configFile)) {
  88. self::$configCache = file_get_contents($configFile);
  89. self::$configCache = json_decode(self::$configCache);
  90. self::$configCache = self::objectToArray(self::$configCache);
  91. //process(self::$configCache);exit();
  92. } else {
  93. throw new \Exception('找不到配置文件');
  94. }
  95. }
  96. if (empty($item)) {
  97. return self::$configCache;
  98. } else {
  99. if (isset(self::$configCache[$item])) {
  100. return self::$configCache[$item];
  101. } else {
  102. throw new \Exception('找不到配置项' . $item);
  103. }
  104. }
  105. }
  106. static protected function objectToArray($obj) {
  107. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  108. foreach ($_arr as $key => $val)
  109. {
  110. $val = is_object($val) ? self::objectToArray($val) : $val;
  111. $arr[$key] = $val;
  112. }
  113. return $arr;
  114. }
  115. }

日志类(/core/lib/log.php)

  1. <?php
  2. namespace core\lib;
  3. use core\lib\config;
  4. class log
  5. {
  6. static public $config = '';
  7. static public $path = '';
  8. static public function init()
  9. {
  10. self::$config = config::getConfig('ini', 'log');
  11. if (isset(self::$config['path'])) {
  12. self::$path = self::$config['path'];
  13. }
  14. }
  15. static public function writeLog($content, $type)
  16. {
  17. date_default_timezone_set('Asia/Shanghai');
  18. if (!is_dir(self::$path . date('Ymd'))) {
  19. mkdir(self::$path . date('Ymd'), '0777', true);
  20. }
  21. $content = date('Y-m-d H:i:s') . ' | ' . json_encode($content) . PHP_EOL;
  22. return file_put_contents(self::$path . date('Ymd') . '/' . $type . '.php', $content, FILE_APPEND);
  23. }
  24. }

模型类(/core/lib/model.php)

  1. <?php
  2. namespace core\lib;
  3. use core\lib\config;
  4. class model extends \PDO
  5. {
  6. public $config = '';
  7. public $dsn = '';
  8. public $username = '';
  9. public $passwd = '';
  10. public $database = '';
  11. public function __construct()
  12. {
  13. $this->config = config::getConfig('ini', 'database');
  14. if (isset($this->config['dsn'])) {
  15. $this->dsn = $this->config['dsn'];
  16. }
  17. if (isset($this->config['username'])) {
  18. $this->username = $this->config['username'];
  19. }
  20. if (isset($this->config['passwd'])) {
  21. $this->passwd = $this->config['passwd'];
  22. }
  23. }
  24. public function connect()
  25. {
  26. try {
  27. $this->database = new \PDO($this->dsn, $this->username, $this->passwd);
  28. } catch (\PDOException $e) {
  29. process('Connection failed: ' . $e->getMessage());
  30. }
  31. }
  32. public function insert($table, $data)
  33. {
  34. $keyStr = '(';
  35. $valueStr = '(';
  36. foreach ($data as $key => $value) {
  37. $keyStr .= $key . ',';
  38. $valueStr .= '\'' . $value . '\'' . ',';
  39. }
  40. $keyStr = substr_replace($keyStr, ')', -1);
  41. $valueStr = substr_replace($valueStr, ')', -1);
  42. $sql = 'INSERT INTO ' . $table . ' ' . $keyStr . ' VALUES ' . $valueStr;
  43. //process($sql);exit();
  44. $count = $this->database->exec($sql);
  45. if ($count === 'false') {
  46. process('Insert failed: ' . $this->database->errorInfo());
  47. } else {
  48. //process('Inserted ' . $count . ' rows.');
  49. }
  50. }
  51. public function delete($table, $where)
  52. {
  53. $sql = 'DELETE FROM ' . $table . ' WHERE ' . $where;
  54. //process($sql);exit();
  55. $count = $this->database->exec($sql);
  56. if ($count === 'false') {
  57. process('Delete failed: ' . $this->database->errorInfo());
  58. } else {
  59. //process('Deleted ' . $count . ' rows.');
  60. }
  61. }
  62. public function update($table, $data, $where)
  63. {
  64. $dataStr = '';
  65. foreach ($data as $key => $value) {
  66. $dataStr .= $key . '=' . '\'' . $value . '\',';
  67. }
  68. $dataStr = substr_replace($dataStr, '', -1);
  69. $sql = 'UPDATE ' . $table . ' SET ' . $dataStr . ' WHERE ' . $where;
  70. //process($sql);exit();
  71. $count = $this->database->exec($sql);
  72. if ($count === 'false') {
  73. process('Update failed: ' . $this->database->errorInfo());
  74. } else {
  75. //process('Updated ' . $count . ' rows.');
  76. }
  77. }
  78. public function select($table, $field, $where='')
  79. {
  80. $fieldStr = '*';
  81. if ($field != '*') {
  82. $fieldStr = implode($field, ',');
  83. }
  84. if (!empty($where)) {
  85. $sql = 'SELECT ' . $fieldStr . ' FROM ' . $table . ' WHERE ' . $where;
  86. } else {
  87. $sql = 'SELECT ' . $fieldStr . ' FROM ' . $table;
  88. }
  89. //process($sql);exit();
  90. $result = $this->database->query($sql);
  91. if ($result === 'false') {
  92. process('Select failed: ' . $this->database->errorInfo());
  93. } else {
  94. /*process($result);
  95. foreach ($result as $row) {
  96. process($row);
  97. }*/
  98. return $result;
  99. }
  100. }
  101. }

控制器类(/core/lib/controller.php)

  1. <?php
  2. namespace core\lib;
  3. class controller
  4. {
  5. public $assignArray = array();
  6. public function assign($key, $value)
  7. {
  8. $this->assignArray[$key] = $value;
  9. }
  10. public function display($file)
  11. {
  12. $file = APP . '/views/' . $file;
  13. if (is_file($file)) {
  14. extract($this->assignArray);
  15. include $file;
  16. }
  17. exit();
  18. }
  19. }

接口控制器类(/core/lib/apiController.php)

  1. <?php
  2. namespace core\lib;
  3. class apiController
  4. {
  5. public $output = array(
  6. 'code' => 200,
  7. 'message' => 'success',
  8. 'data' => ''
  9. );
  10. public function return()
  11. {
  12. echo json_encode($this->output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  13. exit();
  14. }
  15. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注