|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpEpub; |
|
6
|
|
|
|
|
7
|
|
|
class ContentManager |
|
8
|
|
|
{ |
|
9
|
|
|
private readonly string $contentDirectory; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* ContentManager constructor. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $contentDirectory The directory containing the EPUB content. |
|
15
|
|
|
*/ |
|
16
|
14 |
|
public function __construct(string $contentDirectory) |
|
17
|
|
|
{ |
|
18
|
14 |
|
if (! is_dir($contentDirectory)) { |
|
19
|
2 |
|
throw new Exception("Content directory does not exist: {$contentDirectory}"); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
12 |
|
$this->contentDirectory = $contentDirectory; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Gets a list of content files in the EPUB. |
|
27
|
|
|
* |
|
28
|
|
|
* @return array<string> List of content file paths. |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function getContentList(): array |
|
31
|
|
|
{ |
|
32
|
1 |
|
$files = []; |
|
33
|
1 |
|
$iterator = new \RecursiveIteratorIterator( |
|
34
|
1 |
|
new \RecursiveDirectoryIterator($this->contentDirectory, \RecursiveDirectoryIterator::SKIP_DOTS) |
|
35
|
1 |
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** @var \SplFileInfo $file */ |
|
38
|
1 |
|
foreach ($iterator as $file) { |
|
39
|
1 |
|
if ($file->isFile()) { |
|
40
|
1 |
|
$files[] = $file->getRealPath(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return $files; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Adds a new content file to the EPUB. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $filePath The path where the content should be added. |
|
51
|
|
|
* @param string $content The content to add. |
|
52
|
|
|
* |
|
53
|
|
|
* @throws Exception If the file cannot be created. |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function addContent(string $filePath, string $content): void |
|
56
|
|
|
{ |
|
57
|
2 |
|
$fullPath = $this->contentDirectory . DIRECTORY_SEPARATOR . $filePath; |
|
58
|
2 |
|
if (file_put_contents($fullPath, $content) === false) { |
|
59
|
1 |
|
throw new Exception("Failed to add content to: {$fullPath}"); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Updates an existing content file in the EPUB. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $filePath The path of the content to update. |
|
67
|
|
|
* @param string $newContent The new content. |
|
68
|
|
|
* |
|
69
|
|
|
* @throws Exception If the file cannot be updated. |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function updateContent(string $filePath, string $newContent): void |
|
72
|
|
|
{ |
|
73
|
3 |
|
$fullPath = $this->contentDirectory . DIRECTORY_SEPARATOR . $filePath; |
|
74
|
3 |
|
if (! file_exists($fullPath)) { |
|
75
|
2 |
|
throw new Exception("Content file does not exist: {$fullPath}"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (file_put_contents($fullPath, $newContent) === false) { |
|
79
|
|
|
throw new Exception("Failed to update content in: {$fullPath}"); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Deletes a content file from the EPUB. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $filePath The path of the content to delete. |
|
87
|
|
|
* |
|
88
|
|
|
* @throws Exception If the file cannot be deleted. |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function deleteContent(string $filePath): void |
|
91
|
|
|
{ |
|
92
|
3 |
|
$fullPath = $this->contentDirectory . DIRECTORY_SEPARATOR . $filePath; |
|
93
|
3 |
|
if (! file_exists($fullPath)) { |
|
94
|
2 |
|
throw new Exception("Content file does not exist: {$fullPath}"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
if (! unlink($fullPath)) { |
|
98
|
|
|
throw new Exception("Failed to delete content from: {$fullPath}"); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieves the content of a file in the EPUB. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $filePath The path of the content to retrieve. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string The content of the file. |
|
108
|
|
|
* |
|
109
|
|
|
* @throws Exception If the file cannot be read. |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public function getContent(string $filePath): string |
|
112
|
|
|
{ |
|
113
|
3 |
|
$fullPath = $this->contentDirectory . DIRECTORY_SEPARATOR . $filePath; |
|
114
|
3 |
|
if (! file_exists($fullPath)) { |
|
115
|
2 |
|
throw new Exception("Content file does not exist: {$fullPath}"); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
$content = file_get_contents($fullPath); |
|
119
|
1 |
|
if ($content === false) { |
|
120
|
|
|
throw new Exception("Failed to read content from: {$fullPath}"); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return $content; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|