aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-03-26 08:05:46 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-03-26 08:05:46 +0200
commit5f427dd38c8d47711ea73015076bb390761e05dd (patch)
tree023a342befae896020e60581d7efaa91abcdcba4 /src
parent6d0777840e50ce98f3d96629b4e92bbdccd3001c (diff)
downloadphorkie-5f427dd38c8d47711ea73015076bb390761e05dd.tar.gz
phorkie-5f427dd38c8d47711ea73015076bb390761e05dd.zip
use repository and file classes
Diffstat (limited to 'src')
-rw-r--r--src/Phorkie/Exception.php9
-rw-r--r--src/Phorkie/Exception/Input.php17
-rw-r--r--src/Phorkie/Exception/NotFound.php16
-rw-r--r--src/Phorkie/File.php69
-rw-r--r--src/Phorkie/Repository.php86
5 files changed, 197 insertions, 0 deletions
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 @@
+<?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
index 0000000..2b675c1
--- /dev/null
+++ b/src/Phorkie/Exception/Input.php
@@ -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
index 0000000..a62d1f1
--- /dev/null
+++ b/src/Phorkie/Exception/NotFound.php
@@ -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
index 0000000..f12d837
--- /dev/null
+++ b/src/Phorkie/File.php
@@ -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
index 0000000..ae01f6c
--- /dev/null
+++ b/src/Phorkie/Repository.php
@@ -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');
+ }
+
+}
+
+?>