check for services_libravatar
[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     }
32
33     public function checkDeps()
34     {
35         foreach ($this->deps as $package => $class) {
36             if (!class_exists($class, true)) {
37                 $this->fail('PEAR package not installed: ' . $package);
38             }
39         }
40     }
41
42     public function checkDirs()
43     {
44         foreach ($this->writableDirs as $name => $dir) {
45             if (!is_dir($dir)) {
46                 $this->fail($name . ' directory does not exist at ' . $dir);
47             }
48             if (!is_writable($dir)) {
49                 $this->fail($name . ' directory is not writable at ' . $dir);
50             }
51         }
52     }
53
54     public function checkGit()
55     {
56         $line = exec('git --version', $lines, $retval);
57         if ($retval !== 0) {
58             $this->fail('Running git executable failed.');
59         }
60         if (!preg_match('#^git version ([0-9.]+)$#', $line, $matches)) {
61             $this->fail('git version output format unexpected: ' . $line);
62         }
63         if (version_compare($matches[1], '1.7.5') < 0) {
64             $this->fail(
65                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
66             );
67         }
68     }
69
70     public function fail($msg)
71     {
72         throw new Exception($msg);
73     }
74 }
75
76 ?>