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