1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JamesMoss\Flywheel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* NestedRepository |
7
|
|
|
*/ |
8
|
|
|
class NestedRepository extends Repository |
9
|
|
|
{ |
10
|
|
|
const SEPERATOR = '/'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @inherit |
14
|
|
|
*/ |
15
|
4 |
|
public function __construct($name, Config $config) |
16
|
|
|
{ |
17
|
4 |
|
parent::__construct($name, $config); |
18
|
|
|
|
19
|
4 |
|
$this->deleteEmptyDirs = $config->getOption('delete_empty_dirs') === true; |
|
|
|
|
20
|
4 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get the filesystem path for a document based on it's ID. |
24
|
|
|
* |
25
|
|
|
* @param string $id The ID of the document. |
26
|
|
|
* |
27
|
|
|
* @return string The full filesystem path of the document. |
28
|
|
|
*/ |
29
|
3 |
|
public function getPathForDocument($id) |
30
|
|
|
{ |
31
|
3 |
|
if(!$this->validateId($id)) { |
32
|
|
|
throw new \Exception(sprintf('`%s` is not a valid document ID.', $id)); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
if($this->isNestedId($id)) { |
36
|
3 |
|
$path = DIRECTORY_SEPARATOR . str_replace(self::SEPERATOR, DIRECTORY_SEPARATOR, dirname($id)); |
37
|
3 |
|
} else { |
38
|
|
|
$path = ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
return $this->path . $path . DIRECTORY_SEPARATOR . $this->getFilename($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inherit |
46
|
|
|
*/ |
47
|
2 |
|
public function delete($id) |
48
|
|
|
{ |
49
|
2 |
|
$result = parent::delete($id); |
50
|
|
|
|
51
|
2 |
|
if ($id instanceof DocumentInterface) { |
52
|
|
|
$id = $id->getId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
if(!$result || !$this->deleteEmptyDirs || !$this->isNestedId($id)) { |
56
|
1 |
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$path = $this->getPathForDocument($id); |
60
|
1 |
|
$dir = dirname($path); |
61
|
|
|
|
62
|
1 |
|
if(file_exists($dir) && count(glob($dir . '/*')) === 0) { |
63
|
1 |
|
rmdir($dir); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inherit |
71
|
|
|
*/ |
72
|
1 |
|
protected function write($path, $contents) |
73
|
|
|
{ |
74
|
|
|
// ensure path exists by making directories beforehand |
75
|
1 |
|
if(!file_exists(dirname($path))) { |
76
|
1 |
|
mkdir(dirname($path), 0777, true); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return parent::write($path, $contents); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets just the filename for a document based on it's ID. |
84
|
|
|
* |
85
|
|
|
* @param string $id The ID of the document. |
86
|
|
|
* |
87
|
|
|
* @return string The filename of the document, including extension. |
88
|
|
|
*/ |
89
|
3 |
|
public function getFilename($id) |
90
|
|
|
{ |
91
|
3 |
|
return basename($id) . '.' . $this->formatter->getFileExtension(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get an array containing the path of all files in this repository |
96
|
|
|
* |
97
|
|
|
* @return array An array, item is a file path. |
98
|
|
|
*/ |
99
|
1 |
|
public function getAllFiles() |
100
|
|
|
{ |
101
|
1 |
|
$ext = $this->formatter->getFileExtension(); |
102
|
1 |
|
$files = array(); |
103
|
1 |
|
$this->getFilesRecursive($this->path, $files, $ext); |
104
|
|
|
|
105
|
1 |
|
return $files; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
protected function isNestedId($id) |
109
|
|
|
{ |
110
|
3 |
|
return strpos($id, self::SEPERATOR) !== false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Checks to see if a document ID is valid |
115
|
|
|
* |
116
|
|
|
* @param string $id The ID to check |
117
|
|
|
* |
118
|
|
|
* @return bool True if valid, otherwise false |
119
|
|
|
*/ |
120
|
3 |
|
protected function validateId($id) |
121
|
|
|
{ |
122
|
|
|
// Similar regex to the one in the parent method, this allows forward slashes |
123
|
|
|
// in the key name, except for at the start or end. |
124
|
3 |
|
return (boolean)preg_match('/^[^\\/]?[^\\?\\*:;{}\\\\\\n]+[^\\/]$/us', $id); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
protected function getFilesRecursive($dir, array &$result, $ext) |
128
|
|
|
{ |
129
|
1 |
|
$extensionLength = strlen($ext) + 1; // one is for the dot! |
130
|
1 |
|
$files = scandir($dir); |
131
|
1 |
|
foreach($files as $file) { |
132
|
1 |
|
if($file === '.' || $file === '..') { |
133
|
1 |
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if(is_dir($newDir = $dir . DIRECTORY_SEPARATOR . $file)) { |
137
|
1 |
|
$this->getFilesRecursive($newDir, $result, $ext); |
138
|
1 |
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
if(substr($file, -$extensionLength) !== '.' . $ext) { |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
$result[] = $dir . DIRECTORY_SEPARATOR . $file; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inherit |
153
|
|
|
*/ |
154
|
1 |
|
protected function getIdFromPath($path, $ext) |
155
|
|
|
{ |
156
|
1 |
|
return substr($path, strlen($this->path) + 1, -strlen($ext) - 1); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: