summaryrefslogtreecommitdiffstats
path: root/modules/setup/library/Setup/Utils/EnableModuleStep.php
blob: 92af5b7c0c5ccd4ad1af994c4479ebe2766998c8 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Utils;

use Exception;
use Icinga\Application\Icinga;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\IcingaException;
use Icinga\Module\Setup\Step;

class EnableModuleStep extends Step
{
    protected $modulePaths;

    protected $moduleNames;

    protected $errors;

    protected $warnings;

    public function __construct(array $moduleNames)
    {
        $this->moduleNames = $moduleNames;

        $this->modulePaths = array();
        if (($appModulePath = realpath(Icinga::app()->getApplicationDir() . '/../modules')) !== false) {
            $this->modulePaths[] = $appModulePath;
        }
    }

    public function apply()
    {
        $moduleManager = Icinga::app()->getModuleManager();
        $moduleManager->detectInstalledModules($this->modulePaths);

        $success = true;
        foreach ($this->moduleNames as $moduleName) {
            try {
                $moduleManager->enableModule($moduleName);
            } catch (ConfigurationError $e) {
                $this->warnings[$moduleName] = $e;
            } catch (Exception $e) {
                $this->errors[$moduleName] = $e;
                $success = false;
            }
        }

        return $success;
    }

    public function getSummary()
    {
        // Enabling a module is like a implicit action, which does not need to be shown to the user...
    }

    public function getReport()
    {
        $okMessage = mt('setup', 'Module "%s" has been successfully enabled.');
        $failMessage = mt('setup', 'Module "%s" could not be enabled. An error occured:');

        $report = array();
        foreach ($this->moduleNames as $moduleName) {
            if (isset($this->errors[$moduleName])) {
                $report[] = sprintf($failMessage, $moduleName);
                $report[] = sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->errors[$moduleName]));
            } elseif (isset($this->warnings[$moduleName])) {
                $report[] = sprintf($failMessage, $moduleName);
                $report[] = sprintf(mt('setup', 'WARNING: %s'), $this->warnings[$moduleName]->getMessage());
            } else {
                $report[] = sprintf($okMessage, $moduleName);
            }
        }

        return $report;
    }
}