1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staticka; |
4
|
|
|
|
5
|
|
|
use Staticka\Content\ContentInterface; |
6
|
|
|
use Staticka\Content\MarkdownContent; |
7
|
|
|
use Staticka\Contracts\BuilderContract; |
8
|
|
|
use Staticka\Contracts\FilterContract; |
9
|
|
|
use Staticka\Contracts\HelperContract; |
10
|
|
|
use Staticka\Contracts\LayoutContract; |
11
|
|
|
use Staticka\Contracts\PageContract; |
12
|
|
|
use Staticka\Contracts\WebsiteContract; |
13
|
|
|
use Staticka\Factories\PageFactory; |
14
|
|
|
use Staticka\Renderer; |
15
|
|
|
use Staticka\Contracts\RendererContract; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Website |
19
|
|
|
* |
20
|
|
|
* @package Staticka |
21
|
|
|
* @author Rougin Gutib <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Website implements WebsiteContract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Staticka\Contracts\BuilderContract |
27
|
|
|
*/ |
28
|
|
|
protected $builder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* TODO: To be removed in v1.0.0. |
32
|
|
|
* |
33
|
|
|
* @var \Staticka\Content\ContentInterface |
34
|
|
|
*/ |
35
|
|
|
protected $content; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Staticka\Factories\PageFactory |
39
|
|
|
*/ |
40
|
|
|
protected $factory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* TODO: To be removed in v1.0.0. |
44
|
|
|
* |
45
|
|
|
* @var \Staticka\Contracts\LayoutContract |
46
|
|
|
*/ |
47
|
|
|
protected $layout; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* TODO: To be removed in v1.0.0. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $output; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \Staticka\Contracts\PageContract[] |
58
|
|
|
*/ |
59
|
|
|
protected $pages; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* TODO: To be removed in v1.0.0. |
63
|
|
|
* |
64
|
|
|
* @var \Staticka\Contracts\RendererContract |
65
|
|
|
*/ |
66
|
|
|
protected $renderer; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* TODO: Use contracts in v1.0.0 instead. |
70
|
|
|
* |
71
|
|
|
* @param \Staticka\Contracts\RendererContract|\Staticka\Contracts\BuilderContract|null $renderer |
72
|
|
|
* @param \Staticka\Content\ContentInterface|\Staticka\Contracts\LayoutContract|null $content |
73
|
|
|
*/ |
74
|
27 |
|
public function __construct($builder = null, $layout = null) |
75
|
|
|
{ |
76
|
|
|
// TODO: Remove this after v1.0.0. |
77
|
27 |
|
$this->content = new MarkdownContent; |
78
|
|
|
|
79
|
|
|
// TODO: Remove this after v1.0.0. |
80
|
27 |
|
$this->renderer = new Renderer(array(getcwd())); |
81
|
|
|
|
82
|
|
|
// TODO: Remove this after v1.0.0. |
83
|
27 |
|
if ($builder instanceof RendererContract) |
84
|
9 |
|
{ |
85
|
9 |
|
$this->builder = new Builder($builder); |
86
|
|
|
|
87
|
9 |
|
$this->renderer = $builder; |
88
|
3 |
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
21 |
|
$this->builder = $builder ?: new Builder($this->renderer); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// TODO: Remove this after v1.0.0. |
95
|
27 |
|
if ($layout instanceof ContentInterface) |
96
|
9 |
|
{ |
97
|
3 |
|
$this->content = $layout; |
98
|
|
|
|
99
|
3 |
|
$this->layout = new Layout; |
100
|
1 |
|
} |
101
|
|
|
else |
102
|
|
|
{ |
103
|
27 |
|
$this->layout = $layout ?: new Layout; |
104
|
|
|
} |
105
|
27 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add a new page instance in the website. |
109
|
|
|
* |
110
|
|
|
* @param \Staticka\Contracts\PageContract $page |
111
|
|
|
*/ |
112
|
3 |
|
public function add(PageContract $page) |
113
|
|
|
{ |
114
|
3 |
|
$this->pages[] = $page; |
115
|
3 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Compiles the specified pages into HTML output. |
119
|
|
|
* |
120
|
|
|
* @param string $output |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
18 |
|
public function build($output) |
124
|
|
|
{ |
125
|
18 |
|
foreach ($this->pages as $page) |
126
|
|
|
{ |
127
|
18 |
|
$data = (array) $page->data(); |
128
|
|
|
|
129
|
18 |
|
$link = $data[PageContract::DATA_LINK]; |
130
|
|
|
|
131
|
18 |
|
$folder = $output . '/' . $link; |
132
|
|
|
|
133
|
18 |
|
$html = $this->builder->build($page); |
134
|
|
|
|
135
|
18 |
|
$path = str_replace('/index', '', $folder); |
136
|
|
|
|
137
|
18 |
|
if (! file_exists($path)) |
138
|
6 |
|
{ |
139
|
3 |
|
mkdir($path, 0700, true); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
18 |
|
$file = "$folder/index.html"; |
143
|
|
|
|
144
|
18 |
|
if ($link === 'index') |
145
|
6 |
|
{ |
146
|
15 |
|
$file = "$folder.html"; |
147
|
5 |
|
} |
148
|
|
|
|
149
|
18 |
|
file_put_contents($file, $html); |
150
|
6 |
|
} |
151
|
|
|
|
152
|
18 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Removes the files recursively from the specified directory. |
157
|
|
|
* |
158
|
|
|
* @param string $path |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
18 |
|
public function clear($path) |
162
|
|
|
{ |
163
|
18 |
|
$directory = new \RecursiveDirectoryIterator($path, 4096); |
164
|
|
|
|
165
|
18 |
|
$iterator = new \RecursiveIteratorIterator($directory, 2); |
166
|
|
|
|
167
|
18 |
|
foreach ($iterator as $file) |
168
|
|
|
{ |
169
|
18 |
|
$path = $file->getRealPath(); |
170
|
|
|
|
171
|
18 |
|
if (strpos($path, '.git') !== false) |
172
|
6 |
|
{ |
173
|
18 |
|
continue; |
174
|
|
|
} |
175
|
|
|
|
176
|
15 |
|
if ($file->isDir()) |
177
|
5 |
|
{ |
178
|
6 |
|
rmdir($path); |
179
|
|
|
|
180
|
6 |
|
continue; |
181
|
|
|
} |
182
|
|
|
|
183
|
15 |
|
unlink($path); |
184
|
6 |
|
} |
185
|
18 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* TODO: To be removed in v1.0.0. |
189
|
|
|
* Use $this->build() instead. |
190
|
|
|
* |
191
|
|
|
* Compiles the specified pages into HTML output. |
192
|
|
|
* |
193
|
|
|
* @param string $output |
194
|
|
|
* @return self |
195
|
|
|
*/ |
196
|
18 |
|
public function compile($output) |
197
|
|
|
{ |
198
|
18 |
|
if (file_exists($output)) |
199
|
6 |
|
{ |
200
|
18 |
|
$this->clear($output); |
201
|
6 |
|
} |
202
|
|
|
|
203
|
18 |
|
$this->output = (string) $output; |
204
|
|
|
|
205
|
18 |
|
return $this->build($output); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* TODO: To be removed in v1.0.0. |
210
|
|
|
* |
211
|
|
|
* Returns the content instance. |
212
|
|
|
* |
213
|
|
|
* @return \Staticka\Content\ContentInterface |
214
|
|
|
*/ |
215
|
3 |
|
public function content() |
216
|
|
|
{ |
217
|
3 |
|
return $this->content; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Transfers files from a directory into another path. |
222
|
|
|
* |
223
|
|
|
* @param string $source |
224
|
|
|
* @param string $path |
225
|
|
|
* @return self |
226
|
|
|
*/ |
227
|
3 |
|
public function copy($source, $path) |
228
|
|
|
{ |
229
|
3 |
|
$source = realpath($source); |
230
|
|
|
|
231
|
3 |
|
$this->clear((string) $path); |
232
|
|
|
|
233
|
3 |
|
foreach (glob("$source/**/**.**") as $file) |
234
|
|
|
{ |
235
|
3 |
|
$dirname = dirname(realpath($file)); |
236
|
|
|
|
237
|
3 |
|
$basename = basename(realpath($file)); |
238
|
|
|
|
239
|
3 |
|
$newpath = str_replace($source, $path, $dirname); |
240
|
|
|
|
241
|
3 |
|
if (! file_exists($newpath)) |
242
|
1 |
|
{ |
243
|
3 |
|
mkdir($newpath, 0777, true); |
244
|
1 |
|
} |
245
|
|
|
|
246
|
3 |
|
copy($file, "$newpath/$basename"); |
247
|
1 |
|
} |
248
|
|
|
|
249
|
3 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* TODO: To be removed in v1.0.0. |
254
|
|
|
* |
255
|
|
|
* Adds a filter instance. |
256
|
|
|
* |
257
|
|
|
* @param \Staticka\Contracts\FilterContract $filter |
258
|
|
|
* @return self |
259
|
|
|
*/ |
260
|
3 |
|
public function filter(FilterContract $filter) |
261
|
|
|
{ |
262
|
3 |
|
$this->layout->filter($filter); |
263
|
|
|
|
264
|
3 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* TODO: To be removed in v1.0.0. |
269
|
|
|
* |
270
|
|
|
* Adds a helper instance. |
271
|
|
|
* |
272
|
|
|
* @param \Staticka\Contracts\HelperContract $helper |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
3 |
|
public function helper(HelperContract $helper) |
276
|
|
|
{ |
277
|
3 |
|
$this->layout->helper($helper); |
278
|
|
|
|
279
|
3 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* TODO: To be removed in v1.0.0. |
284
|
|
|
* Use $this->add() instead with a factory. |
285
|
|
|
* |
286
|
|
|
* Creates a new page. |
287
|
|
|
* |
288
|
|
|
* @param string $file |
289
|
|
|
* @param array $data |
290
|
|
|
* @return self |
291
|
|
|
*/ |
292
|
21 |
|
public function page($file, $data = array()) |
293
|
|
|
{ |
294
|
21 |
|
$page = new PageFactory($this->layout); |
295
|
|
|
|
296
|
21 |
|
if (file_exists($file)) |
297
|
7 |
|
{ |
298
|
6 |
|
$this->pages[] = $page->file($file, $data); |
299
|
2 |
|
} |
300
|
|
|
else |
301
|
|
|
{ |
302
|
|
|
// TODO: Remove this on v1.0.0. |
303
|
|
|
// Should only be file-based. |
304
|
|
|
|
305
|
21 |
|
$this->pages[] = $page->body($file, $data); |
306
|
|
|
} |
307
|
|
|
|
308
|
21 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* TODO: To be removed in v1.0.0. |
313
|
|
|
* |
314
|
|
|
* Returns the renderer instance. |
315
|
|
|
* |
316
|
|
|
* @return \Staticka\Contracts\RendererContract |
317
|
|
|
*/ |
318
|
6 |
|
public function renderer() |
319
|
|
|
{ |
320
|
6 |
|
return $this->renderer; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* TODO: To be removed in v1.0.0. |
325
|
|
|
* Use $this->copy() instead. |
326
|
|
|
* |
327
|
|
|
* Transfers files from a directory into another path. |
328
|
|
|
* |
329
|
|
|
* @param string $source |
330
|
|
|
* @param string|null $path |
331
|
|
|
* @return self |
332
|
|
|
*/ |
333
|
3 |
|
public function transfer($source, $path = null) |
334
|
|
|
{ |
335
|
3 |
|
return $this->copy($source, $path ? $path : $this->output); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|