1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Core; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Container; |
5
|
|
|
use WebStream\Module\Utility\LoggerUtils; |
6
|
|
|
use WebStream\Delegate\Resolver; |
7
|
|
|
use WebStream\Exception\ApplicationException; |
8
|
|
|
use WebStream\Exception\SystemException; |
9
|
|
|
use WebStream\Exception\DelegateException; |
10
|
|
|
use WebStream\DI\ServiceLocator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Applicationクラス |
14
|
|
|
* @author Ryuichi Tanaka |
15
|
|
|
* @since 2011/08/19 |
16
|
|
|
* @version 0.7 |
17
|
|
|
*/ |
18
|
|
|
class Application |
19
|
|
|
{ |
20
|
|
|
use LoggerUtils; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Container DIコンテナ |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* アプリケーション共通で使用するクラスを初期化する |
29
|
|
|
* @param Container DIコンテナ |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Container $container) |
32
|
|
|
{ |
33
|
|
|
register_shutdown_function([&$this, 'shutdownHandler']); |
34
|
|
|
$this->container = $container; |
35
|
|
|
$this->container->logger->debug("Application start"); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* アプリケーション終了時の処理 |
40
|
|
|
*/ |
41
|
|
|
public function __destruct() |
42
|
|
|
{ |
43
|
|
|
$this->container->logger->debug("Application end"); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* アプリケーションを起動する |
48
|
|
|
*/ |
49
|
|
|
public function run() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$resolver = new Resolver($this->container); |
53
|
|
|
$resolver->runController(); // MVCレイヤへのリクエストの振り分けを実行する |
54
|
|
|
} catch (ApplicationException $e) { |
55
|
|
|
// 内部例外の内、ハンドリングを許可している例外 |
56
|
|
|
try { |
57
|
|
|
$isHandled = false; |
58
|
|
|
if ($e instanceof DelegateException) { |
59
|
|
|
$isHandled = $e->isHandled(); |
60
|
|
|
$e = $e->getOriginException(); |
61
|
|
|
} |
62
|
|
|
if (!$isHandled) { |
63
|
|
|
$this->container->response->move($e->getCode()); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
// 開発者由来の例外は全て500 |
67
|
|
|
$this->container->logger->error($this->addStackTrace($e->getMessage(), $e->getTraceAsString())); |
|
|
|
|
68
|
|
|
$this->container->response->move(500); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} catch (SystemException $e) { |
71
|
|
|
// 内部例外の内、ハンドリング不許可の例外 |
72
|
|
|
$this->container->response->move($e->getCode()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* 例外捕捉不可な異常時のアプリケーション終了処理 |
78
|
|
|
*/ |
79
|
|
|
public function shutdownHandler() |
80
|
|
|
{ |
81
|
|
|
if ($error = error_get_last()) { |
82
|
|
|
$errorMsg = $error['message'] . " " . $error['file'] . "(" . $error['line'] . ")"; |
83
|
|
|
switch ($error['type']) { |
84
|
|
|
case E_ERROR: |
85
|
|
|
case E_CORE_ERROR: |
86
|
|
|
case E_COMPILE_ERROR: |
87
|
|
|
case E_USER_ERROR: |
88
|
|
|
case E_RECOVERABLE_ERROR: |
89
|
|
|
$this->container->logger->fatal($errorMsg); |
|
|
|
|
90
|
|
|
break; |
91
|
|
|
case E_PARSE: |
92
|
|
|
$this->container->logger->error($errorMsg); |
|
|
|
|
93
|
|
|
break; |
94
|
|
|
case E_WARNING: |
95
|
|
|
case E_CORE_WARNING: |
96
|
|
|
case E_COMPILE_WARNING: |
97
|
|
|
case E_USER_WARNING: |
98
|
|
|
case E_STRICT: |
99
|
|
|
case E_NOTICE: |
100
|
|
|
case E_USER_NOTICE: |
101
|
|
|
case E_DEPRECATED: |
102
|
|
|
case E_USER_DEPRECATED: |
103
|
|
|
$this->container->logger->warn($errorMsg); |
|
|
|
|
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->container->logger->enableDirectWrite(); |
|
|
|
|
108
|
|
|
$this->container->response->move(500); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.