22/******************************************************************************/
33// //
44// InstantMedia //
5- // http://instantmedia.ru/, support@instantmedia.ru //
6- // written by Fuze //
5+ // http://instantmedia.ru/ //
6+ // written by Fuze //
77// //
88/******************************************************************************/
99
1010class actionApiMethod extends cmsAction {
1111
12- private $ method_params = array () ;
12+ private $ method_params = [] ;
1313 private $ method_controller_name = null ;
1414 private $ method_action_name = null ;
1515
@@ -18,21 +18,19 @@ class actionApiMethod extends cmsAction {
1818 * @var object
1919 */
2020 private $ method_controller = null ;
21+
2122 /**
2223 * Объект класса api метода
2324 * @var object
2425 */
25- private $ method_action = null ;
26+ private $ method_action = null ;
2627
27- public function __construct ($ controller , $ params= array ()) {
28+ public function __construct ($ controller , $ params = []) {
2829
2930 parent ::__construct ($ controller , $ params );
3031
3132 $ this ->loadApiKey ();
3233
33- // для метода after ставим коллбэк, нам не нужен вывод на экран шаблона
34- $ this ->setCallback ('after ' , array (array ($ controller , 'renderJSON ' )));
35-
3634 }
3735
3836 /**
@@ -44,7 +42,10 @@ public function __construct($controller, $params=array()){
4442 private function initMethod ($ method_name ) {
4543
4644 $ this ->method_name = $ method_name ;
47- if (empty ($ this ->method_name )){ return $ this ; }
45+
46+ if (!$ this ->method_name ) {
47+ return $ this ;
48+ }
4849
4950 $ segments = explode ('. ' , $ method_name );
5051
@@ -53,40 +54,37 @@ private function initMethod($method_name) {
5354
5455 $ this ->method_controller_name = trim ($ segments [0 ]);
5556
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 )) {
5758 $ this ->method_controller_name = null ;
5859 }
5960
6061 if ($ this ->method_controller_name && !cmsCore::isControllerExists ($ this ->method_controller_name )) {
6162 $ this ->method_controller_name = null ;
6263 }
6364
64- if ($ this ->method_controller_name ){
65+ if ($ this ->method_controller_name ) {
6566 $ this ->method_controller = cmsCore::getController ($ this ->method_controller_name , $ this ->request );
6667 }
67-
6868 }
6969 // действие
7070 if (isset ($ segments [1 ])) {
7171
7272 $ this ->method_action_name = trim ($ segments [1 ]);
7373
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 )) {
7575 $ this ->method_action_name = null ;
7676 }
7777
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 ;
8080 }
81-
8281 }
8382 // Параметры действия
84- if (count ($ segments ) > 2 ){
83+ if (count ($ segments ) > 2 ) {
8584 $ this ->method_params = array_slice ($ segments , 2 );
8685 }
8786
8887 return $ this ;
89-
9088 }
9189
9290 /**
@@ -279,7 +277,6 @@ public function run($method_name = null){
279277
280278 // действия после успешного запроса
281279 return $ this ->afterRequest ();
282-
283280 }
284281
285282 /**
@@ -289,25 +286,25 @@ public function run($method_name = null){
289286 private function afterRequest () {
290287
291288 // записываем в лог, если включено
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+ ]);
298296 }
299297
300298 return true ;
301-
302299 }
303300
304301 private function validateMethodParams () {
305302
306- if (empty ($ this ->method_action ->request_params )){
303+ if (empty ($ this ->method_action ->request_params )) {
307304 return false ;
308305 }
309306
310- $ errors = array () ;
307+ $ errors = [] ;
311308
312309 // валидация аналогична валидации форм
313310 foreach ($ this ->method_action ->request_params as $ param_name => $ rules ) {
@@ -320,44 +317,44 @@ private function validateMethodParams() {
320317
321318 $ this ->request ->set ($ param_name , $ value );
322319
323- } elseif (!is_null ($ value ) && isset ($ rules ['default ' ])){
320+ } elseif (!is_null ($ value ) && isset ($ rules ['default ' ])) {
324321
325322 $ value = $ this ->request ->get ($ param_name , $ rules ['default ' ]);
326323
327324 // для применения типизации переменной
328325 $ this ->request ->set ($ param_name , $ value );
329-
330326 }
331327
332- if (!empty ($ rules ['rules ' ])){
328+ if (!empty ($ rules ['rules ' ])) {
333329 foreach ($ rules ['rules ' ] as $ rule ) {
334330
335- if (!$ rule ) { continue ; }
331+ if (!$ rule ) {
332+ continue ;
333+ }
336334
337335 $ validate_function = "validate_ {$ rule [0 ]}" ;
338336
339337 $ rule [] = $ value ;
340338
341339 unset($ rule [0 ]);
342340
343- $ result = call_user_func_array (array ( $ this , $ validate_function) , $ rule );
341+ $ result = call_user_func_array ([ $ this , $ validate_function] , $ rule );
344342
345343 // если получилось false, то дальше не проверяем, т.к.
346344 // ошибка уже найдена
347345 if ($ result !== true ) {
348346 $ errors [$ param_name ] = $ result ;
349347 break ;
350348 }
351-
352349 }
353350 }
354-
355351 }
356352
357- if (!sizeof ($ errors )) { return false ; }
353+ if (!sizeof ($ errors )) {
354+ return false ;
355+ }
358356
359357 return $ errors ;
360-
361358 }
362359
363360 /**
@@ -367,23 +364,27 @@ private function validateMethodParams() {
367364 public function checkRequest () {
368365
369366 $ parent_succes = parent ::checkRequest ();
370- if (!$ parent_succes ){ return false ; }
371367
372- if (empty ($ this ->method_name ) ||
368+ if (!$ parent_succes ) {
369+ return false ;
370+ }
371+
372+ if (empty ($ this ->method_name ) ||
373373 empty ($ this ->method_controller_name ) ||
374- $ this ->method_controller === null ){
374+ $ this ->method_controller === null ) {
375+
375376 return $ this ->error (3 );
376377 }
377378
378- if (empty ($ this ->method_action_name )){
379+ if (empty ($ this ->method_action_name )) {
379380 return $ this ->error (8 );
380381 }
381382
382- if (!$ this ->method_controller ->isEnabled ()){
383+ if (!$ this ->method_controller ->isEnabled ()) {
383384 return $ this ->error (23 );
384385 }
385386
386- $ check_method_name = $ this ->method_controller_name . '. ' . $ this ->method_action_name ;
387+ $ check_method_name = $ this ->method_controller_name . '. ' . $ this ->method_action_name ;
387388
388389 $ is_view = !$ this ->key ['key_methods ' ]['allow ' ] || in_array ($ check_method_name , $ this ->key ['key_methods ' ]['allow ' ]);
389390 $ is_hide = $ this ->key ['key_methods ' ]['disallow ' ] && in_array ($ check_method_name , $ this ->key ['key_methods ' ]['disallow ' ]);
@@ -394,7 +395,6 @@ public function checkRequest() {
394395 }
395396
396397 return true ;
397-
398398 }
399399
400400}
0 commit comments