From bfa1501efa1a49893208c794ea262a075b13fc1d Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 26 Mar 2012 23:38:53 +0200 Subject: [PATCH] use repositories class --- data/templates/display.htm | 5 ++++- src/Phorkie/Repositories.php | 27 +++++++++++++++++++++++++++ src/Phorkie/Repository.php | 8 ++++++++ www/.htaccess | 1 + www/index.php | 14 ++++++-------- www/www-header.php | 2 +- 6 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/Phorkie/Repositories.php diff --git a/data/templates/display.htm b/data/templates/display.htm index 43a76fc..6575617 100644 --- a/data/templates/display.htm +++ b/data/templates/display.htm @@ -3,7 +3,10 @@ {% block content %}

{{repo.getDescription}}

-

edit

+ {% for file in repo.getFiles %}

{{file.getFilename}}

diff --git a/src/Phorkie/Repositories.php b/src/Phorkie/Repositories.php new file mode 100644 index 0000000..6582ae8 --- /dev/null +++ b/src/Phorkie/Repositories.php @@ -0,0 +1,27 @@ +reposDir = $GLOBALS['phorkie']['cfg']['repos']; + } + + /** + * @return Repository + */ + public function createNew() + { + $n = basename(end(glob($this->reposDir . '/*', GLOB_ONLYDIR))) + 1; + $dir = $this->reposDir . '/' . $n . '/'; + mkdir($dir, 0777);//FIXME + $r = new Repository(); + $r->id = $n; + $r->repoDir = $dir; + return $r; + } + +} + +?> diff --git a/src/Phorkie/Repository.php b/src/Phorkie/Repository.php index 531ff68..f23db7e 100644 --- a/src/Phorkie/Repository.php +++ b/src/Phorkie/Repository.php @@ -42,6 +42,11 @@ class Repository $this->repoDir = $repoDir; } + public function getVc() + { + return new \VersionControl_Git($this->repoDir); + } + /** * Loads the list of files in this repository * @@ -84,6 +89,7 @@ class Repository * @param string $type Link type. Supported are: * - "edit" * - "display" + * - "fork" * * @return string */ @@ -93,6 +99,8 @@ class Repository return '/' . $this->id . '/edit'; } else if ($type == 'display') { return '/' . $this->id; + } else if ($type == 'fork') { + return '/' . $this->id . '/fork'; } throw new Exception('Unknown type'); } diff --git a/www/.htaccess b/www/.htaccess index a57b34d..bf82bc2 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -4,3 +4,4 @@ RewriteBase / RewriteRule ^([0-9]+)$ /display.php?id=$1 RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2 +RewriteRule ^([0-9]+)/fork$ /fork.php?id=$1 diff --git a/www/index.php b/www/index.php index 5bda390..8fc2405 100644 --- a/www/index.php +++ b/www/index.php @@ -14,13 +14,11 @@ require_once 'www-header.php'; if (isset($_POST['file'])) { //save - $repoDir = $GLOBALS['phorkie']['cfg']['repos']; - $n = count(glob($repoDir . '/*', GLOB_ONLYDIR)); - $dir = $repoDir . '/' . $n . '/'; - mkdir($dir, 0777);//FIXME - $vc = new \VersionControl_Git($dir); + $rs = new Repositories(); + $repo = $rs->createNew(); + $vc = $repo->getVc(); $vc->initRepository(); - file_put_contents($dir . '.git/description', $_POST['description']); + file_put_contents($repo->repoDir . '.git/description', $_POST['description']); foreach ($_POST['file'] as $num => $arFile) { if ($arFile['name'] != '') { @@ -28,7 +26,7 @@ if (isset($_POST['file'])) { } else { $fname = 'phork' . $num . '.' . $arFile['type']; } - $fpath = $dir . $fname; + $fpath = $repo->repoDir . $fname; file_put_contents($fpath, $arFile['content']); //fixme: let the class do that when it is able to $command = $vc->getCommand('add') @@ -40,7 +38,7 @@ if (isset($_POST['file'])) { ->setOption('author', 'Anonymous ') ->execute(); //redirect to phork - redirect($n); + redirect($repo->getLink('display')); } $phork = array( diff --git a/www/www-header.php b/www/www-header.php index 8b99234..520d793 100644 --- a/www/www-header.php +++ b/www/www-header.php @@ -47,7 +47,7 @@ function render($tplname, $vars) } function redirect($target) { - header('Location: /' . $target); + header('Location: ' . $target); exit(); } ?> \ No newline at end of file -- 2.30.2