source: sandbox/webservice/api/rest/dispatch.php @ 6165

Revision 6165, 2.1 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Refatorado e centralizado tratamento de Erros.

  • Property svn:executable set to *
Line 
1<?php
2
3// load libraries
4require_once(__DIR__.'/../library/tonic/lib/tonic.php');
5require_once(__DIR__.'/../library/utils/Errors.php');
6
7// load adapters
8require_once(__DIR__."/../adapters/ExpressoAdapter.php");
9require_once(__DIR__."/../adapters/MailAdapter.php");
10require_once(__DIR__."/../adapters/CatalogAdapter.php");
11
12// Load all resources
13require_once(__DIR__."/core/ExpressoVersionResource.php");
14require_once(__DIR__."/core/LoginResource.php");
15require_once(__DIR__."/core/LogoutResource.php");
16require_once(__DIR__."/mail/FoldersResource.php");
17require_once(__DIR__."/mail/MessagesResource.php");
18require_once(__DIR__."/mail/AddFolderResource.php");
19require_once(__DIR__."/mail/DelFolderResource.php");
20require_once(__DIR__."/mail/RenameFolderResource.php");
21require_once(__DIR__."/mail/CleanTrashResource.php");
22require_once(__DIR__."/mail/SendSupportFeedbackResource.php");
23require_once(__DIR__."/catalog/ContactsResource.php");
24require_once(__DIR__."/catalog/ContactPictureResource.php");
25
26//Retrieveing the mapping of the URIs and his respectives classNames and classPath
27$config = parse_ini_file( __DIR__ . '/../config/Tonic.srv', true );
28$autoload = array();
29$classpath = array();
30
31foreach( $config as $uri => $classFile )
32        foreach( $classFile as $className => $filePath )
33        {
34                $autoload[ $uri ] = $className;
35                $classpath[ $className ] = $filePath;
36        }
37
38        $request = new Request(array(
39                        'baseUri' => dirname($_SERVER['SCRIPT_NAME']),
40                        'autoload' => $autoload
41        ));
42       
43try {
44        $resource = $request->loadResource();
45        $response = $resource->exec($request);
46       
47} catch (ResponseException $e) {
48       
49        switch ($e->getCode()) {
50               
51                case Response::UNAUTHORIZED:
52                        $response = $e->response($request);
53                        $response->addHeader('WWW-Authenticate', 'Basic realm="Tonic"');
54                        break;
55                default:
56                       
57                        $response = new Response($request);
58                        $response->code = Response::OK;
59                        $response->addHeader('content-type', 'application/json');
60                        if($request->id)
61                                $body['id']     = $request->id;
62                       
63                        $body['error'] = array("code" => "".$e->getCode(), "message" => $e->getMessage());
64                        $response->body = json_encode($body);
65        }
66}
67
68$response->output();
69
70?>
Note: See TracBrowser for help on using the repository browser.