Merge branch 'autocracy'
[mdref/mdref] / mdref / Path.php
index b9212d2660e4540f9ccd6b96016da0d6ed21c006..efa208a31847dc9bafa6e22f436991cc32b1b050 100644 (file)
@@ -2,6 +2,9 @@
 
 namespace mdref;
 
+/**
+ * A path made out of a base dir and an thereof relative path name.
+ */
 class Path
 {
        /**
@@ -16,33 +19,65 @@ class Path
         */
        protected $baseDir = "";
        
+       /**
+        * @param string $baseDir
+        * @param string $path
+        */
        function __construct($baseDir = "", $path = "") {
                $this->baseDir = $baseDir;
                $this->path = $path;
        }
        
+       /**
+        * Create a copy of this path with a different path name
+        * 
+        * @param string $path
+        * @return \mdref\Path
+        */
        function __invoke($path) {
                $that = clone $this;
                $that->path = $path;
                return $that;
        }
        
+       /**
+        * Retrurns the full path as string
+        * @return string
+        */
        function __toString() {
                return $this->getFullPath();
        }
 
+       /**
+        * The base directory
+        * @return string
+        */
        function getBaseDir() {
                return $this->baseDir;
        }
        
+       /**
+        * The path name relative to the base dir
+        * @return string
+        */
        function getPathName() {
                return $this->path;
        }
        
+       /**
+        * The full path
+        * @param string $ext extension
+        * @return string
+        */
        function getFullPath($ext = "") {
                return $this->baseDir . DIRECTORY_SEPARATOR . $this->path . $ext;
        }
        
+       /**
+        * Retrieve a another subpath within the base dir
+        * @param type $path
+        * @return string
+        */
        function getSubPath($path) {
                return trim(substr($path, strlen($this->baseDir)), DIRECTORY_SEPARATOR);
        }
@@ -52,10 +87,14 @@ class Path
        }
        
        function toHtml() {
+               $head = sprintf("<h1>%s</h1>\n", htmlspecialchars(basename($this->getPathName())));
                if ($this->isFile()) {
-                       return htmlspecialchars(file_get_contents($this->getFullPath()));
+                       $html = htmlspecialchars(file_get_contents($this->getFullPath()));
                } elseif ($this->isFile("")) {
-                       return htmlspecialchars(file_get_contents($this->getFullPath("")));
+                       $html = htmlspecialchars(file_get_contents($this->getFullPath("")));
+               } else {
+                       throw new \http\Controller\Exception(404, "Not Found: {$this->getPathName()}");
                }
+               return $head . "<pre>" . $html ."</pre>"; 
        }
 }
\ No newline at end of file