summaryrefslogtreecommitdiff
path: root/src/phorkie/Repositories.php
blob: ab97c566a47291fbd58158e785f3bfe7413ca039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace phorkie;

class Repositories
{
    public function __construct()
    {
        $this->workDir = $GLOBALS['phorkie']['cfg']['workdir'];
        $this->gitDir  = $GLOBALS['phorkie']['cfg']['gitdir'];
    }

    /**
     * @return Repository
     */
    public function createNew()
    {
        chdir($this->gitDir);
        $dirs = glob('*.git', GLOB_ONLYDIR);
        array_walk(
            $dirs,
            function ($dir) {
                return substr($dir, 0, -4);
            }
        );
        sort($dirs, SORT_NUMERIC);
        $n = end($dirs) + 1;

        chdir($this->workDir);
        $dir = $this->workDir . '/' . $n . '/';
        mkdir($dir, 0777);//FIXME
        $r = new Repository();
        $r->id = $n;
        $r->workDir = $dir;
        $r->gitDir = $this->gitDir . '/' . $n . '.git/';
        mkdir($r->gitDir, 0777);//FIXME

        return $r;
    }

    /**
     * Get a list of repository objects
     *
     * @param integer $page    Page number, beginning with 0, or "last"
     * @param integer $perPage Number of repositories per page
     *
     * @return array Array of Repositories first, number of repositories second
     */
    public function getList($page = 0, $perPage = 10)
    {
        chdir($this->gitDir);
        $dirs = glob('*.git', GLOB_ONLYDIR);
        sort($dirs, SORT_NUMERIC);
        if ($page === 'last') {
            //always show the last 10
            $page = intval(count($dirs) / $perPage);
            $start = count($dirs) - $perPage;
            if ($start < 0) {
                $start = 0;
            }
            $some = array_slice($dirs, $start, $perPage);
        } else {
            $some = array_slice($dirs, $page * $perPage, $perPage);
        }

        $repos = array();
        foreach ($some as $oneDir) {
            $r = new Repository();
            try {
                $r->loadById(substr($oneDir, 0, -4));
            } catch (\VersionControl_Git_Exception $e) {
                if (strpos($e->getMessage(), 'does not have any commits') !== false) {
                    //the git repo is broken as the initial commit
                    // has not been finished
                    continue;
                }
                throw $e;
            }
            $repos[] = $r;
        }
        return array($repos, count($dirs), $page);
    }
}

?>