1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Document; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Filesystem\File; |
11
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
12
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
13
|
|
|
|
14
|
|
|
abstract class ReadableDocument |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Do not read the file immediately at construction. The object will need to execute the self::refreshFileContent() |
18
|
|
|
* manually at some point. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $noReadOnConstructor = false; |
23
|
|
|
|
24
|
|
|
/** @var string The content of the document's body. */ |
25
|
|
|
protected $bodyContent = ''; |
26
|
|
|
|
27
|
|
|
protected $metadata; |
28
|
|
|
protected $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ReadableDocument Constructor. |
32
|
|
|
* |
33
|
|
|
* @throws FileNotFoundException When the file given in the constructor does not exist or may not be accessible. |
34
|
|
|
*/ |
35
|
127 |
|
public function __construct(File $file) |
36
|
|
|
{ |
37
|
127 |
|
$filePath = (string)$file; |
38
|
|
|
|
39
|
127 |
|
if (!fs::exists($filePath)) |
40
|
127 |
|
{ |
41
|
2 |
|
throw new FileNotFoundException(null, 0, null, $filePath); |
42
|
|
|
} |
43
|
|
|
|
44
|
125 |
|
$this->metadata = []; |
45
|
125 |
|
$this->file = $file; |
46
|
|
|
|
47
|
125 |
|
if (!$this->noReadOnConstructor) |
48
|
125 |
|
{ |
49
|
119 |
|
$this->readContent(); |
50
|
108 |
|
} |
51
|
114 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the contents of this document. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
4 |
|
public function getContent() |
59
|
|
|
{ |
60
|
4 |
|
return $this->bodyContent; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
41 |
|
public function getIndexName() |
67
|
|
|
{ |
68
|
41 |
|
return $this->getRelativeFilePath(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the relative path to the file, with respect to the site root. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
88 |
|
final public function getRelativeFilePath() |
77
|
|
|
{ |
78
|
88 |
|
return $this->file->getRelativeFilePath(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the extension of the file. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
14 |
|
final public function getExtension() |
87
|
|
|
{ |
88
|
14 |
|
return $this->file->getExtension(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the name of the file without the extension. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
4 |
|
final public function getBasename() |
97
|
|
|
{ |
98
|
4 |
|
return $this->file->getBasename(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the absolute path to the file. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
16 |
|
final public function getAbsoluteFilePath() |
107
|
|
|
{ |
108
|
16 |
|
return $this->file->getAbsolutePath(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the name of the file with its extension. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
final public function getFilename() |
117
|
|
|
{ |
118
|
|
|
return $this->file->getFilename(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Read the contents of the file and handle any parsing that needs to be done. |
123
|
|
|
* |
124
|
|
|
* For example, if a file needs to parse and evaluate FrontMatter, that will need to be in this function call after |
125
|
|
|
* reading the file contents. |
126
|
|
|
* |
127
|
|
|
* @throws \RuntimeException When the file cannot be read. |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
abstract public function readContent(); |
132
|
|
|
} |
133
|
|
|
|