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

Revision 6754, 1.8 KB checked in by niltonneto, 7 years ago (diff)

Ticket #0000 - Copiadas as alterações do Trunk. Versão final da 2.4.1.

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