Skip to content

Commit

Permalink
Update application.js
Browse files Browse the repository at this point in the history
  • Loading branch information
amarksingh authored Jan 8, 2024
1 parent ae444f0 commit aff80fe
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('@ostro/support/helpers')
const path = require('path')
const fs = require('fs')
const ObjectSet = require('lodash.set')
const { IsClass, isFunction } = require('@ostro/support/function')
const { IsClass, isFunction, isset } = require('@ostro/support/function')
const ApplicationContract = require('@ostro/contracts/container/application')
const { Macroable } = require('@ostro/support/macro')

Expand All @@ -12,14 +12,20 @@ class Application extends Macroable.extend(ApplicationContract) {

$bindings = {};

$aliases = {};

$resolved = {};

$scopedInstances = {};

alias(alias = {}) {
for (let aliasKey in alias) {
global[aliasKey] = alias[aliasKey];
}
}

wrap($callback, $parameters = []) {
return function() {
return function () {
return this.apply($callback, $parameters);
};
}
Expand Down Expand Up @@ -48,7 +54,7 @@ class Application extends Macroable.extend(ApplicationContract) {
return this.resolve($abstract, $parameters, true);
}

whenHas(key, done = () => {}, error = () => {}) {
whenHas(key, done = () => { }, error = () => { }) {
key = this[key]
if (typeof key != undefined) {
done(key)
Expand Down Expand Up @@ -82,7 +88,8 @@ class Application extends Macroable.extend(ApplicationContract) {
$abstract = new $abstract(...$parameters)
}
}
this.$instances[key] = $abstract
this.$instances[key] = $abstract;
$abstract;
}
} else {
if ($make) {
Expand All @@ -92,9 +99,9 @@ class Application extends Macroable.extend(ApplicationContract) {
}

}
if ($make) {
$abstract = this.build($abstract, $parameters)
}

$abstract = this.build($abstract, $parameters);

return typeof $abstract == 'object' ? $abstract : undefined

}
Expand All @@ -110,7 +117,7 @@ class Application extends Macroable.extend(ApplicationContract) {
build($concrete, $parameters = []) {

if (this.isBuildable($concrete)) {
if (typeof $concrete.prototype.$app == 'undefined') {
if ($concrete.prototype && typeof $concrete.prototype.$app == 'undefined') {
$concrete.prototype.$app = this
}
if (IsClass($concrete)) {
Expand All @@ -128,6 +135,13 @@ class Application extends Macroable.extend(ApplicationContract) {
return this.$bindings;
}

getAlias($abstract) {
return isset(this.$aliases[$abstract])
? this.getAlias(this.$aliases[$abstract])
: $abstract;
}


bind(key, callback, $parameters = [], shared = false) {
if (typeof key == 'string' && !callback) {
callback = require(key)
Expand All @@ -153,6 +167,10 @@ class Application extends Macroable.extend(ApplicationContract) {
return callback
}

bound(key) {
return this.$bindings[key] && this.$bindings[key].shared == false
}

singleton($abstract, $concrete, ...$parameters) {
return this.bind($abstract, $concrete, $parameters, true);
}
Expand All @@ -166,6 +184,41 @@ class Application extends Macroable.extend(ApplicationContract) {

}

flush() {
this.$aliases = {};
this.$resolved = {};
this.$bindings = {};
this.$instances = {};
this.$scopedInstances = {};
}

forgetInstance($abstract) {
delete this.$instances[$abstract];
}


forgetInstances() {
this.$instances = {};
}

forgetScopedInstances() {
for (let $scoped in this.$scopedInstances) {
delete this.$instances[$scoped];
}
}

getInstance() {
if (is_null(this.$instance)) {
this.$instance = new this.constructor;
}

return this.$instance;
}

setInstance($container = null) {
return this.$instance = $container;
}

app(key) {
return key ? this[key] : this;
}
Expand All @@ -187,10 +240,15 @@ class Application extends Macroable.extend(ApplicationContract) {

return this.$instances[$abstract];
}
__get(target, method) {
return target.resolve(method)

__get(target, key) {
return target.resolve(key)
}

__set(target, key, value) {
target[key] = value;
}

}

module.exports = Application
module.exports = Application

0 comments on commit aff80fe

Please sign in to comment.