1
|
|
|
<?php |
2
|
|
|
use Slim\Slim; |
3
|
|
|
use Slim\Views\Twig; |
4
|
|
|
use Slim\Middleware\SessionCookie; |
5
|
|
|
|
6
|
|
|
class Xhgui_ServiceContainer extends Pimple |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected static $_instance; |
9
|
|
|
|
10
|
|
|
public static function instance() |
11
|
|
|
{ |
12
|
|
|
if (empty(static::$_instance)) { |
13
|
|
|
static::$_instance = new self(); |
14
|
|
|
} |
15
|
|
|
return static::$_instance; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->_slimApp(); |
21
|
|
|
$this->_services(); |
22
|
|
|
$this->_controllers(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Create the Slim app. |
26
|
|
|
protected function _slimApp() |
27
|
|
|
{ |
28
|
|
|
$this['view'] = function ($c) { |
29
|
|
|
$cacheDir = isset($c['config']['cache']) ? $c['config']['cache'] : XHGUI_ROOT_DIR . '/cache'; |
30
|
|
|
|
31
|
|
|
// Configure Twig view for slim |
32
|
|
|
$view = new Twig(); |
33
|
|
|
|
34
|
|
|
$view->parserOptions = array( |
35
|
|
|
'charset' => 'utf-8', |
36
|
|
|
'cache' => $cacheDir, |
37
|
|
|
'auto_reload' => true, |
38
|
|
|
'strict_variables' => false, |
39
|
|
|
'autoescape' => true |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
return $view; |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
$this['app'] = $this->share(function ($c) { |
46
|
|
|
$app = new Slim($c['config']); |
47
|
|
|
|
48
|
|
|
// Enable cookie based sessions |
49
|
|
|
$app->add(new SessionCookie(array( |
50
|
|
|
'httponly' => true, |
51
|
|
|
))); |
52
|
|
|
|
53
|
|
|
// Add renderer. |
54
|
|
|
$app->add(new Xhgui_Middleware_Render()); |
55
|
|
|
|
56
|
|
|
$view = $c['view']; |
57
|
|
|
$view->parserExtensions = array( |
58
|
|
|
new Xhgui_Twig_Extension($app) |
59
|
|
|
); |
60
|
|
|
$app->view($view); |
61
|
|
|
|
62
|
|
|
return $app; |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add common service objects to the container. |
68
|
|
|
*/ |
69
|
|
|
protected function _services() |
70
|
|
|
{ |
71
|
|
|
$this['config'] = Xhgui_Config::all(); |
72
|
|
|
|
73
|
|
|
$this['db'] = $this->share(function ($c) { |
74
|
|
|
$config = $c['config']; |
75
|
|
|
if (empty($config['db.options'])) { |
76
|
|
|
$config['db.options'] = array(); |
77
|
|
|
} |
78
|
|
|
$mongo = new MongoClient($config['db.host'], $config['db.options']); |
79
|
|
|
$mongo->{$config['db.db']}->results->findOne(); |
80
|
|
|
|
81
|
|
|
return $mongo->{$config['db.db']}; |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$this['pdo'] = $this->share(function ($c) { |
85
|
|
|
return new PDO( |
86
|
|
|
$c['config']['pdo']['dsn'], |
87
|
|
|
$c['config']['pdo']['pass'], |
88
|
|
|
$c['config']['pdo']['user'] |
89
|
|
|
); |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$this['watchFunctions'] = function ($c) { |
93
|
|
|
return new Xhgui_WatchFunctions($c['db']); |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$this['searcher.mongo'] = function ($c) { |
97
|
|
|
return new Xhgui_Searcher_Mongo($c['db']); |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
$this['saver'] = function($c) { |
101
|
|
|
return Xhgui_Saver::factory($c['config']); |
102
|
|
|
}; |
103
|
|
|
|
104
|
|
|
$this['searcher.pdo'] = function ($c) { |
105
|
|
|
return new Xhgui_Searcher_Pdo($c['pdo'], $c['config']['pdo']['table']); |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
$this['searcher'] = function ($c) { |
109
|
|
|
$config = $c['config']; |
110
|
|
|
|
111
|
|
|
switch ($config['save.handler']) { |
112
|
|
|
case 'pdo': |
113
|
|
|
return $c['searcher.pdo']; |
114
|
|
|
|
115
|
|
|
case 'mongodb': |
116
|
|
|
default: |
117
|
|
|
return $c['searcher.mongo']; |
118
|
|
|
} |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
$this['saverMongo'] = function($c) { |
122
|
|
|
$config = $c['config']; |
123
|
|
|
$config['save.handler'] = 'mongodb'; |
124
|
|
|
|
125
|
|
|
return Xhgui_Saver::factory($config); |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add controllers to the DI container. |
131
|
|
|
*/ |
132
|
|
|
protected function _controllers() |
133
|
|
|
{ |
134
|
|
|
$this['watchController'] = function ($c) { |
135
|
|
|
return new Xhgui_Controller_Watch($c['app'], $c['searcher']); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
$this['runController'] = function ($c) { |
139
|
|
|
return new Xhgui_Controller_Run($c['app'], $c['searcher']); |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
$this['customController'] = function ($c) { |
143
|
|
|
return new Xhgui_Controller_Custom($c['app'], $c['searcher']); |
144
|
|
|
}; |
145
|
|
|
|
146
|
|
|
$this['waterfallController'] = function ($c) { |
147
|
|
|
return new Xhgui_Controller_Waterfall($c['app'], $c['searcher']); |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
$this['importController'] = function ($c) { |
151
|
|
|
return new Xhgui_Controller_Import($c['app'], $c['saverMongo']); |
152
|
|
|
}; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.