use repository and file classes
authorChristian Weiske <cweiske@cweiske.de>
Mon, 26 Mar 2012 06:05:46 +0000 (08:05 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 26 Mar 2012 06:05:46 +0000 (08:05 +0200)
data/templates/display.htm
data/templates/exception.htm [new file with mode: 0644]
src/Phorkie/Exception.php [new file with mode: 0644]
src/Phorkie/Exception/Input.php [new file with mode: 0644]
src/Phorkie/Exception/NotFound.php [new file with mode: 0644]
src/Phorkie/File.php [new file with mode: 0644]
src/Phorkie/Repository.php [new file with mode: 0644]
www/display.php
www/index.php
www/www-header.php

index 1f33b61ae1a67a1c285c81e1e7ee2339cd4c1fe4..ea2e2cd8a69f827ca4bb8b4e755a69d7bce13657 100644 (file)
@@ -2,15 +2,15 @@
 {% block title %}{{description}}{% endblock %}
 
 {% block content %}
-<h1>{{description}}</h1>
-<p><a href="{{links.edit}}">edit</a></p>
-{% for file in files %}
+<h1>{{repo.getDescription}}</h1>
+<p><a href="{{repo.getLink('edit')}}">edit</a></p>
+{% for file in repo.getFiles %}
 <div>
- <h2>{{file.filename}}</h2>
+ <h2>{{file.getFilename}}</h2>
  <p>
-  <a href="{{file.raw}}">raw</a>
+  <a href="{{file.getLink('raw')}}">raw</a>
  </p>
- <pre>{{file.content}}</pre>
+ <pre>{{file.getContent}}</pre>
 </div>
 {% endfor %}
 {% endblock %}
diff --git a/data/templates/exception.htm b/data/templates/exception.htm
new file mode 100644 (file)
index 0000000..805342a
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+ <head>
+  <!--<link rel="stylesheet" href="phorkie.css" />-->
+  <title>Error - Phorkie</title>
+ </head>
+ <body>
+  <article>
+   <h1>Error</h1>
+   <p>
+    {{exception.getMessage}}
+   </p>
+  </article>
+ </body>
+</html>
diff --git a/src/Phorkie/Exception.php b/src/Phorkie/Exception.php
new file mode 100644 (file)
index 0000000..03031ef
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+namespace Phorkie;
+
+class Exception extends \Exception
+{
+    public $httpStatusCode = 500;
+}
+
+?>
diff --git a/src/Phorkie/Exception/Input.php b/src/Phorkie/Exception/Input.php
new file mode 100644 (file)
index 0000000..2b675c1
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+namespace Phorkie;
+
+/**
+ * Input from e.g. the URL is invalid, like a non-numeric string when one was
+ * expected
+ */
+class Exception_Input extends Exception
+{
+    public function __construct($msg = '', $code = 0)
+    {
+        parent::__construct($msg, $code);
+        $this->httpStatusCode = 400;
+    }
+}
+
+?>
diff --git a/src/Phorkie/Exception/NotFound.php b/src/Phorkie/Exception/NotFound.php
new file mode 100644 (file)
index 0000000..a62d1f1
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+namespace Phorkie;
+
+/**
+ * Something could not be found
+ */
+class Exception_NotFound extends Exception
+{
+    public function __construct($msg = '', $code = 0)
+    {
+        parent::__construct($msg, $code);
+        $this->httpStatusCode = 404;
+    }
+}
+
+?>
diff --git a/src/Phorkie/File.php b/src/Phorkie/File.php
new file mode 100644 (file)
index 0000000..f12d837
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+namespace Phorkie;
+
+class File
+{
+    /**
+     * Full path to the file
+     *
+     * @var string
+     */
+    public $path;
+
+    /**
+     * Repository this file belongs to
+     *
+     * @var string
+     */
+    public $repo;
+
+    public function __construct($path, Repository $repo)
+    {
+        $this->path = $path;
+        $this->repo = $repo;
+    }
+
+    /**
+     * Get filename relative to the repository path
+     *
+     * @return string
+     */
+    public function getFilename()
+    {
+        return basename($this->path);
+    }
+
+    /**
+     * Returns the type of the file, as used internally by Phorkie
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return substr($this->path, strrpos($this->path, '.') + 1);
+    }
+
+    public function getContent()
+    {
+        return file_get_contents($this->path);
+    }
+
+    /**
+     * Get a link to the file
+     *
+     * @param string $type Link type. Supported are:
+     *                     - "raw"
+     *                     - "display"
+     *
+     * @return string
+     */
+    public function getLink($type)
+    {
+        if ($type == 'raw') {
+            return '/' . $this->repo->id . '/raw/' . $this->getFilename();
+        }
+        throw new Exception('Unknown type');
+    }
+}
+
+?>
\ No newline at end of file
diff --git a/src/Phorkie/Repository.php b/src/Phorkie/Repository.php
new file mode 100644 (file)
index 0000000..ae01f6c
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+namespace Phorkie;
+
+
+class Repository
+{
+    /**
+     * Repository ID (number in repositories directory)
+     *
+     * @var integer
+     */
+    public $id;
+
+    /**
+     * Full path to the git repository
+     *
+     * @var string
+     */
+    public $repoDir;
+
+    /**
+     * Load Repository data from GET-Request
+     *
+     * @return void
+     *
+     * @throws Exception When something is wrong
+     */
+    public function loadFromRequest()
+    {
+        if (!isset($_GET['id'])) {
+            throw new Exception_Input('Paste ID missing');
+        }
+        if (!is_numeric($_GET['id'])) {
+            throw new Exception_Input('Paste ID not numeric');
+        }
+        $this->id = (int)$_GET['id'];
+
+        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
+        if (!is_dir($repoDir)) {
+            throw new Exception_NotFound('Paste not found');
+        }
+        $this->repoDir = $repoDir;
+    }
+
+    /**
+     * Loads the list of files in this repository
+     *
+     * @return File[] Array of files
+     */
+    public function getFiles()
+    {
+        $files = glob($this->repoDir . '/*');
+        $arFiles = array();
+        foreach ($files as $path) {
+            $arFiles[] = new File($path, $this);
+        }
+        return $arFiles;
+    }
+
+    public function getDescription()
+    {
+        return file_get_contents($this->repoDir . '/.git/description');
+    }
+
+    /**
+     * Get a link to the repository
+     *
+     * @param string $type Link type. Supported are:
+     *                     - "edit"
+     *                     - "display"
+     *
+     * @return string
+     */
+    public function getLink($type)
+    {
+        if ($type == 'edit') {
+            return '/' . $this->id . '/edit';
+        } else if ($type == 'display') {
+            return '/' . $this->id;
+        }
+        throw new Exception('Unknown type');
+    }
+
+}
+
+?>
index 5bd06bbb55ded4776a6b0fcec643243a3549649b..b6b7d7f7939242f9aa21505a35197087e1ed155b 100644 (file)
@@ -1,23 +1,14 @@
 <?php
+namespace Phorkie;
 /**
  * Display paste contents
- *
  */
 require_once 'www-header.php';
 
-if (!isset($_GET['id'])) {
-    errout(400, 'Paste ID missing');
-}
-if (!is_numeric($_GET['id'])) {
-    errout(400, 'Paste ID not numeric');
-}
-$id = (int)$_GET['id'];
-$repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $id;
-if (!is_dir($repoDir)) {
-    errout(404, 'Paste not found');
-}
+$repo = new Repository();
+$repo->loadFromRequest();
 
-$files = glob($repoDir . '/*');
+/*
 $tplFiles = array();
 foreach ($files as $file) {
     $tplFile = array();
@@ -28,15 +19,19 @@ foreach ($files as $file) {
     $tplFile['raw'] = '/' . $id . '/raw/' . $tplFile['filename'];
     $tplFiles[] = $tplFile;
 }
+*/
 
 render(
     'display',
     array(
+        'repo' => $repo,
+        /*
         'description' => file_get_contents($repoDir . '/.git/description'),
         'files' => $tplFiles,
         'links' => array(
             'edit' => '/' . $id . '/edit'
         )
+        */
     )
 );
 ?>
index 5276bf2c96f6c14f436f34eec566006357317754..31aef7d0d35257db0c5dd90a4f082a097d49237d 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+namespace Phorkie;
 /**
  * Show paste creation form
  *
index 28607da5c123b406203525e89820bb69b0e883b3..8b99234a0b091216d5129158a93c8030fcffebb4 100644 (file)
@@ -1,11 +1,38 @@
 <?php
+namespace Phorkie;
+set_include_path(
+    __DIR__ . '/../src/'
+    . PATH_SEPARATOR . get_include_path()
+);
+spl_autoload_register(
+    function ($class) {
+        $file = str_replace(array('\\', '_'), '/', $class) . '.php';
+        $hdl = @fopen($file, 'r', true);
+        if ($hdl !== false) {
+            fclose($hdl);
+            require $file;
+        }
+    }
+);
+set_exception_handler(
+    function ($e) {
+        if ($e instanceof Exception) {
+            header('HTTP/1.0 ' . $e->httpStatusCode);
+        } else {
+            header('HTTP/1.0 500 Internal server error');
+        }
+        render('exception', array('exception' => $e));
+        exit();
+    }
+);
+
 require_once __DIR__ . '/../data/config.default.php';
 require_once 'VersionControl/Git.php';
 require_once 'Twig/Autoloader.php';
-Twig_Autoloader::register();
+\Twig_Autoloader::register();
 
-$loader = new Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
-$twig = new Twig_Environment(
+$loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
+$twig = new \Twig_Environment(
     $loader,
     array(
         //'cache' => '/path/to/compilation_cache',
@@ -23,14 +50,4 @@ function redirect($target)
     header('Location: /' . $target);
     exit();
 }
-function errout($statusCode, $message)
-{
-    header('HTTP/1.0 ' . $statusCode);
-    echo $message;
-    exit();
-}
-function get_type_from_file($file)
-{
-    return substr($file, strrpos($file, '.') + 1);
-}
 ?>
\ No newline at end of file