1<?php
2
3
4namespace ComboStrap;
5
6
7use DateTime;
8
9/**
10 *
11 * @package ComboStrap
12 *
13 * The wiki file system is based
14 * on drive name (such as page and media)
15 * that locates a directory on the local file system
16 */
17class WikiFileSystem implements FileSystem
18{
19
20
21    public const SCHEME = 'wiki';
22
23
24    /**
25     * @var WikiFileSystem
26     */
27    private static WikiFileSystem $wikiFileSystem;
28
29    public static function getOrCreate(): WikiFileSystem
30    {
31        if (!isset(self::$wikiFileSystem)) {
32            self::$wikiFileSystem = new WikiFileSystem();
33        }
34        return self::$wikiFileSystem;
35    }
36
37    /**
38     * @param WikiPath $path
39     */
40    function exists(Path $path): bool
41    {
42        return FileSystems::exists($path->toLocalPath());
43    }
44
45    /**
46     * @param WikiPath $path
47     * @throws ExceptionNotFound
48     */
49    function getContent(Path $path): string
50    {
51        $localPath = $path->toLocalPath();
52        return FileSystems::getContent($localPath);
53    }
54
55    /**
56     * @param WikiPath $path
57     * @throws ExceptionNotFound
58     */
59    function getModifiedTime(Path $path): DateTime
60    {
61        return FileSystems::getModifiedTime($path->toLocalPath());
62    }
63
64    /**
65     * @param WikiPath $path
66     * @return DateTime
67     * @throws ExceptionNotFound
68     */
69    public function getCreationTime(Path $path): DateTime
70    {
71        return FileSystems::getCreationTime($path->toLocalPath());
72    }
73
74    /**
75     * @param WikiPath $path
76     */
77    public function delete(Path $path)
78    {
79        FileSystems::delete($path->toLocalPath());
80    }
81
82    /**
83     * @param WikiPath $path
84     */
85    public function getSize(Path $path)
86    {
87        return FileSystems::getSize($path->toLocalPath());
88    }
89
90    /**
91     * @param WikiPath $dirPath
92     * @return mixed
93     * @throws ExceptionCompile
94     */
95    public function createDirectory(Path $dirPath)
96    {
97        return FileSystems::createDirectory($dirPath->toLocalPath());
98    }
99
100    /**
101     * @param WikiPath $path
102     * @return bool
103     */
104    public function isDirectory(Path $path): bool
105    {
106        return WikiPath::isNamespacePath($path->toAbsoluteId());
107        // and not FileSystems::isDirectory($path->toLocalPath());
108    }
109
110    /**
111     * @param WikiPath $path
112     * @param string|null $type
113     * @return WikiPath[]
114     */
115    public function getChildren(Path $path, string $type = null): array
116    {
117
118        $children = LocalFileSystem::getOrCreate()->getChildren($path->toLocalPath(), $type);
119        $childrenWiki = [];
120        foreach ($children as $child) {
121            try {
122                $childrenWiki[] = WikiPath::createFromPathObject($child);
123            } catch (ExceptionCompile $e) {
124                // Should not happen
125                LogUtility::internalError("Unable to get back the wiki path from the local path. Error: {$e->getMessage()}");
126            }
127        }
128        return $childrenWiki;
129
130    }
131
132    /**
133     * @param WikiPath $path
134     * @param string $lastFullName
135     * @return Path
136     * @throws ExceptionNotFound
137     */
138    public function closest(Path $path, string $lastFullName): Path
139    {
140        return FileSystems::closest($path->toLocalPath(), $lastFullName);
141    }
142
143    /**
144     * @param WikiPath $path
145     * @return void
146     */
147    public function createRegularFile(Path $path)
148    {
149        FileSystems::createRegularFile($path->toLocalPath());
150    }
151
152    /**
153     * @param WikiPath $path
154     * @param string $content
155     * @return void
156     */
157    public function setContent(Path $path, string $content)
158    {
159        FileSystems::setContent($path->toLocalPath(), $content);
160    }
161}
162