1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Module; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Utility\ApplicationUtils; |
5
|
|
|
use WebStream\Module\Singleton; |
6
|
|
|
use WebStream\Container\Container; |
7
|
|
|
use WebStream\Log\Logger; |
8
|
|
|
use WebStream\Log\LoggerAdapter; |
9
|
|
|
use WebStream\Log\Outputter\FileOutputter; |
10
|
|
|
use WebStream\Delegate\Router; |
11
|
|
|
use WebStream\Delegate\CoreDelegator; |
12
|
|
|
use WebStream\Delegate\AnnotationDelegator; |
13
|
|
|
use WebStream\Http\Request; |
14
|
|
|
use WebStream\Http\Response; |
15
|
|
|
use WebStream\Http\Session; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ServiceLocatorクラス |
19
|
|
|
* @author Ryuichi TANAKA. |
20
|
|
|
* @since 2013/01/14 |
21
|
|
|
* @version 0.7 |
22
|
|
|
*/ |
23
|
|
|
class ServiceLocator |
24
|
|
|
{ |
25
|
|
|
use Singleton, ApplicationUtils; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* コンテナを作成する |
29
|
|
|
* @param boolean テスト環境フラグ |
30
|
|
|
* @return object コンテナ |
31
|
|
|
*/ |
32
|
|
|
public function getContainer() |
33
|
|
|
{ |
34
|
|
|
$container = new Container(); |
35
|
|
|
|
36
|
|
|
// LoggerAdapter |
37
|
|
|
$container->logger = function () { |
|
|
|
|
38
|
|
|
$instance = Logger::getInstance(); |
39
|
|
|
$instance->setOutputter([ |
|
|
|
|
40
|
|
|
new FileOutputter($instance->getConfig()->logPath) |
|
|
|
|
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
return new LoggerAdapter($instance); |
44
|
|
|
}; |
45
|
|
|
// Request |
46
|
|
|
$container->request = function () use (&$container) { |
|
|
|
|
47
|
|
|
$request = new Request(); |
48
|
|
|
$request->inject('logger', $container->logger); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $request->getContainer(); |
51
|
|
|
}; |
52
|
|
|
// Response |
53
|
|
|
$container->response = function () use (&$container) { |
|
|
|
|
54
|
|
|
$response = new Response(); |
55
|
|
|
$response->inject('logger', $container->logger); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
}; |
59
|
|
|
// Session |
60
|
|
|
$container->session = function () use (&$container) { |
|
|
|
|
61
|
|
|
$session = new Session(); |
62
|
|
|
$session->inject('logger', $container->logger); |
|
|
|
|
63
|
|
|
|
64
|
|
|
return $session; |
65
|
|
|
}; |
66
|
|
|
// Router |
67
|
|
|
$container->router = function () use (&$container) { |
|
|
|
|
68
|
|
|
// Router |
69
|
|
|
$config = \Spyc::YAMLLoad($container->applicationInfo->applicationRoot . $container->applicationInfo->routeConfigPath); |
|
|
|
|
70
|
|
|
$router = new Router($config, $container->request); |
|
|
|
|
71
|
|
|
$router->inject('logger', $container->logger) |
|
|
|
|
72
|
|
|
->inject('applicationInfo', $container->applicationInfo); |
|
|
|
|
73
|
|
|
$router->resolve(); |
74
|
|
|
|
75
|
|
|
return $router->getRoutingResult(); |
76
|
|
|
}; |
77
|
|
|
// CoreDelegator |
78
|
|
|
$container->coreDelegator = function () use (&$container) { |
|
|
|
|
79
|
|
|
return new CoreDelegator($container); |
80
|
|
|
}; |
81
|
|
|
// AnnotationDelegator |
82
|
|
|
$container->annotationDelegator = function () use (&$container) { |
|
|
|
|
83
|
|
|
return new AnnotationDelegator($container); |
84
|
|
|
}; |
85
|
|
|
// twig |
86
|
|
|
$container->twig = function () { |
|
|
|
|
87
|
|
|
Twig_Autoloader::register(); |
88
|
|
|
}; |
89
|
|
|
// Application Info |
90
|
|
|
$applicationRoot = $this->getApplicationRoot(); |
91
|
|
|
$container->applicationInfo = function () use ($applicationRoot) { |
|
|
|
|
92
|
|
|
$info = new Container(); |
93
|
|
|
$info->applicationRoot = $applicationRoot; |
|
|
|
|
94
|
|
|
$info->applicationDir = "app"; |
|
|
|
|
95
|
|
|
$info->sharedDir = "_shared"; |
|
|
|
|
96
|
|
|
$info->publicDir = "_public"; |
|
|
|
|
97
|
|
|
$info->cacheDir = "_cache"; |
|
|
|
|
98
|
|
|
$info->cachePrefix = "webstream-cache-"; |
|
|
|
|
99
|
|
|
$info->routeConfigPath = "/config/routes.yml"; |
|
|
|
|
100
|
|
|
$info->validateRuleDir = "core/WebStream/Validate/Rule/"; |
|
|
|
|
101
|
|
|
|
102
|
|
|
return $info; |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
return $container; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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@property
annotation 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.