1 | <?php |
---|
2 | ini_set('display_errors', 'on'); |
---|
3 | // load Tonic library |
---|
4 | require_once __DIR__ . '/../library/tonic/lib/tonic.php'; |
---|
5 | require_once __DIR__ . '/../api/controller.php'; |
---|
6 | require_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 | |
---|
22 | foreach( $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 |
---|
30 | function __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 | |
---|
43 | try { |
---|
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 | |
---|