-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.php
87 lines (66 loc) · 2.35 KB
/
dispatch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once '../lib/loader.php';
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$container = new Pimple();
// mongodb configuration
$container['connection_url'] = "mongodb://username:password@hostname:12345/databasename";
// log configuration
$container['log_dateformat'] = "Y-m-d H:i:s";
$container['log_logformat'] = "[%datetime%] %channel%.%level_name%: %message% %context%\n";
$container['log_file'] = "/tmp/application.log";
$container['log_appender'] = "appname";
// smarty configuration
$container['smarty_template_dir'] = '../views';
$container['smarty_compile_dir'] = sys_get_temp_dir();
// mongodb collections
$container['dbcoll'] = array(
"test" => "test"
);
$container['smarty'] = function($c){
$smarty = new Smarty();
$smarty->setTemplateDir($c['smarty_template_dir']);
$smarty->setCompileDir($c['smarty_compile_dir']);
$smarty->error_reporting = E_ALL & ~E_NOTICE;
return $smarty;
};
$container['log'] = function($c){
$logger = new Monolog\Logger($c['log_appender']);
$formatter = new Monolog\Formatter\LineFormatter($c['log_logformat'], $c['log_dateformat']);
$stream = new Monolog\Handler\StreamHandler($c['log_file'], Monolog\Logger::DEBUG);
$stream->setFormatter($formatter);
$logger->pushHandler($stream);
return $logger;
};
$container['database'] = function($c) {
$m = new Mongo($c["connection_url"]);
$url = parse_url($c["connection_url"]);
$db_name = preg_replace('/\/(.*)/', '$1', $url['path']);
$db = $m->selectDB($db_name);
return $db;
};
include_once '../src/BaseResource.php';
$config = array(
'load' => array(
'../src/webapp/*',
'../src/services/*'
),
#'cache' => new Tonic\MetadataCacheFile('/tmp/tonic.cache') // use the metadata cache
#'cache' => new Tonic\MetadataCacheAPC // use the metadata cache
);
$app = new Tonic\Application($config);
$request = new Tonic\Request();
try {
$resource = $app->getResource($request);
$resource->container = $container;
$response = $resource->exec();
} catch (Tonic\NotFoundException $e) {
$response = new Tonic\Response(404, $e->getMessage());
} catch (Tonic\UnauthorizedException $e) {
$response = new Tonic\Response(401, $e->getMessage());
$response->wwwAuthenticate = 'Basic realm="appname"';
} catch (Tonic\Exception $e) {
$response = new Tonic\Response($e->getCode(), $e->getMessage());
}
$response->output();
?>