source: sandbox/2.4.3-expresso-rest/prototype/rest/dispatch.php @ 7322

Revision 7322, 2.5 KB checked in by alexandrecorreia, 12 years ago (diff)

Ticket #3093 - Integração da API REST, arquivo dispatch.php.

  • 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//require_once(__DIR__."/../adapters/testeAdapter.php");
14
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
28foreach( $config as $uri => $classFile )
29{
30    foreach( $classFile as $className => $filePath )
31    {
32                $autoload[ $uri ] = $className;
33                $classpath[ $className ] = $filePath;
34    }
35}
36
37//The autoload function that's called by the PHP when Tonic routes a class not declared previously
38function __autoload($class) {
39
40        global $classpath;
41
42        if(isset($classpath[ $class ])){
43                require_once(__DIR__ . $classpath[ $class ] );
44        }
45}
46
47// handle request, passing the current env baseUri and autoload mapping;
48
49$restConf = parse_ini_file( __DIR__ . '/../config/REST.ini', true );
50$request = new Request(array(
51        'baseUri'=> $restConf['baseUri'],
52        'autoload' => $autoload,
53));
54
55try {
56    $resource = $request->loadResource();
57    $response = $resource->exec($request);
58
59} catch (ResponseException $e) {
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);
80    }
81}
82
83$response->output();
84
85?>
Note: See TracBrowser for help on using the repository browser.