-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanager.js
106 lines (84 loc) · 2.64 KB
/
manager.js
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
const { Macroable } = require('./macro')
const InvalidArgumentException = require('./exceptions/invalidArgumentException')
const lodash = require('lodash')
class Manager extends Macroable {
constructor(application) {
super()
let $container = application || app()
Object.defineProperties(this, {
'$container': {
value: $container,
writable: true
},
'$config': {
value: $container.make('config'),
writable: true
},
'$customCreators': {
value: {},
writable: true
},
'$drivers': {
value: {},
writable: true
}
})
}
driver($driver = null) {
$driver = $driver || this.getDefaultDriver();
if (is_null($driver)) {
throw new InvalidArgumentException(`Unable to resolve NULL driver for [${this.constructor.name}].`);
}
if (!isset(this.$drivers[$driver])) {
this.$drivers[$driver] = this.getDriver($driver);
}
return this.$drivers[$driver];
}
getDriver(name) {
return this.resolve(name);
}
resolve($name, $config = {}) {
if (isset(this.$customCreators[$config['driver']])) {
return this.callCustomCreator($config, $name);
}
let $driverMethod = 'create' + String.pascal($config['driver']) + 'Driver';
if (method_exists(this, $driverMethod)) {
return this[$driverMethod]($config, $name);
}
throw new InvalidArgumentException("Driver [" + $config['driver'] + "] not supported.");
}
callCustomCreator($config, $name) {
return this.$customCreators[$config['driver']].call(this, this.$container, $name, $config );
}
extend($driver, $callback) {
this.$customCreators[$driver] = $callback;
return this;
}
getDrivers() {
return this.$drivers;
}
getContainer() {
return this.$container;
}
setContainer($container) {
this.$container = $container;
return this;
}
forgetDrivers() {
this.$drivers = {};
return this;
}
setDefaultDriver(name) {
lodash.set(this.$config,`${this.$type}.default`,name)
}
getDefaultDriver() {
return lodash.get(this.$config,`${this.$type}.default`);
}
getConfig(name,defaultValue) {
return lodash.get(this.$config,`${this.$type}.${name}`, defaultValue)
}
__get(target, $method) {
return this.make(target.driver(), $method);
}
}
module.exports = Manager