|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\Delegate; |
|
3
|
|
|
|
|
4
|
|
|
use WebStream\Core\CoreView; |
|
5
|
|
|
use WebStream\Module\Utility\ApplicationUtils; |
|
6
|
|
|
use WebStream\Module\Utility\CommonUtils; |
|
7
|
|
|
use WebStream\Module\Container; |
|
8
|
|
|
use WebStream\Module\ClassLoader; |
|
9
|
|
|
use WebStream\Exception\Extend\ClassNotFoundException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* CoreDelegator |
|
13
|
|
|
* @author Ryuichi TANAKA. |
|
14
|
|
|
* @since 2011/11/30 |
|
15
|
|
|
* @version 0.7 |
|
16
|
|
|
*/ |
|
17
|
|
|
class CoreDelegator |
|
18
|
|
|
{ |
|
19
|
|
|
use CommonUtils; |
|
20
|
|
|
use ApplicationUtils |
|
21
|
|
|
{ |
|
22
|
|
|
ApplicationUtils::getNamespace as getDefinedNamespace; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Container DIコンテナ |
|
27
|
|
|
*/ |
|
28
|
|
|
private $container; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Logger ロガー |
|
32
|
|
|
*/ |
|
33
|
|
|
private $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Container Coreレイヤコンテナ |
|
37
|
|
|
*/ |
|
38
|
|
|
private $coreContainer; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* @param Container 依存コンテナ |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Container $container) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->container = $container; |
|
47
|
|
|
$this->logger = $container->logger; |
|
|
|
|
|
|
48
|
|
|
$this->coreContainer = new Container(); |
|
49
|
|
|
$this->initialize(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Destructor |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __destruct() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->coreContainer->remove("controller"); |
|
58
|
|
|
$this->coreContainer->remove("view"); |
|
59
|
|
|
$this->coreContainer->remove("service"); |
|
60
|
|
|
$this->coreContainer->remove("model"); |
|
61
|
|
|
$this->coreContainer->remove("helper"); |
|
62
|
|
|
$this->logger->debug("CoreDelegator container is clear."); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* 各レイヤのオブジェクトをコンテナに設定する |
|
67
|
|
|
*/ |
|
68
|
|
|
private function initialize() |
|
69
|
|
|
{ |
|
70
|
|
|
$container = $this->container; |
|
71
|
|
|
$classLoader = new ClassLoader(); |
|
72
|
|
|
$classLoader->inject('logger', $container->logger) |
|
|
|
|
|
|
73
|
|
|
->inject('applicationInfo', $container->applicationInfo); |
|
|
|
|
|
|
74
|
|
|
$pageName = $this->getPageName(); |
|
75
|
|
|
$serviceClassName = $pageName . "Service"; |
|
76
|
|
|
$modelClassName = $pageName . "Model"; |
|
77
|
|
|
$helperClassName = $pageName . "Helper"; |
|
78
|
|
|
$appRoot = $container->applicationInfo->applicationRoot . "/app"; |
|
|
|
|
|
|
79
|
|
|
$controllerNamespace = $this->getNamespace($appRoot, $container->router->controller); |
|
|
|
|
|
|
80
|
|
|
$serviceNamespace = $this->getNamespace($appRoot, $serviceClassName); |
|
81
|
|
|
$modelNamespace = $this->getNamespace($appRoot, $modelClassName); |
|
82
|
|
|
$helperNamespace = $this->getNamespace($appRoot, $helperClassName); |
|
83
|
|
|
|
|
84
|
|
|
// Controller |
|
85
|
|
|
$this->coreContainer->controller = function () use ($container, $controllerNamespace) { |
|
|
|
|
|
|
86
|
|
|
$controllerClassPath = $controllerNamespace . "\\" . $container->router->controller; |
|
|
|
|
|
|
87
|
|
|
if (!class_exists($controllerClassPath)) { |
|
88
|
|
|
throw new ClassNotFoundException("Undefined class path: " . $controllerClassPath); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return new $controllerClassPath($container); |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
// View |
|
95
|
|
|
$this->coreContainer->view = function () use ($container) { |
|
|
|
|
|
|
96
|
|
|
return new CoreView($container); |
|
97
|
|
|
}; |
|
98
|
|
|
|
|
99
|
|
|
// Service |
|
100
|
|
|
if ($serviceNamespace !== null) { |
|
101
|
|
|
$serviceClassPath = $serviceNamespace . "\\" . $serviceClassName; |
|
102
|
|
|
$this->coreContainer->service = function () use ($container, $classLoader, $serviceClassPath, $serviceClassName) { |
|
|
|
|
|
|
103
|
|
|
if ($classLoader->import($container->applicationInfo->applicationDir . "/services/" . $serviceClassName . ".php")) { |
|
|
|
|
|
|
104
|
|
|
return new $serviceClassPath($container); |
|
105
|
|
|
} |
|
106
|
|
|
}; |
|
107
|
|
|
} else { |
|
108
|
|
|
$this->coreContainer->service = function () {}; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Model |
|
112
|
|
View Code Duplication |
if ($modelNamespace !== null) { |
|
|
|
|
|
|
113
|
|
|
$modelClassPath = $modelNamespace . "\\" . $modelClassName; |
|
114
|
|
|
$this->coreContainer->model = function () use ($container, $classLoader, $modelClassPath, $modelClassName) { |
|
|
|
|
|
|
115
|
|
|
if ($classLoader->import($container->applicationInfo->applicationDir . "/models/" . $modelClassName . ".php")) { |
|
|
|
|
|
|
116
|
|
|
return new $modelClassPath($container); |
|
117
|
|
|
} |
|
118
|
|
|
}; |
|
119
|
|
|
} else { |
|
120
|
|
|
$classpath = "\WebStream\Exception\Extend\ClassNotFoundException"; |
|
121
|
|
|
$message = $pageName . "Service and " . $pageName . "Model is not defined."; |
|
122
|
|
|
$this->coreContainer->model = new CoreExceptionDelegator($classpath, $message); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Helper |
|
126
|
|
View Code Duplication |
if ($helperNamespace !== null) { |
|
|
|
|
|
|
127
|
|
|
$helperClassPath = $helperNamespace . "\\" . $helperClassName; |
|
128
|
|
|
$this->coreContainer->helper = function () use ($container, $classLoader, $helperClassPath, $helperClassName) { |
|
|
|
|
|
|
129
|
|
|
if ($classLoader->import($container->applicationInfo->applicationDir . "/helpers/" . $helperClassName . ".php")) { |
|
|
|
|
|
|
130
|
|
|
return new $helperClassPath($container); |
|
131
|
|
|
} |
|
132
|
|
|
}; |
|
133
|
|
|
} else { |
|
134
|
|
|
$classpath = "\WebStream\Exception\Extend\ClassNotFoundException"; |
|
135
|
|
|
$message = $pageName . "Helper is not defined."; |
|
136
|
|
|
$this->coreContainer->helper = new CoreExceptionDelegator($classpath, $message); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* 名前空間を返却する |
|
142
|
|
|
* @param string アプリケーションルート |
|
143
|
|
|
* @param string クラス名 |
|
144
|
|
|
* @return string 名前空間 |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getNamespace($appRoot, $className) |
|
147
|
|
|
{ |
|
148
|
|
|
if (file_exists($appRoot) && is_dir($appRoot)) { |
|
149
|
|
|
$iterator = new \RecursiveIteratorIterator( |
|
150
|
|
|
new \RecursiveDirectoryIterator($appRoot), |
|
151
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY, |
|
152
|
|
|
\RecursiveIteratorIterator::CATCH_GET_CHILD // for Permission deny |
|
153
|
|
|
); |
|
154
|
|
|
foreach ($iterator as $filepath => $fileObject) { |
|
155
|
|
|
if (strpos($filepath, $className . ".php") !== false) { |
|
156
|
|
|
return $this->getDefinedNamespace($filepath); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* ページ名を返却する |
|
166
|
|
|
* @return string ページ名 |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getPageName() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->container->router->pageName; |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Controllerインスタンスを返却する |
|
175
|
|
|
* @return object Controllerインスタンス |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getController() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->coreContainer->controller; |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Viewインスタンスを返却する |
|
184
|
|
|
* @return object Viewインスタンス |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getView() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->coreContainer->view; |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Serviceインスタンスを返却する |
|
193
|
|
|
* @return object Serviceインスタンス |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getService() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->coreContainer->service; |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Modelインスタンスを返却する |
|
202
|
|
|
* @return object Modelインスタンス |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getModel() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->coreContainer->model; |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Helperインスタンスを返却する |
|
211
|
|
|
* @return object Helperインスタンス |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getHelper() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->coreContainer->helper; |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.