source: trunk/prototype/rest/dispatch.php @ 7342

Revision 7342, 2.4 KB checked in by alexandrecorreia, 11 years ago (diff)

Ticket #3093 - Integrando API Rest(CELEPAR) com o ramo Trunk.

  • Property svn:executable set to *
Line 
1<?php
2// load Tonic library
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");
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
27foreach( $config as $uri => $classFile )
28{
29    foreach( $classFile as $className => $filePath )
30    {
31                $autoload[ $uri ] = $className;
32                $classpath[ $className ] = $filePath;
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
41        if(isset($classpath[ $class ])){
42                require_once(__DIR__ . $classpath[ $class ] );
43        }
44}
45
46// handle request, passing the current env baseUri and autoload mapping;
47
48$restConf = parse_ini_file( __DIR__ . '/../config/REST.ini', true );
49$request = new Request(array(
50        'baseUri'=> $restConf['baseUri'],
51        'autoload' => $autoload,
52));
53
54try {
55    $resource = $request->loadResource();
56    $response = $resource->exec($request);
57
58} catch (ResponseException $e) {
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);
79    }
80}
81
82$response->output();
83
84?>
Note: See TracBrowser for help on using the repository browser.