This repository has been archived by the owner on Aug 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Module.php
88 lines (75 loc) · 2.5 KB
/
Module.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
88
<?php
namespace Eva\EvaUser;
use Eva\EvaEngine\Engine;
use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Eva\EvaEngine\Module\StandardInterface;
use Eva\EvaUser\Models\Login;
use Eva\EvaEngine\Exception;
class Module implements ModuleDefinitionInterface, StandardInterface
{
public static function registerGlobalAutoloaders()
{
return array(
'Eva\EvaUser' => __DIR__ . '/src/EvaUser',
);
}
public static function registerGlobalEventListeners()
{
return array(
'user' => 'Eva\EvaUser\Events\UserListener',
);
}
public static function registerGlobalViewHelpers()
{
}
public static function registerGlobalRelations()
{
}
/**
* Registers the module auto-loader
*/
public function registerAutoloaders()
{
}
/**
* Registers the module-only services
*
* @param \Phalcon\DiInterface $di
*/
public function registerServices($di)
{
$dispatcher = $di->getDispatcher();
$dispatcher->setDefaultNamespace('Eva\EvaUser\Controllers');
static::registerGlobalServices($di);
}
/**
* 注册全局的服务,需在 app 或者 其他 module 中的 registerServices 手动调用。
*
* @param \Phalcon\DiInterface $di
*/
public static function registerGlobalServices($di)
{
$config = $di->getConfig();
// 当用户系统和
if (!empty($config->user->dbAdapter)) {
$di->set('userDbMaster', function () use ($di, $config) {
$slaves = $config->user->dbAdapter->slave;
$slaveKey = array_rand($slaves->toArray());
if (!isset($slaves->$slaveKey) || count($slaves) < 1) {
throw new Exception\RuntimeException(sprintf('No DB slave options found'));
}
return Engine::diDbAdapter($slaves->$slaveKey->adapter, $slaves->$slaveKey->toArray(), $di);
});
$di->set('userDbSlave', function () use ($di, $config) {
$slaves = $config->user->dbAdapter->slave;
$slaveKey = array_rand($slaves->toArray());
if (!isset($slaves->$slaveKey) || count($slaves) < 1) {
throw new Exception\RuntimeException(sprintf('No DB slave options found'));
}
return Engine::diDbAdapter($slaves->$slaveKey->adapter, $slaves->$slaveKey->toArray(), $di);
});
}
}
}