forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionManager.js
More file actions
79 lines (71 loc) · 1.9 KB
/
SessionManager.js
File metadata and controls
79 lines (71 loc) · 1.9 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
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const Drivers = require('./Drivers')
const Ioc = require('adonis-fold').Ioc
const Session = require('./index')
const CE = require('../Exceptions')
/**
* Makes driver instance from native or extended driver. Executes
* the callback when unable to find the specified driver.
*
* @param {String} driver
* @param {Object} drivers
* @param {Object} extendedDrivers
* @param {Function} callback
*
* @return {Object}
*
* @private
*/
const _makeDriverInstance = (driver, drivers, extendedDrivers, callback) => {
const driverInstance = drivers[driver] ? Ioc.make(drivers[driver]) : extendedDrivers[driver]
if (!driverInstance) {
callback()
}
return driverInstance
}
/**
* Session class for reading and writing sessions
* during http request
* @returns {Session}
* @class
*/
class SessionManager {
/**
* Extend session provider by adding a new named
* driver. This method is used the IoC container, so
* feel free to use Ioc.extend syntax.
*
* @param {String} key - name of the driver
* @param {Object} value - Driver implmentation
*
* @example
* Ioc.extend('Adonis/Src/Session', 'redis', (app) => {
* return new RedisImplementation()
* })
*/
static extend (key, value) {
this.drivers = this.drivers || {}
this.drivers[key] = value
}
/**
* @constructor
*/
constructor (Config) {
const driver = Config.get('session.driver')
this.constructor.drivers = this.constructor.drivers || {}
Session.driver = _makeDriverInstance(driver, Drivers, this.constructor.drivers, () => {
throw CE.RuntimeException.invalidSessionDriver(driver)
})
Session.config = Config
return Session
}
}
module.exports = SessionManager