/
ApiHandler.php
104 lines (90 loc) · 2.67 KB
/
ApiHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* ApiHandler class - ApiHandler.php file
*
* @author Dmitriy Tyurin <fobia3d@gmail.com>
* @copyright Copyright (c) 2014 Dmitriy Tyurin
*/
namespace Api;
use Log;
/**
* ApiHandler class
*
* @package Api
*/
class ApiHandler
{
/**
* @var string директория классов
*/
protected $classDirectory;
/**
* 'apiMethodName' => array('className', 'classMethod')
* @var array
*/
protected $apimap = array();
public function __construct($classDirectory = null)
{
$this->classDirectory = ($classDirectory) ? $classDirectory : SYSPATH . '/app/Api';
}
/**
* Выполнить метод
*
* @param string $method
* @param array $params
* @return array
*/
public function request($method, $params = null)
{
$params = (array) $params;
if (array_key_exists($method, $this->apimap)) {
$map = $this->apimap[$method];
$class = array_shift($map);
$classMethod = array_shift($map);
} else {
$class = $this->getClass($method);
$classMethod = 'invoke';
$map = array();
}
if ( ! class_exists($class) || ! method_exists( $class, $classMethod)) {
return array(
'error' => array(
'err_msg' => 'неизвестный метод',
'err_code' => 0,
'method' => $method,
'params' => $params
)
);
}
$obj = new $class($params);
$obj->ignoreValidationErrors();
dispatchMethod($obj, $classMethod, $map);
return $obj->getFormatResponse();
}
/**
* Генерирует название класса и подключает при необходимости
*
* @param string $method
* @return string
*/
public function getClass($method, $autoload = true)
{
$class = 'Api_' . preg_replace_callback('/^\w|_\w/', function($matches) {
return strtoupper($matches[0]);
}, str_replace('.', '_', $method));
if ( ! class_exists($class) && $autoload) {
Log::warning("Class '$class' not autoloaded");
$list = explode('.', $method);
array_pop($list);
array_push($list, $method);
$file = $this->classDirectory . '/' . implode('/', $list) . '.php';
if (file_exists($file)) {
require_once $file;
}
if ( ! class_exists($class) ) {
Log::error("Class '$class' not exists.");
}
}
return $class;
}
}