Skip to content

Commit

Permalink
Merge pull request #1 from amarksingh/master
Browse files Browse the repository at this point in the history
manager improvment
  • Loading branch information
amarksingh authored Dec 17, 2021
2 parents dff45d1 + 5495cff commit 8cced39
Showing 1 changed file with 17 additions and 50 deletions.
67 changes: 17 additions & 50 deletions sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,50 @@ const SessionStarter = require('./sessionStarter')
const InvalidCookieClassException = require('./invalidCookieClassException')
const CookieSession = require('./cookieSession')
const SessionStore = require('./sessionStore')
const kApp = Symbol('app')
const kDrivers = Symbol('drivers')
const kCustomCreators = Symbol('customCreators')
class SessionManager {
const Manager = require('@ostro/support/manager')

constructor(app) {
Object.defineProperties(this, {
[kApp]: {
value: app,
},
[kDrivers]: {
value: {},
writable: true,
},
[kCustomCreators]: {
value: {},
writable: true,
}
})
}
class SessionManager extends Manager {

driver() {
return this.get(this.getDriverName())
}
$type = 'session';

getSessionConfig() {
return this[kApp]['config']['session']
return this.$config[this.$type]
}

config() {
let sessionConfig = this.getSessionConfig()
return {
...sessionConfig,
key: this[kApp]['config']['app']['key'] || 'a@jf#H&5@6*1jV(>df6d!fk8^6g56%tt+b4'
key: this.$config['app']['key'] || 'a@jf#H&5@6*1jV(>df6d!fk8^6g56%tt+b4'
}
}

get(name) {
return this[kDrivers][name] || this.resolve(name);
}

start() {
return this.startSession(new SessionStarter(this.config(), new CookieSession(this[kApp], this.config(), this.driver()), this.driver()))
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[kCustomCreators][this.getDriverName()])) {
return this.callCustomCreator(config);
if ((this.$customCreators[this.getDriverName()])) {
return this.callCustomCreator(config, name);
}
var driverName = this.getDriverName().split('.')
var driverMethod = 'create' + driverName[0].ucfirst() + 'Driver';
if (this[driverMethod]) {
if (isset(this[driverMethod])) {
return this[driverMethod](config, driverName);
} else {
throw new Error(`Driver [${this.getDriverName()}] is not supported.`);
}
}

callCustomCreator($config) {
var driver = this[kCustomCreators][this.getDriverName()](this[kApp]['config'], $config);
callCustomCreator($config, name) {
var driver = this.$customCreators[this.getDriverName()](this.$constraints, name, $config);
return this.adapt($driver);
}

Expand All @@ -83,32 +59,23 @@ class SessionManager {
}

createDatabaseDriver($config) {
return this.adapt((new(require('./adapter/database'))(this[kApp].database, $config['table'])))
return this.adapt((new(require('./adapter/database'))(this.$container.database, $config['table'])))
}

createCacheDriver($config, driver) {
return this.adapt((new(require('./adapter/cache'))(this[kApp].cache.driver(driver[1]), $config)))
return this.adapt((new(require('./adapter/cache'))(this.$container.cache.driver(driver[1]), $config)))
}

adapt(session) {
return new SessionStore(session);
}

createSession(adapter) {
return adapter;
}

set($name, $driver) {
this[kDrivers][$name] = $driver;
return this;
}

getConfig(name) {
return this.getSessionConfig()[name];
getDriverName() {
return super.getConfig('driver');
}

getDriverName() {
return this.getSessionConfig()['driver'];
getDefaultDriver() {
return this.getDriverName();
}
}

Expand Down

0 comments on commit 8cced39

Please sign in to comment.