Modules.php 4.51 KB
Newer Older
1 2 3
<?php
namespace ACP3\Core;

4
use ACP3\Core\Modules\Helper\ControllerActionExists;
Tino Goratsch's avatar
Tino Goratsch committed
5
use ACP3\Core\Modules\ModuleInfoCache;
6
use ACP3\Core\Modules\Vendors;
7
use ACP3\Modules\ACP3\System;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9 10 11 12 13 14 15 16

/**
 * Class Modules
 * @package ACP3\Core
 */
class Modules
{
    /**
17
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
18 19
     */
    protected $container;
20 21 22 23
    /**
     * @var \ACP3\Core\Modules\Helper\ControllerActionExists
     */
    protected $controllerActionExists;
24
    /**
Tino Goratsch's avatar
Tino Goratsch committed
25
     * @var \ACP3\Core\Modules\ModuleInfoCache
26
     */
Tino Goratsch's avatar
Tino Goratsch committed
27
    protected $moduleInfoCache;
28
    /**
29
     * @var \ACP3\Core\Modules\Vendors
30
     */
31
    protected $vendors;
32 33 34
    /**
     * @var array
     */
Tino Goratsch's avatar
Tino Goratsch committed
35
    private $modulesInfo = [];
36 37 38
    /**
     * @var array
     */
39
    private $allModules = [];
40 41

    /**
42
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
43
     * @param \ACP3\Core\Modules\Helper\ControllerActionExists          $controllerActionExists
Tino Goratsch's avatar
Tino Goratsch committed
44
     * @param \ACP3\Core\Modules\ModuleInfoCache                        $moduleInfoCache
45
     * @param \ACP3\Core\Modules\Vendors                                $vendors
46 47
     */
    public function __construct(
48
        ContainerInterface $container,
49
        ControllerActionExists $controllerActionExists,
Tino Goratsch's avatar
Tino Goratsch committed
50 51
        ModuleInfoCache $moduleInfoCache,
        Vendors $vendors
52 53 54
    )
    {
        $this->container = $container;
55
        $this->controllerActionExists = $controllerActionExists;
Tino Goratsch's avatar
Tino Goratsch committed
56
        $this->moduleInfoCache = $moduleInfoCache;
57
        $this->vendors = $vendors;
58 59 60
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
61
     * Returns, whether the given module controller action exists
62 63 64 65 66
     *
     * @param string $path
     *
     * @return boolean
     */
67
    public function controllerActionExists($path)
68
    {
69
        return $this->controllerActionExists->controllerActionExists($path);
70 71 72
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
73
     * Returns, whether a module is active or not
74 75 76 77 78 79 80 81 82 83 84 85
     *
     * @param string $module
     *
     * @return boolean
     */
    public function isActive($module)
    {
        $info = $this->getModuleInfo($module);
        return !empty($info) && $info['active'] === true;
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
86
     * Returns the available information about the given module
87 88 89 90 91 92 93 94
     *
     * @param string $module
     *
     * @return array
     */
    public function getModuleInfo($module)
    {
        $module = strtolower($module);
Tino Goratsch's avatar
Tino Goratsch committed
95 96
        if (empty($this->modulesInfo)) {
            $this->modulesInfo = $this->moduleInfoCache->getModulesInfoCache();
97
        }
Tino Goratsch's avatar
Tino Goratsch committed
98
        return !empty($this->modulesInfo[$module]) ? $this->modulesInfo[$module] : [];
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }

    /**
     * @param string $module
     *
     * @return integer
     */
    public function getModuleId($module)
    {
        $info = $this->getModuleInfo($module);
        return !empty($info) ? $info['id'] : 0;
    }

    /**
113
     * Checks, whether a modules in currently installed or not
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
     *
     * @param string $moduleName
     *
     * @return boolean
     */
    public function isInstalled($moduleName)
    {
        $info = $this->getModuleInfo($moduleName);
        return !empty($info) && $info['installed'] === true;
    }

    /**
     * Returns all currently installed AND active modules
     *
     * @return array
     */
    public function getActiveModules()
    {
        $modules = $this->getAllModules();

        foreach ($this->allModules as $key => $values) {
            if ($values['active'] === false) {
                unset($modules[$key]);
            }
        }

        return $modules;
    }

    /**
     * Returns all currently installed modules
     *
146
     * @return array
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
     */
    public function getInstalledModules()
    {
        $modules = $this->getAllModules();

        foreach ($this->allModules as $key => $values) {
            if ($values['installed'] === false) {
                unset($modules[$key]);
            }
        }

        return $modules;
    }

    /**
162
     * Returns an alphabetically sorted array of all found ACP3 modules
163
     *
164
     * @return array
165 166 167 168
     */
    public function getAllModules()
    {
        if (empty($this->allModules)) {
169 170
            foreach ($this->vendors->getVendors() as $vendor) {
                foreach (Filesystem::scandir(MODULES_DIR . $vendor . '/') as $module) {
171 172 173 174 175 176 177 178 179 180 181 182 183
                    $info = $this->getModuleInfo($module);
                    if (!empty($info)) {
                        $this->allModules[$info['name']] = $info;
                    }
                }
            }

            ksort($this->allModules);
        }

        return $this->allModules;
    }
}