CoreView::__initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Core;
3
4
use WebStream\Annotation\Attributes\Filter;
5
use WebStream\Annotation\Base\IAnnotatable;
6
use WebStream\Container\Container;
7
use WebStream\DI\Injector;
8
use WebStream\Template\ITemplateEngine;
9
use WebStream\Util\CommonUtils;
10
11
/**
12
 * CoreViewクラス
13
 * @author Ryuichi TANAKA.
14
 * @since 2011/09/12
15
 * @version 0.7
16
 */
17
class CoreView implements CoreInterface, IAnnotatable
18
{
19
    use Injector, CommonUtils;
20
21
    /**
22
     * @var Request リクエスト
0 ignored issues
show
Bug introduced by
The type WebStream\Core\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
     */
24
    private $request;
25
26
    /**
27
     * @var Response レスポンス
0 ignored issues
show
Bug introduced by
The type WebStream\Core\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
     */
29
    private $response;
30
31
    /**
32
     * @var Container アプリケーション情報
33
     */
34
    private $applicationInfo;
35
36
    /**
37
     * @var ITemplateEngine テンプレートエンジン
38
     */
39
    private $templateEngine;
40
41
    /**
42
     * @var LoggerAdapter ロガー
0 ignored issues
show
Bug introduced by
The type WebStream\Core\LoggerAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    private $logger;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function __destruct()
50
    {
51
        $this->logger->debug("View end.");
52
    }
53
54
    /**
55
     * {@inheritdoc}
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 テンプレートエンジン
0 ignored issues
show
Documentation Bug introduced by
The doc comment テンプレートエンジン at position 0 could not be parsed: Unknown type name 'テンプレートエンジン' at position 0 in テンプレートエンジン.
Loading history...
71
     */
72
    public function setTemplateEngine(ITemplateEngine $templateEngine = null)
73
    {
74
        $this->templateEngine = $templateEngine;
75
    }
76
77
    /**
78
     * テンプレートを描画する
79
     * @param array<string> パラメータ
0 ignored issues
show
Documentation Bug introduced by
The doc comment パラメータ at position 0 could not be parsed: Unknown type name 'パラメータ' at position 0 in パラメータ.
Loading history...
80
     */
81
    public function draw(array $params)
82
    {
83
        $mimeType = $params["mimeType"];
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 テンプレートファイルパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment テンプレートファイルパス at position 0 could not be parsed: Unknown type name 'テンプレートファイルパス' at position 0 in テンプレートファイルパス.
Loading history...
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 ファイルタイプ
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルタイプ at position 0 could not be parsed: Unknown type name 'ファイルタイプ' at position 0 in ファイルタイプ.
Loading history...
117
     */
118
    private function outputHeader($type)
119
    {
120
        $this->response->setType($type);
121
    }
122
123
    /**
124
     * publicディレクトリにある静的ファイルを表示する
125
     * @param String ファイルパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルパス at position 0 could not be parsed: Unknown type name 'ファイルパス' at position 0 in ファイルパス.
Loading history...
126
     */
127
    final public function __file($filepath)
128
    {
129
        $publicDir = $this->applicationInfo->publicDir;
0 ignored issues
show
Bug Best Practice introduced by
The property publicDir does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
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 ファイルパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルパス at position 0 could not be parsed: Unknown type name 'ファイルパス' at position 0 in ファイルパス.
Loading history...
144
     */
145
    final private function display($filename)
146
    {
147
        $this->response->displayFile($filename);
148
    }
149
150
    /**
151
     * ファイルをダウンロードする
152
     * @param string ファイルパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルパス at position 0 could not be parsed: Unknown type name 'ファイルパス' at position 0 in ファイルパス.
Loading history...
153
     */
154
    final private function download($filename)
155
    {
156
        $userAgent = $this->request->userAgent();
157
        $this->response->downloadFile($filename, $userAgent);
158
    }
159
}
160