-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicocontainer.js
125 lines (86 loc) · 3.35 KB
/
picocontainer.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 1 что такое адапптеры
* фабрики объектов - с помоцью которых объекты и создаются
* могут объединяться в цепочки
* Caching -> Constructor Injection
* 2 Как работает constructor DI в PHP
* создаётся объект через ComponentAdapter,
* также вычисляются типы зависимостей в конструкторе компонента
* в JS такое не пройдёт так как нет типов параметров
*
* 3 Как работает ComponentAdapter в PHP
*
* 4 Список ComponentAdapter
*
*/
/*
PicoContainer
ComponentAdapter
function Apple(){}
function Banana(){}
function Store(apple,banana){
this._apple = apple;
this._banana = banana;
}
pico = new PicoContainer();
pico.addComponent('Apple');
pico.addComponent('Banana');
pico.addComponent('Store',Store,[Apple,Banana]);
pico.addComponent(Store,[Instance(Apple),Instance("Banana"), "some data", 10, function(){}]);
pico.addComponent(Caching(ConstructorInjection('logger',function(fileName,logLevel){
var log4js = require('log4js')();
log4js.addAppender(log4js.fileAppender(fileName), 'mylogger');
var logger = log4js.getLogger('mylogger');
logger.setLevel(logLevel);
return logger;
},['./mylog.log','ERROR']));
pico.getComponent(Store);
addComponentImpl(key, class, dependencies)
getComponent(Foo);
Foo.setName = function(){};
pico.addComponentImpl('Name',Name);
pico.addComponent(new SetterInjection('Foo',Foo,[Name]));
*/
var ca = require('./componentadapter'),
Interface = require('./interface');
function PicoContainer(componentAdapterFactory){
this._componentAdapters = {};
this._componentAdapterFactory = componentAdapterFactory;
if(! this._componentAdapterFactory){
this._componentAdapterFactory = new ca.ConstructorInjectionFactory();
}
};
module.exports.PicoContainer = PicoContainer;
PicoContainer.prototype.addAdapter = function(componentAdapter){
if(! componentAdapter){
throw Error("Required parameter component adapter.");
}
var componentKey = componentAdapter.getComponentKey();
if(this._componentAdapters[componentKey]){
throw Error("Component key '" + componentKey + "' already registered.");
}
this._componentAdapters[componentKey] = componentAdapter;
};
PicoContainer.prototype.addComponent = function(componentKey, componentImpl, dependencies){
this.addAdapter(this._componentAdapterFactory.createComponentAdapter(componentKey, componentImpl, dependencies));
};
PicoContainer.prototype.addComponentInstance = function(componentKey, componentInstance){
this.addAdapter(new ca.InstanceAdapter(componentKey, componentInstance));
};
PicoContainer.prototype.removeComponent = function(componentKey){
delete this._componentAdapters[componentKey];
};
PicoContainer.prototype.getComponent = function(componentKey){
var componentAdapter = this.getComponentAdapter(componentKey);
return componentAdapter ? this.getComponentFromAdapter(componentAdapter) : null;
};
PicoContainer.prototype.getComponentAdapter = function(componentKey){
if(componentKey && this._componentAdapters[componentKey]){
return this._componentAdapters[componentKey];
}
};
PicoContainer.prototype.getComponentFromAdapter = function(componentAdapter){
if(componentAdapter){
return componentAdapter.getComponentInstance(this);
}
};