1<?php
2
3namespace ComboStrap;
4
5use DateTime;
6
7class MarkupFileSystem implements FileSystem
8{
9
10    const SCHEME = "markup";
11    const CANONICAL = "markup-file-system";
12
13    private static MarkupFileSystem $pageFileSystem;
14
15    public static function getOrCreate(): MarkupFileSystem
16    {
17        if (!isset(self::$pageFileSystem)) {
18            self::$pageFileSystem = new MarkupFileSystem();
19        }
20        return self::$pageFileSystem;
21    }
22
23
24    /**
25     * @param MarkupPath $path
26     * @return bool
27     */
28    function exists(Path $path): bool
29    {
30        return FileSystems::exists($path->getPathObject());
31    }
32
33    /**
34     * @param MarkupPath $path
35     * @throws ExceptionNotFound
36     */
37    function getContent(Path $path): string
38    {
39        return FileSystems::getContent($path->getPathObject());
40    }
41
42    /**
43     * @param MarkupPath $path
44     * @throws ExceptionNotFound
45     */
46    function getModifiedTime(Path $path): DateTime
47    {
48        return FileSystems::getModifiedTime($path->getPathObject());
49    }
50
51    /**
52     * @param MarkupPath $path
53     * @param string|null $type
54     * @return MarkupPath[]
55     */
56    public function getChildren(Path $path, string $type = null): array
57    {
58        /**
59         * A page is never a directory
60         * We get the parent directory, iterate over it and returns the children page
61         */
62        $pathObject = $path->getPathObject();
63        try {
64            $parent = $pathObject->getParent();
65        } catch (ExceptionNotFound $e) {
66            return [];
67        }
68        $childrenPage = [];
69        $childrenPath = FileSystems::getChildren($parent, $type);
70        foreach ($childrenPath as $child) {
71            if ($child->toUriString() === $pathObject->toUriString()) {
72                continue;
73            }
74            $childrenPage[] = MarkupPath::createPageFromPathObject($child);
75        }
76        return $childrenPage;
77    }
78
79    public function setContent(Path $path, string $content)
80    {
81        try {
82            FileSystems::setContent($path->toLocalPath(), $content);
83        } catch (ExceptionCast $e) {
84            throw new ExceptionRuntimeInternal("The path could not be cast to a local path", self::CANONICAL, 1, $e);
85        }
86    }
87
88    public function delete(Path $path)
89    {
90
91        try {
92            FileSystems::delete($path->toLocalPath());
93        } catch (ExceptionCast|ExceptionFileSystem $e) {
94            throw new ExceptionRuntimeInternal("The path could not be deleted", self::CANONICAL, 1, $e);
95        }
96
97    }
98}
99