Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
helpers_InstallHelper
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 initContainer
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 installRecursively
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
72
 install
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getInstaller
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 log
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2015 (original work) Open Assessment Technologies SA;
19 *
20 *
21 */
22
23/**
24 * Helper class for instalation.
25 */
26class helpers_InstallHelper
27{
28    /**
29     * @var \Pimple\Container
30     */
31    protected static $container;
32
33    /**
34     * @var \Psr\Log\LoggerInterface
35     */
36    protected static $logger;
37
38    /**
39     * Initialize the container and the logger.
40     *
41     * @param \Pimple\Container $container
42     */
43    public static function initContainer($container)
44    {
45        if ($container instanceof \Pimple\Container) {
46            static::$container = $container;
47            static::$logger = static::$container
48                ->offsetGet(\oat\oatbox\log\LoggerService::SERVICE_ID)
49                ->getLogger();
50        }
51    }
52
53    /**
54     *
55     * @param array $extensionIDs
56     * @param array $installData
57     * @throws common_exception_Error
58     * @throws common_ext_ExtensionException
59     * @return array installed extensions ids
60     */
61    public static function installRecursively($extensionIDs, $installData = [])
62    {
63        $toInstall = [];
64        $installed = [];
65        foreach ($extensionIDs as $id) {
66            $ext = common_ext_ExtensionsManager::singleton()->getExtensionById($id);
67            if (!common_ext_ExtensionsManager::singleton()->isInstalled($ext->getId())) {
68                static::log('d', 'Extension ' . $id . ' needs to be installed');
69                $toInstall[$id] = $ext;
70            }
71        }
72
73        while (!empty($toInstall)) {
74            $modified = false;
75            foreach ($toInstall as $key => $extension) {
76                // if all dependencies are installed
77                static::log('d', 'Considering extension ' . $key);
78                $allInstalled   = array_keys(common_ext_ExtensionsManager::singleton()->getinstalledextensions());
79                $missing    = array_diff(array_keys($extension->getDependencies()), $allInstalled);
80                if (count($missing) == 0) {
81                    static::install($extension, $installData);
82                    $installed[] = $extension->getId();
83                    static::log('i', 'Extension ' . $extension->getId() . ' installed');
84                    unset($toInstall[$key]);
85                    $modified = true;
86                    break;
87                } else {
88                    $missing = array_diff($missing, array_keys($toInstall));
89                    foreach ($missing as $extID) {
90                        static::log('d', 'Extension ' . $extID . ' is required but missing, added to install list');
91                        $toInstall = [$extID => common_ext_ExtensionsManager::singleton()->getExtensionById($extID)]
92                            + $toInstall;
93                        $modified = true;
94                    }
95                }
96            }
97            // no extension could be installed, and no new requirements was added
98            if (!$modified) {
99                throw new \common_exception_Error('Unfulfilable/Cyclic reference found in extensions');
100            }
101        }
102        return $installed;
103    }
104
105    protected static function install($extension, $installData)
106    {
107        $importLocalData = (isset($installData['import_local']) && $installData['import_local'] == true);
108        $extinstaller = static::getInstaller($extension, $importLocalData);
109
110        helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::LONG);
111        $extinstaller->install();
112        helpers_TimeOutHelper::reset();
113        ;
114    }
115
116    protected static function getInstaller($extension, $importLocalData)
117    {
118        $instance = new \common_ext_ExtensionInstaller($extension, $importLocalData);
119        $instance->initContainer(static::$container);
120
121        return $instance;
122    }
123
124    /**
125     * Log message
126     *
127     * @see common_Logger class
128     *
129     * @param string $logLevel
130     * <ul>
131     *   <li>'w' - warning</li>
132     *   <li>'t' - trace</li>
133     *   <li>'d' - debug</li>
134     *   <li>'i' - info</li>
135     *   <li>'e' - error</li>
136     *   <li>'f' - fatal</li>
137     * </ul>
138     * @param string $message
139     * @param array $tags
140     */
141    public static function log($logLevel, $message, $tags = [])
142    {
143        if (static::$logger instanceof \Psr\Log\LoggerInterface) {
144            static::$logger->log(
145                common_log_Logger2Psr::getPsrLevelFromCommon($logLevel),
146                $message
147            );
148        }
149        if (method_exists('common_Logger', $logLevel)) {
150            call_user_func('common_Logger::' . $logLevel, $message, $tags);
151        }
152    }
153}