From 5f427dd38c8d47711ea73015076bb390761e05dd Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 26 Mar 2012 08:05:46 +0200 Subject: use repository and file classes --- data/templates/display.htm | 12 +++--- data/templates/exception.htm | 15 +++++++ src/Phorkie/Exception.php | 9 ++++ src/Phorkie/Exception/Input.php | 17 ++++++++ src/Phorkie/Exception/NotFound.php | 16 +++++++ src/Phorkie/File.php | 69 ++++++++++++++++++++++++++++++ src/Phorkie/Repository.php | 86 ++++++++++++++++++++++++++++++++++++++ www/display.php | 21 ++++------ www/index.php | 1 + www/www-header.php | 43 +++++++++++++------ 10 files changed, 257 insertions(+), 32 deletions(-) create mode 100644 data/templates/exception.htm create mode 100644 src/Phorkie/Exception.php create mode 100644 src/Phorkie/Exception/Input.php create mode 100644 src/Phorkie/Exception/NotFound.php create mode 100644 src/Phorkie/File.php create mode 100644 src/Phorkie/Repository.php diff --git a/data/templates/display.htm b/data/templates/display.htm index 1f33b61..ea2e2cd 100644 --- a/data/templates/display.htm +++ b/data/templates/display.htm @@ -2,15 +2,15 @@ {% block title %}{{description}}{% endblock %} {% block content %} -

{{description}}

-

edit

-{% for file in files %} +

{{repo.getDescription}}

+

edit

+{% for file in repo.getFiles %}
-

{{file.filename}}

+

{{file.getFilename}}

- raw + raw

-
{{file.content}}
+
{{file.getContent}}
{% endfor %} {% endblock %} diff --git a/data/templates/exception.htm b/data/templates/exception.htm new file mode 100644 index 0000000..805342a --- /dev/null +++ b/data/templates/exception.htm @@ -0,0 +1,15 @@ + + + + + Error - Phorkie + + +
+

Error

+

+ {{exception.getMessage}} +

+
+ + diff --git a/src/Phorkie/Exception.php b/src/Phorkie/Exception.php new file mode 100644 index 0000000..03031ef --- /dev/null +++ b/src/Phorkie/Exception.php @@ -0,0 +1,9 @@ + diff --git a/src/Phorkie/Exception/Input.php b/src/Phorkie/Exception/Input.php new file mode 100644 index 0000000..2b675c1 --- /dev/null +++ b/src/Phorkie/Exception/Input.php @@ -0,0 +1,17 @@ +httpStatusCode = 400; + } +} + +?> diff --git a/src/Phorkie/Exception/NotFound.php b/src/Phorkie/Exception/NotFound.php new file mode 100644 index 0000000..a62d1f1 --- /dev/null +++ b/src/Phorkie/Exception/NotFound.php @@ -0,0 +1,16 @@ +httpStatusCode = 404; + } +} + +?> diff --git a/src/Phorkie/File.php b/src/Phorkie/File.php new file mode 100644 index 0000000..f12d837 --- /dev/null +++ b/src/Phorkie/File.php @@ -0,0 +1,69 @@ +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 index 0000000..ae01f6c --- /dev/null +++ b/src/Phorkie/Repository.php @@ -0,0 +1,86 @@ +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'); + } + +} + +?> diff --git a/www/display.php b/www/display.php index 5bd06bb..b6b7d7f 100644 --- a/www/display.php +++ b/www/display.php @@ -1,23 +1,14 @@ 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' ) + */ ) ); ?> diff --git a/www/index.php b/www/index.php index 5276bf2..31aef7d 100644 --- a/www/index.php +++ b/www/index.php @@ -1,4 +1,5 @@ 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 -- cgit v1.2.3