-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathServiceContainer.php
More file actions
162 lines (131 loc) · 4.52 KB
/
ServiceContainer.php
File metadata and controls
162 lines (131 loc) · 4.52 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
use Slim\Slim;
use Slim\Views\Twig;
use Slim\Middleware\SessionCookie;
class Xhgui_ServiceContainer extends Pimple
{
protected static $_instance;
public static function instance()
{
if (empty(static::$_instance)) {
static::$_instance = new self();
}
return static::$_instance;
}
public function __construct()
{
$this->_slimApp();
$this->_services();
$this->_controllers();
}
// Create the Slim app.
protected function _slimApp()
{
$this['view'] = function ($c) {
$cacheDir = isset($c['config']['cache']) ? $c['config']['cache'] : XHGUI_ROOT_DIR . '/cache';
// Configure Twig view for slim
$view = new Twig();
$view->twigTemplateDirs = array(dirname(__DIR__) . '/templates');
$view->parserOptions = array(
'charset' => 'utf-8',
'cache' => $cacheDir,
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
return $view;
};
$this['app'] = $this->share(function ($c) {
if ($c['config']['timezone']) {
date_default_timezone_set($c['config']['timezone']);
}
$app = new Slim($c['config']);
// Enable cookie based sessions
$app->add(new SessionCookie(array(
'httponly' => true,
)));
// Add renderer.
$app->add(new Xhgui_Middleware_Render());
$view = $c['view'];
$view->parserExtensions = array(
new Xhgui_Twig_Extension($app)
);
$app->view($view);
return $app;
});
}
/**
* Add common service objects to the container.
*/
protected function _services()
{
$this['config'] = Xhgui_Config::all();
$this['db'] = $this->share(function ($c) {
$config = $c['config'];
if (empty($config['db.options'])) {
$config['db.options'] = array();
}
if (empty($config['db.driverOptions'])) {
$config['db.driverOptions'] = array();
}
$mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']);
$mongo->{$config['db.db']}->results->findOne();
return $mongo->{$config['db.db']};
});
$this['pdo'] = $this->share(function ($c) {
return new PDO(
$c['config']['pdo']['dsn'],
$c['config']['pdo']['pass'],
$c['config']['pdo']['user']
);
});
$this['searcher.mongo'] = function ($c) {
return new Xhgui_Searcher_Mongo($c['db']);
};
$this['searcher.pdo'] = function ($c) {
return new Xhgui_Searcher_Pdo($c['pdo'], $c['config']['pdo']['table']);
};
$this['searcher'] = function ($c) {
$config = $c['config'];
switch ($config['save.handler']) {
case 'pdo':
return $c['searcher.pdo'];
case 'mongodb':
default:
return $c['searcher.mongo'];
}
};
$this['saver.mongo'] = function ($c) {
$config = $c['config'];
$config['save.handler'] = 'mongodb';
return Xhgui_Saver::factory($config);
};
$this['saver'] = function ($c) {
return Xhgui_Saver::factory($c['config']);
};
}
/**
* Add controllers to the DI container.
*/
protected function _controllers()
{
$this['watchController'] = function ($c) {
return new Xhgui_Controller_Watch($c['app'], $c['searcher']);
};
$this['runController'] = function ($c) {
return new Xhgui_Controller_Run($c['app'], $c['searcher']);
};
$this['customController'] = function ($c) {
return new Xhgui_Controller_Custom($c['app'], $c['searcher']);
};
$this['waterfallController'] = function ($c) {
return new Xhgui_Controller_Waterfall($c['app'], $c['searcher']);
};
$this['importController'] = function ($c) {
return new Xhgui_Controller_Import($c['app'], $c['saver'], $c['config']['upload.token']);
};
$this['exportController'] = function ($c) {
return new Xhgui_Controller_Export($c['app'], $c['searcher']);
};
}
}