dc3387bcc1faad481c6061e67c7b7a90025bbb64
[phorkie.git] / src / phorkie / Repositories.php
1 <?php
2 namespace phorkie;
3
4 class Repositories
5 {
6     public function __construct()
7     {
8         $this->workDir = $GLOBALS['phorkie']['cfg']['workdir'];
9         $this->gitDir  = $GLOBALS['phorkie']['cfg']['gitdir'];
10     }
11
12     /**
13      * @return Repository
14      */
15     public function createNew()
16     {
17         chdir($this->gitDir);
18         $dirs = glob('*.git', GLOB_ONLYDIR);
19         array_walk($dirs, function ($dir) { return substr($dir, 0, -4); });
20         sort($dirs, SORT_NUMERIC);
21         $n = end($dirs) + 1;
22
23         chdir($this->workDir);
24         $dir = $this->workDir . '/' . $n . '/';
25         mkdir($dir, 0777);//FIXME
26         $r = new Repository();
27         $r->id = $n;
28         $r->workDir = $dir;
29         $r->gitDir = $this->gitDir . '/' . $n . '.git/';
30         mkdir($r->gitDir, 0777);//FIXME
31
32         return $r;
33     }
34
35     /**
36      * Get a list of repository objects
37      *
38      * @param integer $page    Page number, beginning with 0
39      * @param integer $perPage Number of repositories per page
40      *
41      * @return array Array of Repositories
42      */
43     public function getList($page = 0, $perPage = 10)
44     {
45         chdir($this->gitDir);
46         $dirs = glob('*.git', GLOB_ONLYDIR);
47         sort($dirs, SORT_NUMERIC);
48
49         $some = array_slice($dirs, $page * $perPage, $perPage);
50         $repos = array();
51         foreach ($some as $oneDir) {
52             $r = new Repository();
53             $r->loadById(substr($oneDir, 0, -4));
54             $repos[] = $r;
55         }
56         return $repos;
57     }
58 }
59
60 ?>