1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Renderable\File; |
11
|
|
|
|
12
|
|
|
use SplFileInfo; |
13
|
|
|
|
14
|
|
|
class File |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var SplFileInfo |
18
|
|
|
*/ |
19
|
|
|
protected $fileInfo; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $relativeSource; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $outputPath; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $content; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $configuration = []; |
40
|
|
|
|
41
|
10 |
|
public function __construct(SplFileInfo $fileInfo, string $relativeSource) |
42
|
|
|
{ |
43
|
10 |
|
$this->relativeSource = $relativeSource; |
44
|
10 |
|
$this->fileInfo = $fileInfo; |
45
|
10 |
|
$this->content = file_get_contents($fileInfo->getRealPath()); |
46
|
10 |
|
} |
47
|
|
|
|
48
|
4 |
|
public function setOutputPath(string $outputPath) |
49
|
|
|
{ |
50
|
4 |
|
$this->outputPath = $outputPath; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
|
|
public function getRelativeSource() : string |
54
|
|
|
{ |
55
|
|
|
return $this->relativeSource; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
public function getBaseName() : string |
59
|
|
|
{ |
60
|
5 |
|
return $this->fileInfo->getBasename('.'.$this->fileInfo->getExtension()); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function getPrimaryExtension() : string |
64
|
|
|
{ |
65
|
3 |
|
$fileParts = explode('.', $this->fileInfo->getBasename()); |
66
|
3 |
|
if (count($fileParts) > 2) { |
67
|
|
|
return $fileParts[count($fileParts) - 2]; |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
return $fileParts[count($fileParts) - 1]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getExtension() : string |
74
|
|
|
{ |
75
|
|
|
return $this->fileInfo->getExtension(); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
public function getRelativeUrl() : string |
79
|
|
|
{ |
80
|
3 |
|
if ($position = strpos($this->outputPath, '/index.html')) { |
81
|
2 |
|
return substr($this->outputPath, 0, $position); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this->outputPath; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function getContent() : string |
88
|
|
|
{ |
89
|
6 |
|
return $this->content; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function changeContent(string $newContent) |
93
|
|
|
{ |
94
|
4 |
|
$this->content = $newContent; |
95
|
4 |
|
} |
96
|
|
|
|
97
|
2 |
|
public function setConfiguration(array $configuration) |
98
|
|
|
{ |
99
|
2 |
|
$this->configuration = $configuration; |
100
|
2 |
|
} |
101
|
|
|
|
102
|
4 |
|
public function getConfiguration() : array |
103
|
|
|
{ |
104
|
4 |
|
return $this->configuration; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function getOutputPath() : string |
108
|
|
|
{ |
109
|
1 |
|
return $this->outputPath; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function getLayout() : string |
113
|
|
|
{ |
114
|
1 |
|
return $this->configuration['layout'] ?? 'default'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|