[6351] | 1 | <?php |
---|
| 2 | // load Tonic library |
---|
[7322] | 3 | require_once(__DIR__ . '/../library/tonic/lib/tonic.php'); |
---|
| 4 | require_once(__DIR__.'/../library/utils/Errors.php'); |
---|
| 5 | require_once(__DIR__ . '/../api/controller.php'); |
---|
| 6 | require_once(ROOTPATH . '/rest/oauth/OAuth2StorageUserCredential.php'); |
---|
[6351] | 7 | |
---|
[7322] | 8 | // load adapters |
---|
| 9 | require_once(__DIR__."/../adapters/ExpressoAdapter.php"); |
---|
| 10 | require_once(__DIR__."/../adapters/MailAdapter.php"); |
---|
| 11 | require_once(__DIR__."/../adapters/CatalogAdapter.php"); |
---|
| 12 | require_once(__DIR__."/../adapters/CalendarAdapter.php"); |
---|
| 13 | //require_once(__DIR__."/../adapters/testeAdapter.php"); |
---|
| 14 | |
---|
[6351] | 15 | //Retrieveing the mapping of the URIs and his respectives classNames and classPath |
---|
| 16 | $config = parse_ini_file( __DIR__ . '/../config/Tonic.srv', true ); |
---|
| 17 | |
---|
| 18 | //looping through the mapping to create 2 separated maps: |
---|
| 19 | // First indexed by the uri that carry the classNames, |
---|
| 20 | // that its used by Tonic to autoload them when routed by him accordingly; |
---|
| 21 | // Second indexed by the className that carry the classPaths, |
---|
| 22 | // used by the autoload register to find the correct path of the class that's going to |
---|
| 23 | // be loaded; |
---|
| 24 | |
---|
| 25 | $autoload = array(); |
---|
| 26 | $classpath = array(); |
---|
| 27 | |
---|
[7322] | 28 | foreach( $config as $uri => $classFile ) |
---|
| 29 | { |
---|
[6351] | 30 | foreach( $classFile as $className => $filePath ) |
---|
| 31 | { |
---|
[7322] | 32 | $autoload[ $uri ] = $className; |
---|
| 33 | $classpath[ $className ] = $filePath; |
---|
[6351] | 34 | } |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | //The autoload function that's called by the PHP when Tonic routes a class not declared previously |
---|
| 38 | function __autoload($class) { |
---|
| 39 | |
---|
| 40 | global $classpath; |
---|
| 41 | |
---|
[6357] | 42 | if(isset($classpath[ $class ])){ |
---|
| 43 | require_once(__DIR__ . $classpath[ $class ] ); |
---|
| 44 | } |
---|
[6351] | 45 | } |
---|
| 46 | |
---|
| 47 | // handle request, passing the current env baseUri and autoload mapping; |
---|
[6367] | 48 | |
---|
| 49 | $restConf = parse_ini_file( __DIR__ . '/../config/REST.ini', true ); |
---|
[6351] | 50 | $request = new Request(array( |
---|
[6367] | 51 | 'baseUri'=> $restConf['baseUri'], |
---|
[6351] | 52 | 'autoload' => $autoload, |
---|
| 53 | )); |
---|
| 54 | |
---|
| 55 | try { |
---|
| 56 | $resource = $request->loadResource(); |
---|
| 57 | $response = $resource->exec($request); |
---|
| 58 | |
---|
| 59 | } catch (ResponseException $e) { |
---|
[7322] | 60 | switch ($e->getCode()) |
---|
| 61 | { |
---|
| 62 | case Response::UNAUTHORIZED: |
---|
| 63 | $response = $e->response($request); |
---|
| 64 | $response->addHeader('WWW-Authenticate', 'Basic realm="Tonic"'); |
---|
| 65 | break; |
---|
| 66 | |
---|
| 67 | default: |
---|
| 68 | $response = new Response($request); |
---|
| 69 | $response->code = Response::OK; |
---|
| 70 | $response->addHeader('content-type', 'application/json'); |
---|
| 71 | if($request->id) |
---|
| 72 | { |
---|
| 73 | $body['id'] = $request->id; |
---|
| 74 | } |
---|
| 75 | $body['error'] = array("code" => "".$e->getCode(), "message" => $e->getMessage()); |
---|
| 76 | |
---|
| 77 | $response->body = json_encode($body); |
---|
| 78 | |
---|
| 79 | //$response = $e->response($request); |
---|
[6351] | 80 | } |
---|
| 81 | } |
---|
[7322] | 82 | |
---|
[6351] | 83 | $response->output(); |
---|
| 84 | |
---|
[7322] | 85 | ?> |
---|