source: branches/2.4/prototype/rest/dispatch.php @ 7442

Revision 7442, 2.7 KB checked in by eduardow, 11 years ago (diff)

Ticket #3093 - Integrando API Rest (CELEPAR) - commit para o branches.

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