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 --- 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 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) 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 (limited to 'src') 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'); + } + +} + +?> -- cgit v1.2.3