2
2
/******************************************************************************/
3
3
// //
4
4
// InstantMedia //
5
- // http://instantmedia.ru/, [email protected] //
6
- // written by Fuze //
5
+ // http://instantmedia.ru/ //
6
+ // written by Fuze //
7
7
// //
8
8
/******************************************************************************/
9
9
10
10
class actionApiMethod extends cmsAction {
11
11
12
- private $ method_params = array () ;
12
+ private $ method_params = [] ;
13
13
private $ method_controller_name = null ;
14
14
private $ method_action_name = null ;
15
15
@@ -18,21 +18,19 @@ class actionApiMethod extends cmsAction {
18
18
* @var object
19
19
*/
20
20
private $ method_controller = null ;
21
+
21
22
/**
22
23
* Объект класса api метода
23
24
* @var object
24
25
*/
25
- private $ method_action = null ;
26
+ private $ method_action = null ;
26
27
27
- public function __construct ($ controller , $ params= array ()) {
28
+ public function __construct ($ controller , $ params = []) {
28
29
29
30
parent ::__construct ($ controller , $ params );
30
31
31
32
$ this ->loadApiKey ();
32
33
33
- // для метода after ставим коллбэк, нам не нужен вывод на экран шаблона
34
- $ this ->setCallback ('after ' , array (array ($ controller , 'renderJSON ' )));
35
-
36
34
}
37
35
38
36
/**
@@ -44,7 +42,10 @@ public function __construct($controller, $params=array()){
44
42
private function initMethod ($ method_name ) {
45
43
46
44
$ this ->method_name = $ method_name ;
47
- if (empty ($ this ->method_name )){ return $ this ; }
45
+
46
+ if (!$ this ->method_name ) {
47
+ return $ this ;
48
+ }
48
49
49
50
$ segments = explode ('. ' , $ method_name );
50
51
@@ -53,40 +54,37 @@ private function initMethod($method_name) {
53
54
54
55
$ this ->method_controller_name = trim ($ segments [0 ]);
55
56
56
- if ($ this ->method_controller_name && !preg_match ('/^[a-z]{1}[a-z0-9_]*$/ ' , $ this ->method_controller_name )){
57
+ if ($ this ->method_controller_name && !preg_match ('/^[a-z]{1}[a-z0-9_]*$/ ' , $ this ->method_controller_name )) {
57
58
$ this ->method_controller_name = null ;
58
59
}
59
60
60
61
if ($ this ->method_controller_name && !cmsCore::isControllerExists ($ this ->method_controller_name )) {
61
62
$ this ->method_controller_name = null ;
62
63
}
63
64
64
- if ($ this ->method_controller_name ){
65
+ if ($ this ->method_controller_name ) {
65
66
$ this ->method_controller = cmsCore::getController ($ this ->method_controller_name , $ this ->request );
66
67
}
67
-
68
68
}
69
69
// действие
70
70
if (isset ($ segments [1 ])) {
71
71
72
72
$ this ->method_action_name = trim ($ segments [1 ]);
73
73
74
- if ($ this ->method_action_name && !preg_match ('/^[a-z]{1}[a-z0-9_]*$/ ' , $ this ->method_action_name )){
74
+ if ($ this ->method_action_name && !preg_match ('/^[a-z]{1}[a-z0-9_]*$/ ' , $ this ->method_action_name )) {
75
75
$ this ->method_action_name = null ;
76
76
}
77
77
78
- if ($ this ->method_action_name && $ this ->method_controller !== null ){
79
- $ this ->method_controller ->current_action = 'api_ ' . $ this ->method_controller_name . '_ ' . $ this ->method_action_name ;
78
+ if ($ this ->method_action_name && $ this ->method_controller !== null ) {
79
+ $ this ->method_controller ->current_action = 'api_ ' . $ this ->method_controller_name . '_ ' . $ this ->method_action_name ;
80
80
}
81
-
82
81
}
83
82
// Параметры действия
84
- if (count ($ segments ) > 2 ){
83
+ if (count ($ segments ) > 2 ) {
85
84
$ this ->method_params = array_slice ($ segments , 2 );
86
85
}
87
86
88
87
return $ this ;
89
-
90
88
}
91
89
92
90
/**
@@ -279,7 +277,6 @@ public function run($method_name = null){
279
277
280
278
// действия после успешного запроса
281
279
return $ this ->afterRequest ();
282
-
283
280
}
284
281
285
282
/**
@@ -289,25 +286,25 @@ public function run($method_name = null){
289
286
private function afterRequest () {
290
287
291
288
// записываем в лог, если включено
292
- if (!empty ($ this ->options ['log_success ' ])){
293
- $ this ->model ->log (array (
294
- 'request_time ' => number_format (cmsCore::getTime (), 4 ),
295
- 'method ' => $ this ->method_name ,
296
- 'key_id ' => $ this ->key ['id ' ]
297
- ));
289
+ if (!empty ($ this ->options ['log_success ' ])) {
290
+
291
+ $ this ->model ->log ([
292
+ 'request_time ' => number_format ((microtime (true ) - $ this ->start_time ), 4 ),
293
+ 'method ' => $ this ->method_name ,
294
+ 'key_id ' => $ this ->key ['id ' ]
295
+ ]);
298
296
}
299
297
300
298
return true ;
301
-
302
299
}
303
300
304
301
private function validateMethodParams () {
305
302
306
- if (empty ($ this ->method_action ->request_params )){
303
+ if (empty ($ this ->method_action ->request_params )) {
307
304
return false ;
308
305
}
309
306
310
- $ errors = array () ;
307
+ $ errors = [] ;
311
308
312
309
// валидация аналогична валидации форм
313
310
foreach ($ this ->method_action ->request_params as $ param_name => $ rules ) {
@@ -320,44 +317,44 @@ private function validateMethodParams() {
320
317
321
318
$ this ->request ->set ($ param_name , $ value );
322
319
323
- } elseif (!is_null ($ value ) && isset ($ rules ['default ' ])){
320
+ } elseif (!is_null ($ value ) && isset ($ rules ['default ' ])) {
324
321
325
322
$ value = $ this ->request ->get ($ param_name , $ rules ['default ' ]);
326
323
327
324
// для применения типизации переменной
328
325
$ this ->request ->set ($ param_name , $ value );
329
-
330
326
}
331
327
332
- if (!empty ($ rules ['rules ' ])){
328
+ if (!empty ($ rules ['rules ' ])) {
333
329
foreach ($ rules ['rules ' ] as $ rule ) {
334
330
335
- if (!$ rule ) { continue ; }
331
+ if (!$ rule ) {
332
+ continue ;
333
+ }
336
334
337
335
$ validate_function = "validate_ {$ rule [0 ]}" ;
338
336
339
337
$ rule [] = $ value ;
340
338
341
339
unset($ rule [0 ]);
342
340
343
- $ result = call_user_func_array (array ( $ this , $ validate_function) , $ rule );
341
+ $ result = call_user_func_array ([ $ this , $ validate_function] , $ rule );
344
342
345
343
// если получилось false, то дальше не проверяем, т.к.
346
344
// ошибка уже найдена
347
345
if ($ result !== true ) {
348
346
$ errors [$ param_name ] = $ result ;
349
347
break ;
350
348
}
351
-
352
349
}
353
350
}
354
-
355
351
}
356
352
357
- if (!sizeof ($ errors )) { return false ; }
353
+ if (!sizeof ($ errors )) {
354
+ return false ;
355
+ }
358
356
359
357
return $ errors ;
360
-
361
358
}
362
359
363
360
/**
@@ -367,23 +364,27 @@ private function validateMethodParams() {
367
364
public function checkRequest () {
368
365
369
366
$ parent_succes = parent ::checkRequest ();
370
- if (!$ parent_succes ){ return false ; }
371
367
372
- if (empty ($ this ->method_name ) ||
368
+ if (!$ parent_succes ) {
369
+ return false ;
370
+ }
371
+
372
+ if (empty ($ this ->method_name ) ||
373
373
empty ($ this ->method_controller_name ) ||
374
- $ this ->method_controller === null ){
374
+ $ this ->method_controller === null ) {
375
+
375
376
return $ this ->error (3 );
376
377
}
377
378
378
- if (empty ($ this ->method_action_name )){
379
+ if (empty ($ this ->method_action_name )) {
379
380
return $ this ->error (8 );
380
381
}
381
382
382
- if (!$ this ->method_controller ->isEnabled ()){
383
+ if (!$ this ->method_controller ->isEnabled ()) {
383
384
return $ this ->error (23 );
384
385
}
385
386
386
- $ check_method_name = $ this ->method_controller_name . '. ' . $ this ->method_action_name ;
387
+ $ check_method_name = $ this ->method_controller_name . '. ' . $ this ->method_action_name ;
387
388
388
389
$ is_view = !$ this ->key ['key_methods ' ]['allow ' ] || in_array ($ check_method_name , $ this ->key ['key_methods ' ]['allow ' ]);
389
390
$ is_hide = $ this ->key ['key_methods ' ]['disallow ' ] && in_array ($ check_method_name , $ this ->key ['key_methods ' ]['disallow ' ]);
@@ -394,7 +395,6 @@ public function checkRequest() {
394
395
}
395
396
396
397
return true ;
397
-
398
398
}
399
399
400
400
}
0 commit comments