95 lines
2.2 KiB
PHP
95 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace api;
|
||
|
||
include_once "../../global.inc.php";
|
||
include_once PATH_DATABASE . "/SQLManager.php";
|
||
include_once PATH_UTILITTY . "/XmlManager.php";
|
||
include_once PATH_UTILITTY . "/JsonManager.php";
|
||
require_once PATH_3PARTY . "/Slim/Slim.php";
|
||
|
||
\Slim\Slim::registerAutoloader();
|
||
|
||
/**
|
||
* This class provides some general API methods
|
||
* @author stubbfel
|
||
* @since 20.06.2013
|
||
*/
|
||
abstract class Api extends \Slim\Slim {
|
||
|
||
/**
|
||
* Variable for the sql manager of the api
|
||
* @var <T>:SqlManager
|
||
*/
|
||
protected $sqlManager;
|
||
|
||
/**
|
||
* Variable for the serialazarion manager of the api
|
||
* @var <T>:SqlManager
|
||
*/
|
||
protected $serialManager;
|
||
|
||
/**
|
||
* Variable for the contentype of XML
|
||
* @var string
|
||
*/
|
||
protected static $contentypeXML = "application/xml;charset=utf-8";
|
||
|
||
/**
|
||
* Variable for the contentype of Json
|
||
* @var string
|
||
*/
|
||
protected static $contentypeJson = "application/json;charset=utf-8";
|
||
|
||
/**
|
||
* Variable for the regex−string to search json-contenttype
|
||
* @var string
|
||
*/
|
||
private static $jsonRegStr = '/json/';
|
||
|
||
/**
|
||
* Keyword for the accept parameter of the requestheader
|
||
* @var string
|
||
*/
|
||
private static $keyReqHeaderAccept = "Accept";
|
||
|
||
/**
|
||
* Constructor
|
||
* @param array[assoc] $headers - RequestHeader
|
||
*/
|
||
public function __construct($headers = array()) {
|
||
$this->connect();
|
||
parent::__construct();
|
||
|
||
// set content type
|
||
if ($headers && array_key_exists(self::$keyReqHeaderAccept, $headers) && preg_match(self::$jsonRegStr, $headers[self::$keyReqHeaderAccept])) {
|
||
$this->serialManager = new \utiliy\JsonManager();
|
||
$this->contentType(self::$contentypeJson);
|
||
} else {
|
||
$this->serialManager = new \utiliy\XmlManager();
|
||
$this->contentType(self::$contentypeXML);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Default-DeConstructor
|
||
*/
|
||
public function __destruct() {
|
||
|
||
// destroy the sqlManager
|
||
$this->sqlManager->closeConnection();
|
||
unset($this->sqlManager);
|
||
unset($this->serialManager);
|
||
}
|
||
|
||
/**
|
||
* Method start a connection to the database
|
||
*/
|
||
public function connect() {
|
||
$this->sqlManager->connect();
|
||
}
|
||
|
||
}
|
||
|
||
?>
|