f1fbc0f519e69d63d773e88db52b4713c8867a6d
[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     protected $elasticsearch;
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         $this->elasticsearch = $cfg['elasticsearch'];
27     }
28
29     public static function run()
30     {
31         $sc = new self();
32         $sc->checkDeps();
33         $sc->checkDirs();
34         $sc->checkGit();
35         if ($this->elasticsearch != '') {
36             $sc->checkDatabase();
37         }
38     }
39
40     public function checkDeps()
41     {
42         foreach ($this->deps as $package => $class) {
43             if (!class_exists($class, true)) {
44                 $this->fail('PEAR package not installed: ' . $package);
45             }
46         }
47     }
48
49     public function checkDirs()
50     {
51         foreach ($this->writableDirs as $name => $dir) {
52             if (!is_dir($dir)) {
53                 $this->fail($name . ' directory does not exist at ' . $dir);
54             }
55             if (!is_writable($dir)) {
56                 $this->fail($name . ' directory is not writable at ' . $dir);
57             }
58         }
59     }
60
61     public function checkGit()
62     {
63         $line = exec('git --version', $lines, $retval);
64         if ($retval !== 0) {
65             $this->fail('Running git executable failed.');
66         }
67         if (!preg_match('#^git version ([0-9.]+)$#', $line, $matches)) {
68             $this->fail('git version output format unexpected: ' . $line);
69         }
70         if (version_compare($matches[1], '1.7.5') < 0) {
71             $this->fail(
72                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
73             );
74         }
75     }
76
77     public function checkDatabase()
78     {
79         $es = parse_url($this->elasticsearch);
80         if (!preg_match("#/.+/#", $es['path'], $matches)) {
81             $this->fail('Improper elasticsearch url.  Elasticsearch requires a'
82                        . ' search domain to store your data. (e.g. http://localhost:9200/phorkie/)');
83         }
84         $dbs = new Database();
85         $dbs->getSetup()->setup();
86     }
87
88     public function fail($msg)
89     {
90         throw new Exception($msg);
91     }
92 }
93
94 ?>