1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Rarst\Hugo\wprss2hugo\Filesystem; |
||
5 | |||
6 | use Rarst\Hugo\wprss2hugo\Serializer\Content; |
||
7 | use Rarst\Hugo\wprss2hugo\Serializer\Data; |
||
8 | use Symfony\Component\Filesystem\Filesystem; |
||
9 | |||
10 | /** |
||
11 | * Main storage implementation that writes files to filesystem. |
||
12 | */ |
||
13 | class Storage implements Write |
||
14 | { |
||
15 | /** @var string */ |
||
16 | private $basePath; |
||
17 | |||
18 | /** @var Filesystem */ |
||
19 | private $filesystem; |
||
20 | |||
21 | /** @var Data */ |
||
22 | private $frontMatter; |
||
23 | |||
24 | /** @var Content */ |
||
25 | private $content; |
||
26 | |||
27 | /** @var Data */ |
||
28 | private $data; |
||
29 | |||
30 | /** |
||
31 | * Set up with path and dependencies. |
||
32 | */ |
||
33 | public function __construct( |
||
34 | string $basePath, |
||
35 | Filesystem $filesystem, |
||
36 | Content $content, |
||
37 | Data $frontMatter, |
||
38 | Data $data |
||
39 | ) { |
||
40 | $this->basePath = $basePath; |
||
41 | $this->filesystem = $filesystem; |
||
42 | $this->frontMatter = $frontMatter; |
||
43 | $this->content = $content; |
||
44 | $this->data = $data; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Write a content file to a path. |
||
49 | */ |
||
50 | public function content(string $path, array $frontMatter = [], string $content = ''): void |
||
51 | { |
||
52 | switch ($this->frontMatter->type()) { |
||
53 | |||
54 | case 'toml': |
||
55 | $prefix = '+++' . PHP_EOL; |
||
56 | $suffix = '+++' . PHP_EOL; |
||
57 | break; |
||
58 | |||
59 | case 'yaml': |
||
60 | $prefix = '---' . PHP_EOL; |
||
61 | $suffix = '---' . PHP_EOL; |
||
62 | break; |
||
63 | |||
64 | default: |
||
65 | $prefix = ''; |
||
66 | $suffix = PHP_EOL; |
||
67 | } |
||
68 | |||
69 | if (false === stripos($content, '<p>')) { // Lacks wpautop() paragraphs. |
||
70 | $content = nl2br($content); |
||
71 | } |
||
72 | |||
73 | $this->save( |
||
74 | "{$path}.{$this->content->type()}", |
||
75 | $prefix |
||
76 | . $this->frontMatter->serialize(array_filter($frontMatter)) |
||
77 | . $suffix |
||
78 | . $this->content->serialize($content) |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Write a data file to a path. |
||
84 | */ |
||
85 | public function data(string $path, array $data = []): void |
||
86 | { |
||
87 | $this->save( |
||
88 | "{$path}.{$this->data->type()}", |
||
89 | $this->data->serialize($data) |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Convert a content file (e.g. `page-name.md`), if it exists, into an index one (e.g. `page-name/index.md`). |
||
95 | */ |
||
96 | private function makeIndex(string $path): void |
||
0 ignored issues
–
show
|
|||
97 | { |
||
98 | $filePath = "{$path}.{$this->content->type()}"; |
||
99 | |||
100 | if ($this->exists($filePath)) { |
||
101 | $this->move($filePath, "{$path}/_index.md"); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Write a string to a path. |
||
107 | */ |
||
108 | private function save(string $path, string $content): void |
||
109 | { |
||
110 | $this->filesystem->dumpFile($this->basePath . '/' . $path, $content); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Check if a file exists ata a given path. |
||
115 | */ |
||
116 | private function exists(string $path): bool |
||
117 | { |
||
118 | return $this->filesystem->exists($this->basePath . '/' . $path); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Move a file from a path to a destination path. |
||
123 | */ |
||
124 | private function move(string $from, string $to): void |
||
125 | { |
||
126 | try { |
||
127 | // Doing this in one move with `rename()` errors for whatever reason. |
||
128 | $this->filesystem->copy( |
||
129 | $this->basePath . '/' . $from, |
||
130 | $this->basePath . '/' . $to |
||
131 | ); |
||
132 | $this->filesystem->remove($this->basePath . '/' . $from); |
||
133 | } catch (\Exception $e) { |
||
134 | // TODO this might happen if files try to get on top of each other. Error in console about it? |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 |
This check looks for private methods that have been defined, but are not used inside the class.