1: <?php
2:
3: namespace api;
4:
5: include_once "../../global.inc.php";
6: include_once PATH_DATABASE . "/SQLManager.php";
7: include_once PATH_UTILITTY . "/XmlManager.php";
8: include_once PATH_UTILITTY . "/JsonManager.php";
9: require_once PATH_3PARTY . "/Slim/Slim.php";
10:
11: \Slim\Slim::registerAutoloader();
12:
13: /**
14: * This class provides some general API methods
15: * @author stubbfel
16: * @since 20.06.2013
17: */
18: abstract class Api extends \Slim\Slim {
19:
20: /**
21: * Variable for the sql manager of the api
22: * @var <T>:SqlManager
23: */
24: protected $sqlManager;
25:
26: /**
27: * Variable for the serialazarion manager of the api
28: * @var <T>:SqlManager
29: */
30: protected $serialManager;
31:
32: /**
33: * Variable for the contentype of XML
34: * @var string
35: */
36: protected static $contentypeXML = "application/xml;charset=utf-8";
37:
38: /**
39: * Variable for the contentype of Json
40: * @var string
41: */
42: protected static $contentypeJson = "application/json;charset=utf-8";
43:
44: /**
45: * Variable for the regex−string to search json-contenttype
46: * @var string
47: */
48: private static $jsonRegStr = '/json/';
49:
50: /**
51: * Keyword for the accept parameter of the requestheader
52: * @var string
53: */
54: private static $keyReqHeaderAccept = "Accept";
55:
56: /**
57: * Constructor
58: * @param array[assoc] $headers - RequestHeader
59: */
60: public function __construct($headers = array()) {
61: $this->connect();
62: parent::__construct();
63:
64: // set content type
65: if ($headers && array_key_exists(self::$keyReqHeaderAccept, $headers) && preg_match(self::$jsonRegStr, $headers[self::$keyReqHeaderAccept])) {
66: $this->serialManager = new \utiliy\JsonManager();
67: $this->contentType(self::$contentypeJson);
68: } else {
69: $this->serialManager = new \utiliy\XmlManager();
70: $this->contentType(self::$contentypeXML);
71: }
72: }
73:
74: /**
75: * Default-DeConstructor
76: */
77: public function __destruct() {
78:
79: // destroy the sqlManager
80: $this->sqlManager->closeConnection();
81: unset($this->sqlManager);
82: unset($this->serialManager);
83: }
84:
85: /**
86: * Method start a connection to the database
87: */
88: public function connect() {
89: $this->sqlManager->connect();
90: }
91:
92: }
93:
94: ?>
95: