Last active
September 21, 2015 12:22
-
-
Save di7spider/9f100b32ed9817ec2bae to your computer and use it in GitHub Desktop.
1C Bitrix :: base frame API on phalconphp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define("NO_AGENT_CHECK", true); | |
require_once $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'; | |
/** | |
* API | |
* | |
* https://docs.phalconphp.com/ru/latest/api/index.html | |
*/ | |
class Api | |
{ | |
static public $app; | |
/** Http коды */ | |
static public $status = array( | |
100 => 'Continue', | |
101 => 'Switching Protocols', | |
200 => 'OK', | |
201 => 'Created', | |
202 => 'Accepted', | |
203 => 'Non-Authoritative Information', | |
204 => 'No Content', | |
205 => 'Reset Content', | |
206 => 'Partial Content', | |
300 => 'Multiple Choices', | |
301 => 'Moved Permanently', | |
302 => 'Found', | |
303 => 'See Other', | |
304 => 'Not Modified', | |
305 => 'Use Proxy', | |
307 => 'Temporary Redirect', | |
400 => 'Bad Request', | |
401 => 'Unauthorized', | |
402 => 'Payment Required', | |
403 => 'Forbidden', | |
404 => 'Not Found', | |
405 => 'Method Not Allowed', | |
406 => 'Not Acceptable', | |
407 => 'Proxy Authentication Required', | |
408 => 'Request Timeout', | |
409 => 'Conflict', | |
410 => 'Gone', | |
411 => 'Length Required', | |
412 => 'Precondition Failed', | |
413 => 'Request Entity Too Large', | |
414 => 'Request-URI Too Long', | |
415 => 'Unsupported Media Type', | |
416 => 'Requested Range Not Satisfiable', | |
417 => 'Expectation Failed', | |
500 => 'Internal Server Error', | |
501 => 'Not Implemented', | |
502 => 'Bad Gateway', | |
503 => 'Service Unavailable', | |
504 => 'Gateway Timeout', | |
505 => 'HTTP Version Not Supported', | |
509 => 'Bandwidth Limit Exceeded' | |
); | |
/** Проверка прав доступа */ | |
static public function isAccess() | |
{ | |
$app = self::$app; | |
if( is_object($app) ){ | |
return self::$app-> request-> isAjax() || $GLOBALS['USER']-> IsAdmin(); | |
} | |
} | |
/** Response ответ */ | |
static public function response($params) | |
{ | |
$app = self::$app; | |
if( is_array($params) && is_object($app) ){ | |
$params['status'] = !empty($params['status']) ? $params['status'] : 200; | |
$params['format'] = !empty($params['format']) ? $params['format'] : 'json'; | |
$response = $app-> response; | |
$response-> setStatusCode( | |
$params['status'], | |
self::$status[ | |
$params['status'] | |
] | |
); | |
$response-> sendHeaders(); | |
if( self::isAccess() ){ | |
if($app-> request-> get('debug', 'int') != 1){ | |
$response-> setContentType('application/'.$params['format']); | |
} | |
if($params['format'] == 'json'){ | |
$response-> setJsonContent($params['response']); | |
} | |
}else{ | |
$response-> setContent("NOT-FOUND"); | |
} | |
$response-> send(); | |
} | |
} | |
/** Обработчик ошибок */ | |
static public function error($e) | |
{ | |
/** Отдаем ответ */ | |
self::response( | |
Array( | |
'response' => array( | |
'status' => 'ERROR' | |
) | |
) | |
); | |
} | |
/** Обработчик запросов */ | |
static public function on() | |
{ | |
$loader = new Phalcon\Loader; | |
$di = new Phalcon\DI\FactoryDefault(); | |
self::$app = $app = new Phalcon\Mvc\Micro($di); | |
// Получение списка всех роботов | |
$app | |
-> map( | |
'/{v:v[0-9]+}/{c:[a-zA-Z\d_]+}/{a:[a-zA-Z\d_]+}.{f:json}', | |
function($version, $controller, $action, $format) use($app, $loader){ | |
$arResponse = Array( | |
'format' => $format, | |
'response' => array( | |
'status' => 'ERROR' | |
) | |
); | |
if( Api::isAccess() ){ | |
$dispatcher = $app-> dispatcher; | |
$response = $app-> response; | |
$dir = __DIR__.'/source/'.$version; | |
if( file_exists($dir) ){ | |
$loader | |
/** Регистрируем пространсво имен */ | |
-> registerNamespaces( | |
array( | |
"Api" => $dir.'/api/' | |
) | |
) | |
/** Регистрируем директории */ | |
-> registerDirs( | |
array( | |
$dir.'/lib/' | |
) | |
) | |
-> register(); | |
/** Подключаем конфиг файлы */ | |
array_map( | |
function($name) use ($dir){ | |
$path = $dir.'/'.$name.'.php'; | |
if( file_exists($path) ){ | |
require_once $path; | |
} | |
}, | |
array( | |
'settings', | |
'functions' | |
) | |
); | |
/** Диспатчер, подгружает класс обработчик для API */ | |
$dispatcher-> setNamespaceName('Api'); | |
$dispatcher-> setControllerName( | |
Phalcon\Text::uncamelize($controller) | |
); | |
$dispatcher-> setActionName( | |
lcfirst( | |
Phalcon\Text::uncamelize($action) | |
) | |
); | |
$dispatcher-> dispatch(); | |
$arResponse['response'] = $value = $dispatcher-> getReturnedValue(); | |
$arResponse['status'] = $value['status']['code']; | |
$arResponse['response']['status'] = ( | |
!empty($value['status']['type']) | |
? $value['status']['type'] | |
: 'OK' | |
); | |
} | |
} | |
/** Отдаем ответ */ | |
Api::response($arResponse); | |
} | |
) | |
-> via( | |
array( | |
'GET', | |
'POST' | |
) | |
); | |
/** Обработчик 404 */ | |
$app-> notFound(function() use ($app){ | |
/** Отдаем ответ */ | |
Api::response( | |
Array( | |
'status' => 404, | |
'response' => array( | |
'status' => 'NOT-FOUND' | |
) | |
) | |
); | |
}); | |
$app-> handle(); | |
} | |
} | |
try{ | |
Api::on(); | |
}catch(Phalcon\Exception $e){ | |
Api::error($e); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment