Config.php 2.75 KB
Newer Older
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
<?php
namespace ACP3\Core;

use ACP3\Modules\ACP3\System;

/**
 * Manages the various module settings
 * @package ACP3\Core
 */
class Config
{
    /**
     * @var \ACP3\Modules\ACP3\System\Model
     */
    protected $systemModel;
    /**
     * @var \ACP3\Core\Cache
     */
    protected $coreCache;
    /**
     * @var array
     */
    protected $settings = [];

    /**
Tino Goratsch's avatar
Tino Goratsch committed
26
     * @param \ACP3\Core\Cache                $coreCache
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
     * @param \ACP3\Modules\ACP3\System\Model $systemModel
     */
    public function __construct(
        Cache $coreCache,
        System\Model $systemModel
    )
    {
        $this->coreCache = $coreCache;
        $this->systemModel = $systemModel;
    }

    /**
     * Erstellt/Verändert die Konfigurationsdateien für die Module
     *
     * @param array  $data
     * @param string $module
     *
     * @return bool
     */
    public function setSettings($data, $module)
    {
        $bool = $bool2 = false;
        $moduleId = $this->systemModel->getModuleId($module);
        if (!empty($moduleId)) {
            foreach ($data as $key => $value) {
                $updateValues = [
                    'value' => $value
                ];
                $where = [
                    'module_id' => $moduleId,
                    'name' => $key
                ];
                $bool = $this->systemModel->update($updateValues, $where, System\Model::TABLE_NAME_SETTINGS);
            }
61
            $bool2 = $this->saveCache();
62 63 64 65 66 67 68 69 70 71
        }

        return $bool !== false && $bool2 !== false;
    }

    /**
     * Setzt den Cache für die Einstellungen eines Moduls
     *
     * @return bool
     */
72
    protected function saveCache()
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    {
        $settings = $this->systemModel->getAllModuleSettings();

        $data = [];
        foreach ($settings as $setting) {
            if (is_int($setting['value'])) {
                $data[$setting['module_name']][$setting['name']] = (int)$setting['value'];
            } elseif (is_float($setting['value'])) {
                $data[$setting['module_name']][$setting['name']] = (float)$setting['value'];
            } else {
                $data[$setting['module_name']][$setting['name']] = $setting['value'];
            }
        }

        return $this->coreCache->save('settings', $data);
    }

    /**
     * Gibt den Inhalt der Konfigurationsdateien der Module aus
     *
93
     * @param string $module
94 95 96 97 98 99 100
     *
     * @return array
     */
    public function getSettings($module)
    {
        if ($this->settings === []) {
            if ($this->coreCache->contains('settings') === false) {
101
                $this->saveCache();
102 103 104 105 106 107 108 109
            }

            $this->settings = $this->coreCache->fetch('settings');
        }

        return isset($this->settings[$module]) ? $this->settings[$module] : [];
    }
}