source: sandbox/2.4-expresso-api/prototype/rest/dispatch.php @ 6230

Revision 6230, 1.7 KB checked in by acoutinho, 9 years ago (diff)

Ticket #2758 - Implementacao dos recursos de contatos dinamicos no modelo de rest

  • Property svn:executable set to *
Line 
1<?php
2ini_set('display_errors', 'on');
3// load Tonic library
4require_once __DIR__ . '/../library/tonic/lib/tonic.php';
5require_once __DIR__ . '/../api/controller.php';
6require_once ROOTPATH . '/rest/oauth/OAuth2StorageUserCredential.php';
7
8
9//Retrieveing the mapping of the URIs and his respectives classNames and classPath
10$config = parse_ini_file( __DIR__ . '/../config/Tonic.srv', true );
11
12//looping through the mapping to create 2 separated maps:
13// First indexed by the uri that carry the classNames,
14// that its used by Tonic to autoload them when routed by him accordingly;
15// Second indexed by the className that carry the classPaths,
16// used by the autoload register to find the correct path of the class that's going to
17// be loaded;
18
19$autoload = array();
20$classpath = array();
21
22foreach( $config as $uri => $classFile )
23    foreach( $classFile as $className => $filePath )
24    {
25        $autoload[ $uri ] = $className;
26        $classpath[ $className ] = $filePath;
27    }
28
29//The autoload function that's called by the PHP when Tonic routes a class not declared previously
30function __autoload($class) {
31
32        global $classpath;
33
34        require_once(__DIR__ . $classpath[ $class ] );
35}
36
37// handle request, passing the current env baseUri and autoload mapping;
38$request = new Request(array(
39        'baseUri'=> '/acoutinho/expressoAPI/rest',
40        'autoload' => $autoload,
41));
42
43try {
44    $resource = $request->loadResource();
45    $response = $resource->exec($request);
46
47} catch (ResponseException $e) {
48    switch ($e->getCode()) {
49    case Response::UNAUTHORIZED:
50        $response = $e->response($request);
51        $response->addHeader('WWW-Authenticate', 'Basic realm="Tonic"');
52        break;
53    default:
54        $response = $e->response($request);
55    }
56}
57$response->output();
58
Note: See TracBrowser for help on using the repository browser.