openshift makefile
[mdref/mdref] / mdref / Path.php
1 <?php
2
3 namespace mdref;
4
5 /**
6 * A path made out of a base dir and an thereof relative path name.
7 */
8 class Path
9 {
10 /**
11 * Computed path
12 * @var string
13 */
14 protected $path = "";
15
16 /**
17 * The base directory where path is located
18 * @var string
19 */
20 protected $baseDir = "";
21
22 /**
23 * @param string $baseDir
24 * @param string $path
25 */
26 function __construct($baseDir = "", $path = "") {
27 $this->baseDir = $baseDir;
28 $this->path = $path;
29 }
30
31 /**
32 * Create a copy of this path with a different path name
33 *
34 * @param string $path
35 * @return \mdref\Path
36 */
37 function __invoke($path) {
38 $that = clone $this;
39 $that->path = $path;
40 return $that;
41 }
42
43 /**
44 * Retrurns the full path as string
45 * @return string
46 */
47 function __toString() {
48 return $this->getFullPath();
49 }
50
51 /**
52 * The base directory
53 * @return string
54 */
55 function getBaseDir() {
56 return $this->baseDir;
57 }
58
59 /**
60 * The path name relative to the base dir
61 * @return string
62 */
63 function getPathName() {
64 return $this->path;
65 }
66
67 /**
68 * The full path
69 * @param string $ext extension
70 * @return string
71 */
72 function getFullPath($ext = "") {
73 return $this->baseDir . DIRECTORY_SEPARATOR . $this->path . $ext;
74 }
75
76 /**
77 * Retrieve a another subpath within the base dir
78 * @param type $path
79 * @return string
80 */
81 function getSubPath($path) {
82 return trim(substr($path, strlen($this->baseDir)), DIRECTORY_SEPARATOR);
83 }
84
85 function isFile($ext = ".md") {
86 return is_file($this->getFullPath($ext));
87 }
88
89 function toHtml() {
90 $head = sprintf("<h1>%s</h1>\n", htmlspecialchars(basename($this->getPathName())));
91 if ($this->isFile()) {
92 $html = htmlspecialchars(file_get_contents($this->getFullPath()));
93 } elseif ($this->isFile("")) {
94 $html = htmlspecialchars(file_get_contents($this->getFullPath("")));
95 } else {
96 throw new \http\Controller\Exception(404, "Not Found: {$this->getPathName()}");
97 }
98 return $head . "<pre>" . $html ."</pre>";
99 }
100 }