1 | <?php |
---|
2 | // load Tonic library |
---|
3 | require_once __DIR__ . '/../library/tonic/lib/tonic.php'; |
---|
4 | require_once __DIR__ . '/../api/controller.php'; |
---|
5 | require_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 | |
---|
20 | foreach( $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 |
---|
29 | function __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 | |
---|
46 | try { |
---|
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 | |
---|