search paging
[phorkie.git] / src / phorkie / SetupCheck.php
1 <?php
2 namespace phorkie;
3
4 class SetupCheck
5 {
6     protected $deps = array(
7         'pear.php.net/VersionControl_Git'  => 'VersionControl_Git',
8         'pear.twig-project.org/Twig'       => 'Twig_Autoloader',
9         'pear.php.net/Date_HumanDiff'      => 'Date_HumanDiff',
10         'pear.php.net/Services_Libravatar' => 'Services_Libravatar',
11     );
12
13     protected $writableDirs;
14
15
16     public function __construct()
17     {
18         $cfg = $GLOBALS['phorkie']['cfg'];
19         $this->writableDirs = array(
20             'gitdir' => $cfg['gitdir'],
21             'workdir' => $cfg['workdir'],
22         );
23     }
24
25     public static function run()
26     {
27         $sc = new self();
28         $sc->checkDeps();
29         $sc->checkDirs();
30         $sc->checkGit();
31         $sc->checkDatabase();
32     }
33
34     public function checkDeps()
35     {
36         foreach ($this->deps as $package => $class) {
37             if (!class_exists($class, true)) {
38                 $this->fail('PEAR package not installed: ' . $package);
39             }
40         }
41     }
42
43     public function checkDirs()
44     {
45         foreach ($this->writableDirs as $name => $dir) {
46             if (!is_dir($dir)) {
47                 $this->fail($name . ' directory does not exist at ' . $dir);
48             }
49             if (!is_writable($dir)) {
50                 $this->fail($name . ' directory is not writable at ' . $dir);
51             }
52         }
53     }
54
55     public function checkGit()
56     {
57         $line = exec('git --version', $lines, $retval);
58         if ($retval !== 0) {
59             $this->fail('Running git executable failed.');
60         }
61         if (!preg_match('#^git version ([0-9.]+)$#', $line, $matches)) {
62             $this->fail('git version output format unexpected: ' . $line);
63         }
64         if (version_compare($matches[1], '1.7.5') < 0) {
65             $this->fail(
66                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
67             );
68         }
69     }
70
71     public function checkDatabase()
72     {
73         $dbs = new Database_Setup_Elasticsearch();
74         $dbs->setup();
75     }
76
77     public function fail($msg)
78     {
79         throw new Exception($msg);
80     }
81 }
82
83 ?>