Passed
Push — master ( 79eb06...102376 )
by 世昌
02:35
created

ModuleTemplateBase::prepareStaticModuleSource()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 7
nop 2
dl 0
loc 16
rs 9.4888
c 1
b 0
f 0
1
<?php
2
namespace suda\application\template;
3
4
use suda\framework\Request;
5
use suda\application\Application;
6
use suda\framework\filesystem\FileSystem;
7
use suda\application\template\compiler\Compiler;
8
use suda\application\Resource as ApplicationResource;
9
10
/**
11
 * 模块模板
12
 */
13
class ModuleTemplateBase extends CompilableTemplate
14
{
15
    /**
16
     * 模板模块
17
     *
18
     * @var string|null
19
     */
20
    protected $module = null;
21
22
    /**
23
     * 路由组
24
     *
25
     * @var string|null
26
     */
27
    protected $group = null;
28
29
    /**
30
     * 安全URI路径
31
     *
32
     * @var string
33
     */
34
    protected $uriName = 'application';
35
36
    /**
37
     * 模板名
38
     *
39
     * @var string
40
     */
41
    protected $name;
42
43
    /**
44
     * 应用环境
45
     *
46
     * @var Application
47
     */
48
    protected $application;
49
50
    /**
51
     * 配置文件
52
     *
53
     * @var array
54
     */
55
    protected $config;
56
57
    /**
58
     * 请求信息
59
     *
60
     * @var Request
61
     */
62
    protected $request;
63
64
    /**
65
     * ModuleTemplateBase constructor.
66
     * @param string $name 模板描述符
67
     * @param Application $application
68
     * @param Request $request
69
     * @param string|null $defaultModule
70
     */
71
    public function __construct(string $name, Application $application, Request $request, ?string $defaultModule = null)
72
    {
73
        parent::__construct('', []);
74
        $this->application = $application;
75
        $this->request = $request;
76
        list($this->module, $this->group, $this->name) = $application->parseRouteName($name, $defaultModule, 'default');
77
        $this->group = $request->getAttribute('group', $this->group);
78
        $this->config = $this->getModuleConfig($this->module);
79
        $this->uriName = TemplateUtil::getSafeUriName($this->application, $this->module);
80
        $this->value = [];
81
    }
82
83
    /**
84
     * @param string|null $module
85
     * @return mixed
86
     */
87
    protected function getModuleConfig(?string $module)
88
    {
89
        return TemplateUtil::getConfig($this->application, $module);
90
    }
91
92
93
    /**
94
     * @return Compiler
95
     */
96
    protected function createCompiler(): Compiler
97
    {
98
        $compiler = new ModuleTemplateCompiler;
99
        $compiler->init();
100
        $this->application->event()->exec(
101
            'application:template:compile::create',
102
            [$compiler, $this->config, $this->application]
103
        );
104
        return $compiler;
105
    }
106
107
    /**
108
     * @param string|null $module
109
     * @param string|null $name
110
     * @return string|null
111
     */
112
    protected function getModuleStaticPath(?string $module, ?string  $name = null)
113
    {
114
        $name = $name ?? $this->getModuleConfig($module)['static'];
115
        return $this->getResource($module)->getResourcePath($this->getTemplatePath().'/'.$name) ?? '';
116
    }
117
118
119
    /**
120
     * @param string|null $module
121
     * @param string|null $name
122
     * @return string
123
     */
124
    protected function getModuleStaticOutputPath(?string $module, ?string  $name = null)
125
    {
126
        $config = $this->getModuleConfig($module);
127
        $name = $name ?? $this->getModuleConfig($module)['static'];
128
        $path = $config['assets-path'].'/'. $this->getModuleUriName($module) .'/'.$name;
129
        FileSystem::make($path);
130
        return $path;
131
    }
132
133
    /**
134
     * @param string|null $module
135
     * @return mixed
136
     */
137
    protected function getModuleUriName(?string $module)
138
    {
139
        $config = $this->getModuleConfig($module);
140
        return $config['uri-name'];
141
    }
142
143
    /**
144
     * @param string|null $module
145
     * @return ApplicationResource
146
     */
147
    protected function getResource(?string $module): ApplicationResource
148
    {
149
        return TemplateUtil::getResource($this->application, $module ?? $this->module);
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    protected function getTemplatePath()
156
    {
157
        return TemplateUtil::getTemplatePath($this->application);
158
    }
159
160
    /**
161
     * @param string|null $module
162
     * @param string|null $name
163
     * @return string
164
     */
165
    protected function getStaticModulePrefix(?string $module = null, ?string $name = null)
166
    {
167
        if ($module === null) {
168
            $module = $this->module;
169
        }
170
        $this->prepareStaticModuleSource($module, $name);
171
        $config = TemplateUtil::getConfig($this->application, $module);
172
        $name = $name ?? $config['static'];
173
        return $this->getModuleStaticAssetRoot($module) .'/'.$this->getModuleUriName($module). '/'.$name;
174
    }
175
176
    /**
177
     * @param string|null $module
178
     * @return string
179
     */
180
    protected function getModulePrefix(?string $module = null)
181
    {
182
        if ($module === null) {
183
            $module = $this->module;
184
        }
185
        return $this->getModuleAssetRoot($module) .'/'.$this->getModuleUriName($module);
186
    }
187
188
    /**
189
     * @param string|null $module
190
     * @return string
191
     */
192
    protected function getModuleAssetRoot(?string $module)
193
    {
194
        return TemplateUtil::getRequestAsset($this->application, $this->request, $module);
195
    }
196
197
    /**
198
     * @param string|null $module
199
     * @return string
200
     */
201
    protected function getModuleStaticAssetRoot(?string $module)
202
    {
203
        return TemplateUtil::getStaticRequestAsset($this->application, $this->request, $module);
204
    }
205
206
    /**
207
     * @param string|null $module
208
     * @param string|null $name
209
     */
210
    protected function prepareStaticModuleSource(?string $module, ?string  $name = null)
211
    {
212
        $copySource = $this->application->conf('copy-static-source', SUDA_DEBUG);
213
        if ($copySource) {
214
            $static = $this->getModuleStaticPath($module, $name);
215
            $staticCopyed = is_dir($static) && in_array($static, static::$copiedStaticPaths);
216
            if ($staticCopyed === false) {
217
                $from = $static;
218
                $to = $this->getModuleStaticOutputPath($module, $name);
219
                $time = sprintf('copy template static source %s => %s ', $from, $to);
220
                $this->application->debug()->time($time);
221
                if (FileSystem::copyDir($from, $to)) {
222
                    $this->application->debug()->timeEnd($time);
223
                    static::$copiedStaticPaths[] = $static;
224
                } else {
225
                    $this->application->debug()->warning('Failed: '.$time);
226
                }
227
            }
228
        }
229
    }
230
231
    /**
232
     * @return Application
233
     */
234
    public function getApplication(): Application
235
    {
236
        return $this->application;
237
    }
238
239
    /**
240
     * @return Request
241
     */
242
    public function getRequest(): Request
243
    {
244
        return $this->request;
245
    }
246
}
247