Skip to content

Commit

Permalink
refactor: make service providers accept a 'config' parameter in their…
Browse files Browse the repository at this point in the history
… constructor
  • Loading branch information
simplymichael committed Jun 14, 2024
1 parent 55f0e53 commit fc84e1e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/service-providers/app-service-provider.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const config = require("../config");
const createObjectStore = require("../framework/component/registry");
const ServiceProvider = require("../framework/component/service-provider");
const ServiceProvider = require("./service-provider");


/*
* Extending the parent `ServiceContainer` gives us access to
* the DI Container via `this.container()` method.
*/
class AppServiceProvider extends ServiceProvider {
constructor() {
super();
constructor(config) {
super(config);
}

/**
* Register the service's dependencies in the dependency container.
*/
register() {
const container = this.container();
const config = this.config();

container.bindWithFunction("config", function configGetter() {
return config;
Expand Down
68 changes: 68 additions & 0 deletions src/service-providers/app-service-provider.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-env node, mocha */

const { chai } = require("../lib/test-helper");
const AppServiceProvider = require("./app-service-provider");

let expect;
let sp;
const config = {
app: {
name: "Simple Framework",
version: "1.0.0",
},
};

module.exports = {
constructor() {
describe("constructor(config)", function() {
before(async function() {
expect = (await chai()).expect;
sp = new AppServiceProvider(config);
});

it("should initialize and return an object", function() {
expect(sp).to.be.an("object");
});

describe("provider object", function() {
it("should have a 'container' method that returns the service container", function() {
expect(sp).to.be.an("object");
expect(sp).to.have.property("container").to.be.a("function");

for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) {
expect(sp.container()).to.have.property(method).to.be.a("function");
}
});

it("should have a 'config' method that returns the passed 'config'", function() {
expect(sp).to.be.an("object");
expect(sp).to.have.property("config").to.be.a("function");
expect(sp.config()).to.deep.equal(config);
});

it("should have a 'register' method that binds values into the container", function() {
expect(sp).to.be.an("object");
expect(sp).to.have.property("register").to.be.a("function");
});

describe(".register()", function() {
it("should bind the 'config' and 'registry' into the service container", function() {
//expect(() => sp.container().resolve("config")).to.throw(/Could not resolve 'config'/);
//expect(() => sp.container().resolve("registry")).to.throw(/Could not resolve 'registry'/);

sp.register();

expect(sp.container().resolve("config")).to.deep.equal(config);

const registry = sp.container().resolve("registry");
const registryMethods = ["add", "contains", "get", "remove"];

for(const method of registryMethods) {
expect(registry).to.have.property(method).to.be.a("function");
}
});
});
});
});
}
};
8 changes: 4 additions & 4 deletions src/service-providers/cache-service-provider.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const RedisStore = require("../framework/component/connector/redis");
const ServiceProvider = require("../framework/component/service-provider");
const CacheFactory = require("../framework/factory/cache");
const ServiceProvider = require("./service-provider");


module.exports = class CacheServiceProvider extends ServiceProvider {
constructor() {
super();
constructor(config) {
super(config);
}

register() {
const container = this.container();
const config = container.resolve("config");
const config = this.config() ?? container.resolve("config");
const cacheConfig = config.get("cache");
const defaultCache = cacheConfig.default;
const defaultCacheData = cacheConfig.stores[defaultCache];
Expand Down
17 changes: 17 additions & 0 deletions src/service-providers/service-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const FrameworkServiceProvider = require("../framework/component/service-provider");

class ServiceProvider extends FrameworkServiceProvider {
#config;

constructor(config) {
super();

this.#config = config;
}

config() {
return this.#config;
}
}

module.exports = ServiceProvider;
46 changes: 46 additions & 0 deletions src/service-providers/service-provider.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-env node, mocha */

const { chai } = require("../lib/test-helper");
const ServiceProvider = require("./service-provider");

let expect;
let sp;
const config = {
app: {
name: "Simple Framework",
version: "1.0.0",
},
};


module.exports = {
constructor() {
describe("constructor(config)", function() {
before(async function() {
expect = (await chai()).expect;
sp = new ServiceProvider(config);
});

it("should initialize and return an object", function() {
expect(sp).to.be.an("object");
});

describe("provider object", function() {
it("should have a 'container' method that returns the service container", function() {
expect(sp).to.be.an("object");
expect(sp).to.have.property("container").to.be.a("function");

for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) {
expect(sp.container()).to.have.property(method).to.be.a("function");
}
});

it("should have a 'config' method that returns the passed 'config'", function() {
expect(sp).to.be.an("object");
expect(sp).to.have.property("config").to.be.a("function");
expect(sp.config()).to.deep.equal(config);
});
});
});
}
};

0 comments on commit fc84e1e

Please sign in to comment.