You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
3.4 KiB

  1. <?php
  2. /**
  3. * 接口统一入口
  4. * @author: dogstar 2014-10-04
  5. */
  6. /** ---------------- 根目录定义,自动加载 ---------------- **/
  7. defined('API_ROOT') || define('API_ROOT', dirname(__FILE__));
  8. //自动加载
  9. require_once API_ROOT . '/../PhalApi.php';
  10. $loader = new PhalApi_Loader(API_ROOT, array('Service'));
  11. date_default_timezone_set('Asia/Shanghai');
  12. PhalApi_Translator::setLanguage('zh_cn');
  13. /** ---------------- 注册&初始化服务组件 ---------------- **/
  14. DI()->loader = $loader;
  15. DI()->config = new PhalApi_Config_File(dirname(__FILE__) . '/Config');
  16. DI()->request = new PhalApi_Request();
  17. DI()->logger = new PhalApi_Logger_Explorer(
  18. PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);
  19. DI()->notorm = function() {
  20. $notorm = new PhalApi_DB_NotORM(DI()->config->get('dbs'), true);
  21. return $notorm;
  22. };
  23. DI()->cache = function() {
  24. //$mc = new PhalApi_Cache_Memcached(DI()->config->get('sys.mc'));
  25. $mc = new Memcached_Mock();
  26. return $mc;
  27. };
  28. class Memcached_Mock {
  29. public $data = array();
  30. public function __call($method, $params)
  31. {
  32. echo 'Memcached::' . $method . '() with: ', json_encode($params), " ... \n";
  33. }
  34. public function get($key)
  35. {
  36. echo "Memcached::get($key) ... \n";
  37. return isset($this->data[$key]) ? $this->data[$key] : null;
  38. }
  39. public function set($key, $value, $expire)
  40. {
  41. echo "Memcached::get($key, ", json_encode($value), ", $expire) ... \n";
  42. $this->data[$key] = $value;
  43. }
  44. public function delete($key)
  45. {
  46. unset($this->data[$key]);
  47. }
  48. }
  49. if (!class_exists('Memcached')) {
  50. class Memcached extends Memcached_Mock {
  51. }
  52. }
  53. if (!class_exists('Redis')) {
  54. class Redis {
  55. public function __call($method, $params) {
  56. echo 'Redis::' . $method . '() with: ', json_encode($params), " ... \n";
  57. }
  58. }
  59. }
  60. //加密,测试情况下为防止本地环境没有mcrypt模块 这里作了替身
  61. DI()->crypt = function() {
  62. //return new Crypt_Mock();
  63. return new PhalApi_Crypt_MultiMcrypt(DI()->config->get('sys.crypt.mcrypt_iv'));
  64. };
  65. class Crypt_Mock implements PhalApi_Crypt
  66. {
  67. public function encrypt($data, $key)
  68. {
  69. echo "Crypt_Mock::encrypt($data, $key) ... \n";
  70. return $data;
  71. }
  72. public function decrypt($data, $key)
  73. {
  74. echo "Crypt_Mock::decrypt($data, $key) ... \n";
  75. return $data;
  76. }
  77. }
  78. /** ---------------- 公共的测试替身或桩 ---------------- **/
  79. class PhalApi_Response_Json_Mock extends PhalApi_Response_Json {
  80. protected function handleHeaders($headers) {
  81. }
  82. }
  83. class PhalApi_Response_JsonP_Mock extends PhalApi_Response_JsonP {
  84. protected function handleHeaders($headers) {
  85. }
  86. }
  87. class PhalApi_Api_Impl extends PhalApi_Api {
  88. public function getRules() {
  89. return array(
  90. '*' => array(
  91. 'version' => array('name' => 'version'),
  92. ),
  93. 'add' => array(
  94. 'left' => array('name' => 'left', 'type' => 'int'),
  95. 'right' => array('name' => 'right', 'type' => 'int'),
  96. ),
  97. );
  98. }
  99. public function add()
  100. {
  101. return $this->left + $this->right;
  102. }
  103. }
  104. class PhalApi_Filter_Impl implements PhalApi_Filter {
  105. public function check() {
  106. }
  107. }
  108. if (!class_exists('Yaconf', false)) {
  109. class Yaconf {
  110. public static function __callStatic($method, $params) {
  111. echo "Yaconf::$method()...\n";
  112. }
  113. }
  114. }