1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Core; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Container; |
5
|
|
|
use WebStream\Module\Utility\CommonUtils; |
6
|
|
|
use WebStream\Template\ITemplateEngine; |
7
|
|
|
use WebStream\Annotation\Filter; |
8
|
|
|
use WebStream\Annotation\Base\IAnnotatable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CoreViewクラス |
12
|
|
|
* @author Ryuichi TANAKA. |
13
|
|
|
* @since 2011/09/12 |
14
|
|
|
* @version 0.7 |
15
|
|
|
*/ |
16
|
|
|
class CoreView implements CoreInterface, IAnnotatable |
17
|
|
|
{ |
18
|
|
|
use CommonUtils; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Container 依存コンテナ |
22
|
|
|
*/ |
23
|
|
|
private $container; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ITemplateEngine テンプレートエンジン |
27
|
|
|
*/ |
28
|
|
|
private $templateEngine; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoggerAdapter ロガー |
32
|
|
|
*/ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Container $container) |
39
|
|
|
{ |
40
|
|
|
$this->container = $container; |
41
|
|
|
$this->logger = $container->logger; |
|
|
|
|
42
|
|
|
$this->logger->debug("View start."); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function __destruct() |
49
|
|
|
{ |
50
|
|
|
$this->logger->debug("View end."); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* @Filter(type="initialize") |
56
|
|
|
*/ |
57
|
|
|
public function __initialize(Container $container) |
58
|
|
|
{ |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function __customAnnotation(array $annotation) |
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* テンプレートエンジンを設定する |
70
|
|
|
* @param ITemplateEngine テンプレートエンジン |
71
|
|
|
*/ |
72
|
|
|
public function setTemplateEngine(ITemplateEngine $templateEngine = null) |
73
|
|
|
{ |
74
|
|
|
$this->templateEngine = $templateEngine; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* テンプレートを描画する |
79
|
|
|
* @param array<string> パラメータ |
80
|
|
|
*/ |
81
|
|
|
public function draw(array $params) |
82
|
|
|
{ |
83
|
|
|
$mimeType = $params["mimeType"] ?: "html"; |
84
|
|
|
$this->outputHeader($mimeType); |
85
|
|
|
|
86
|
|
|
// HTML,XML以外はテンプレートを使用しない |
87
|
|
|
if ($mimeType !== "html" && $mimeType !== "xml") { |
88
|
|
|
$this->logger->debug("Only html or xml draw view template."); |
89
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->templateEngine !== null) { |
94
|
|
|
$this->templateEngine->render($params); |
95
|
|
|
if ($this->templateEngine instanceof \WebStream\Template\Basic) { |
96
|
|
|
$this->templateEngine->renderHelper($params); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* テンプレートキャッシュを作成する |
103
|
|
|
* @param string テンプレートファイルパス |
104
|
|
|
* @param string 保存データ |
105
|
|
|
* @param integer 有効期限 |
106
|
|
|
*/ |
107
|
|
|
public function templateCache($filepath, $cacheData, $cacheTime) |
108
|
|
|
{ |
109
|
|
|
if ($this->templateEngine instanceof \WebStream\Template\Basic) { |
110
|
|
|
$this->templateEngine->cache($filepath, $cacheData, $cacheTime); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 共通ヘッダを出力する |
116
|
|
|
* @param String ファイルタイプ |
117
|
|
|
*/ |
118
|
|
|
private function outputHeader($type) |
119
|
|
|
{ |
120
|
|
|
$this->container->response->setType($type); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* publicディレクトリにある静的ファイルを表示する |
125
|
|
|
* @param String ファイルパス |
126
|
|
|
*/ |
127
|
|
|
final public function __file($filepath) |
128
|
|
|
{ |
129
|
|
|
$publicDir = $this->container->applicationInfo->publicDir; |
|
|
|
|
130
|
|
|
if (preg_match('/\/views\/' . $publicDir . '\/img\/.+\.(?:jp(?:e|)g|png|bmp|(?:tif|gi)f)$/i', $filepath) || |
131
|
|
|
preg_match('/\/views\/' . $publicDir . '\/css\/.+\.css$/i', $filepath) || |
132
|
|
|
preg_match('/\/views\/' . $publicDir . '\/js\/.+\.js$/i', $filepath)) { // 画像,css,jsの場合 |
133
|
|
|
$this->display($filepath); |
134
|
|
|
} elseif (preg_match('/\/views\/' . $publicDir . '\/file\/.+$/i', $filepath)) { // それ以外のファイル |
135
|
|
|
$this->download($filepath); |
136
|
|
|
} else { // 全てのファイル |
137
|
|
|
$this->display($filepath); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* 画像、CSS、JavaScriptファイルを表示する |
143
|
|
|
* @param string ファイルパス |
144
|
|
|
*/ |
145
|
|
|
final private function display($filename) |
146
|
|
|
{ |
147
|
|
|
$this->container->response->displayFile($filename); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* ファイルをダウンロードする |
152
|
|
|
* @param string ファイルパス |
153
|
|
|
*/ |
154
|
|
|
final private function download($filename) |
155
|
|
|
{ |
156
|
|
|
$userAgent = $this->container->request->userAgent(); |
|
|
|
|
157
|
|
|
$this->container->response->downloadFile($filename, $userAgent); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.