change line endings
This commit is contained in:
@@ -1,122 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace BSR\Lib;
|
||||
|
||||
use BSR\Lib\Exception\WebException;
|
||||
|
||||
abstract class WebService
|
||||
{
|
||||
private $func = null;
|
||||
private $status = 200;
|
||||
|
||||
private $log = '';
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param int $verbosity
|
||||
* @param bool $withTime
|
||||
*/
|
||||
public function log($message, $verbosity = 1, $withTime = false) {
|
||||
if(Configuration::get('log.verbosity') < $verbosity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if($withTime) {
|
||||
$message = date("d-m-Y h:m:s").' '.$message;
|
||||
}
|
||||
|
||||
$this->log .= $message."\n";
|
||||
}
|
||||
|
||||
public function Run()
|
||||
{
|
||||
$this->log("------------------");
|
||||
$this->log("Start request", 1, true);
|
||||
$data = array();
|
||||
|
||||
try {
|
||||
$result = $this->Call();
|
||||
$data["result"][$this->func] = $result;
|
||||
} catch (WebException $e) {
|
||||
$data["error"]["code"] = $e->getCode();
|
||||
$data["error"]["name"] = $e->getName();
|
||||
$data["error"]["reason"] = $e->getMessage();
|
||||
$this->status = 400;
|
||||
|
||||
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
|
||||
} catch (\Exception $e) {
|
||||
$data["failure"]["message"] = $e->getMessage();
|
||||
$this->status = 500;
|
||||
|
||||
$this->log(sprintf("Failure : %s", $e->getMessage()));
|
||||
}
|
||||
|
||||
$this->Send($data);
|
||||
|
||||
$this->log("Request finished", 1, true);
|
||||
$this->log("------------------\n\n");
|
||||
|
||||
if(Configuration::get('log.verbosity') > 0) {
|
||||
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
}
|
||||
|
||||
private function Call()
|
||||
{
|
||||
ob_start();
|
||||
session_save_path(Configuration::get('session.save_path'));
|
||||
session_start();
|
||||
|
||||
$params = empty($_GET) ? $_POST : $_GET;
|
||||
if (empty($params)) {
|
||||
throw new WebException ("CallArgument", "arguments error", -1);
|
||||
}
|
||||
|
||||
if (!array_key_exists("func", $params)) {
|
||||
throw new WebException ("CallArgFunction", "no 'func' specified", -2);
|
||||
}
|
||||
|
||||
$this->func = $params["func"];
|
||||
unset($params['func']);
|
||||
|
||||
if (!is_callable(array($this, $this->func))) {
|
||||
throw new WebException ("CallFunction", "'func' method not available", -3);
|
||||
}
|
||||
|
||||
$rm = new \ReflectionMethod($this, $this->func);
|
||||
$nbParams = count($params);
|
||||
$nbArgsFix = $rm->getNumberOfRequiredParameters();
|
||||
$nbArgs = $rm->getNumberOfParameters();
|
||||
|
||||
/* Check the number of arguments. */
|
||||
if ($nbParams < $nbArgsFix) {
|
||||
throw new WebException ("CallArgNumber", "you must provide at least " . $nbArgsFix . " arguments", 4);
|
||||
}
|
||||
if ($nbParams > $nbArgs) {
|
||||
throw new WebException ("CallArgNumber", "you must provide at most " . $nbArgs . " arguments", 4);
|
||||
}
|
||||
|
||||
$this->log("Calling '".$this->func."'");
|
||||
$this->log("Params: ".print_r($params, true), 2);
|
||||
return call_user_func_array(array($this, $this->func), $params);
|
||||
}
|
||||
|
||||
private function Send(array $data)
|
||||
{
|
||||
static $status_messages = array(
|
||||
200 => 'Ok',
|
||||
400 => 'Bad request',
|
||||
404 => 'Not Found',
|
||||
403 => 'Not Authorized',
|
||||
500 => 'Server Error',
|
||||
);
|
||||
|
||||
header(sprintf('HTTP/1.0 %s %s', $this->status, $status_messages[$this->status]));
|
||||
|
||||
ob_clean();
|
||||
flush();
|
||||
|
||||
$this->log("Data: ".print_r($data, true), 2);
|
||||
echo json_encode($data);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace BSR\Lib;
|
||||
|
||||
use BSR\Lib\Exception\WebException;
|
||||
|
||||
abstract class WebService
|
||||
{
|
||||
private $func = null;
|
||||
private $status = 200;
|
||||
|
||||
private $log = '';
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param int $verbosity
|
||||
* @param bool $withTime
|
||||
*/
|
||||
public function log($message, $verbosity = 1, $withTime = false) {
|
||||
if(Configuration::get('log.verbosity') < $verbosity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if($withTime) {
|
||||
$message = date("d-m-Y h:m:s").' '.$message;
|
||||
}
|
||||
|
||||
$this->log .= $message."\n";
|
||||
}
|
||||
|
||||
public function Run()
|
||||
{
|
||||
$this->log("------------------");
|
||||
$this->log("Start request", 1, true);
|
||||
$data = array();
|
||||
|
||||
try {
|
||||
$result = $this->Call();
|
||||
$data["result"][$this->func] = $result;
|
||||
} catch (WebException $e) {
|
||||
$data["error"]["code"] = $e->getCode();
|
||||
$data["error"]["name"] = $e->getName();
|
||||
$data["error"]["reason"] = $e->getMessage();
|
||||
$this->status = 400;
|
||||
|
||||
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
|
||||
} catch (\Exception $e) {
|
||||
$data["failure"]["message"] = $e->getMessage();
|
||||
$this->status = 500;
|
||||
|
||||
$this->log(sprintf("Failure : %s", $e->getMessage()));
|
||||
}
|
||||
|
||||
$this->Send($data);
|
||||
|
||||
$this->log("Request finished", 1, true);
|
||||
$this->log("------------------\n\n");
|
||||
|
||||
if(Configuration::get('log.verbosity') > 0) {
|
||||
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
}
|
||||
|
||||
private function Call()
|
||||
{
|
||||
ob_start();
|
||||
session_save_path(Configuration::get('session.save_path'));
|
||||
session_start();
|
||||
|
||||
$params = empty($_GET) ? $_POST : $_GET;
|
||||
if (empty($params)) {
|
||||
throw new WebException ("CallArgument", "arguments error", -1);
|
||||
}
|
||||
|
||||
if (!array_key_exists("func", $params)) {
|
||||
throw new WebException ("CallArgFunction", "no 'func' specified", -2);
|
||||
}
|
||||
|
||||
$this->func = $params["func"];
|
||||
unset($params['func']);
|
||||
|
||||
if (!is_callable(array($this, $this->func))) {
|
||||
throw new WebException ("CallFunction", "'func' method not available", -3);
|
||||
}
|
||||
|
||||
$rm = new \ReflectionMethod($this, $this->func);
|
||||
$nbParams = count($params);
|
||||
$nbArgsFix = $rm->getNumberOfRequiredParameters();
|
||||
$nbArgs = $rm->getNumberOfParameters();
|
||||
|
||||
/* Check the number of arguments. */
|
||||
if ($nbParams < $nbArgsFix) {
|
||||
throw new WebException ("CallArgNumber", "you must provide at least " . $nbArgsFix . " arguments", 4);
|
||||
}
|
||||
if ($nbParams > $nbArgs) {
|
||||
throw new WebException ("CallArgNumber", "you must provide at most " . $nbArgs . " arguments", 4);
|
||||
}
|
||||
|
||||
$this->log("Calling '".$this->func."'");
|
||||
$this->log("Params: ".print_r($params, true), 2);
|
||||
return call_user_func_array(array($this, $this->func), $params);
|
||||
}
|
||||
|
||||
private function Send(array $data)
|
||||
{
|
||||
static $status_messages = array(
|
||||
200 => 'Ok',
|
||||
400 => 'Bad request',
|
||||
404 => 'Not Found',
|
||||
403 => 'Not Authorized',
|
||||
500 => 'Server Error',
|
||||
);
|
||||
|
||||
header(sprintf('HTTP/1.0 %s %s', $this->status, $status_messages[$this->status]));
|
||||
|
||||
ob_clean();
|
||||
flush();
|
||||
|
||||
$this->log("Data: ".print_r($data, true), 2);
|
||||
echo json_encode($data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user