-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsessionManager.js
More file actions
82 lines (66 loc) · 2.43 KB
/
sessionManager.js
File metadata and controls
82 lines (66 loc) · 2.43 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
require('@ostro/support/helpers')
const SessionStarter = require('./sessionStarter')
const InvalidCookieClassException = require('./invalidCookieClassException')
const CookieSession = require('./cookieSession')
const SessionStore = require('./sessionStore')
const Manager = require('@ostro/support/manager')
class SessionManager extends Manager {
$type = 'session';
getSessionConfig() {
return this.$config[this.$type]
}
config() {
let sessionConfig = this.getSessionConfig()
return {
...sessionConfig,
key: this.$config['app']['key'] || 'a@jf#H&5@6*1jV(>df6d!fk8^6g56%tt+b4'
}
}
start() {
return this.startSession(new SessionStarter(this.config(), new CookieSession(this.$container, this.config(), this.driver()), this.driver()))
}
startSession(session) {
return (request, response, next) => {
session.start(request, response, next)
}
}
resolve(name) {
var config = this.getSessionConfig();
if ((this.$customCreators[this.getDriverName()])) {
return this.callCustomCreator(config, name);
}
var driverName = this.getDriverName().split('.')
var driverMethod = 'create' + driverName[0].ucfirst() + 'Driver';
if (isset(this[driverMethod])) {
return this[driverMethod](config, driverName);
} else {
throw new Error(`Driver [${this.getDriverName()}] is not supported.`);
}
}
callCustomCreator($config, name) {
var driver = this.$customCreators[this.getDriverName()](this.$constraints, name, $config);
return this.adapt($driver);
}
createMemoryDriver($config) {
return this.adapt((new(require('./adapter/memory'))($config['files'])));
}
createFileDriver($config) {
return this.adapt((new(require('./adapter/file'))($config['files'])))
}
createDatabaseDriver($config) {
return this.adapt((new(require('./adapter/database'))(this.$container.db, $config['table'])))
}
createCacheDriver($config, driver) {
return this.adapt((new(require('./adapter/cache'))(this.$container.cache.driver(driver[1]), $config)))
}
adapt(session) {
return new SessionStore(session);
}
getDriverName() {
return super.getConfig('driver');
}
getDefaultDriver() {
return this.getDriverName();
}
}
module.exports = SessionManager