Test Failed
Push — master ( ecd01f...e22447 )
by Vítězslav
03:26
created

Shared::takeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Všeobecně sdílený objekt frameworku.
4
 * Tento objekt je automaticky přez svůj singleton instancován do každého Ease*
5
 * objektu.
6
 * Poskytuje kdykoliv přístup k často volaným objektům framworku jako například
7
 * uživatel, databáze, webstránka nebo logy.
8
 * Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu.
9
 *
10
 * @author    Vitex <[email protected]>
11
 * @copyright 2009-2018 [email protected] (G)
12
 */
13
14
namespace Ease;
15
16
/**
17
 * Všeobecně sdílený objekt frameworku.
18
 * Tento objekt je automaticky přez svůj singleton instancován do každého Ease*
19
 * objektu.
20
 * Poskytuje kdykoliv přístup k často volaným objektům framworku jako například
21
 * uživatel, databáze, webstránka nebo logy.
22
 * Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu.
23
 *
24
 * @copyright 2009-2016 [email protected] (G)
25
 * @author    Vitex <[email protected]>
26
 */
27
class Shared extends Atom
28
{
29
    /**
30
     * Pole konfigurací.
31
     *
32
     * @var array
33
     */
34
    public $configuration = [];
35
36
    /**
37
     * Saves obejct instace (singleton...).
38
     *
39
     * @var Shared
40
     */
41
    private static $instance = null;
42
43
    /**
44
     * Array of Status Messages
45
     * @var array of Logger\Message
46
     */
47
    public $messages = [];
48
49
    /**
50
     * Inicializace sdílené třídy.
51
     */
52
    public function __construct()
53
    {
54
        $cgiMessages = [];
55
        $webMessages = [];
56
        $prefix      = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
57
        $msgFile     = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
58
        if (file_exists($msgFile) && is_readable($msgFile) && filesize($msgFile)
59
            && is_writable($msgFile)) {
60
            $cgiMessages = unserialize(file_get_contents($msgFile));
61
            file_put_contents($msgFile, '');
62
        }
63
64
        if (defined('EASE_APPNAME')) {
65
            if (isset($_SESSION[constant('EASE_APPNAME')]['EaseMessages'])) {
66
                $webMessages = $_SESSION[constant('EASE_APPNAME')]['EaseMessages'];
67
                unset($_SESSION[constant('EASE_APPNAME')]['EaseMessages']);
68
            }
69
        } else {
70
            if (isset($_SESSION['EaseMessages'])) {
71
                $webMessages = $_SESSION['EaseMessages'];
72
                unset($_SESSION['EaseMessages']);
73
            }
74
        }
75
        $this->statusMessages = is_array($cgiMessages) ? array_merge($cgiMessages,
76
                $webMessages) : $webMessages;
77
    }
78
79
    /**
80
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako konstruktor)
81
     * se bude v ramci behu programu pouzivat pouze jedna jeho Instance (ta prvni).
82
     *
83
     * @param string $class název třídy jenž má být zinstancována
84
     *
85
     * @link   http://docs.php.net/en/language.oop5.patterns.html Dokumentace a priklad
86
     *
87
     * @return \Ease\Shared
88
     */
89
    public static function singleton()
90
    {
91
        if (!isset(self::$instance)) {
92
            self::$instance = new self();
93
        }
94
        return self::$instance;
95
    }
96
97
    /**
98
     * Vrací se.
99
     *
100
     * @return Shared
101
     */
102
    public static function &instanced()
103
    {
104
        $easeShared = self::singleton();
105
106
        return $easeShared;
107
    }
108
109
    /**
110
     * Nastavuje hodnotu konfiguračního klíče.
111
     *
112
     * @param string $configName  klíč
113
     * @param mixed  $configValue hodnota klíče
114
     */
115
    public function setConfigValue($configName, $configValue)
116
    {
117
        $this->configuration[$configName] = $configValue;
118
    }
119
120
    /**
121
     * Vrací konfigurační hodnotu pod klíčem.
122
     *
123
     * @param string $configName klíč
124
     *
125
     * @return mixed
126
     */
127
    public function getConfigValue($configName)
128
    {
129
        return array_key_exists($configName, $this->configuration) ? $this->configuration[$configName]
130
                : null;
131
    }
132
133
    /**
134
     * Vrací instanci objektu logování.
135
     *
136
     * @return Logger
137
     */
138
    public static function logger()
139
    {
140
        return Logger\Regent::singleton();
141
    }
142
143
    /**
144
     * Write remaining messages to temporary file.
145
     */
146
    public function __destruct()
147
    {
148
        if (php_sapi_name() == 'cli') {
149
            $prefix       = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
150
            $messagesFile = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
151
            file_put_contents($messagesFile, serialize($this->statusMessages));
152
        } else {
153
            if (defined('EASE_APPNAME')) {
154
                $_SESSION[constant('EASE_APPNAME')]['EaseMessages'] = $this->statusMessages;
155
            } else {
156
                $_SESSION['EaseMessages'] = $this->statusMessages;
157
            }
158
        }
159
    }
160
161
    /**
162
     * Load Configuration values from json file $this->configFile and define UPPERCASE keys
163
     *
164
     * @param string  $configFile      Path to file with configuration
165
     * @param boolean $defineConstants false to do not define constants
166
     *
167
     * @return array full configuration array
168
     */
169
    public function loadConfig($configFile, $defineConstants = false)
170
    {
171
        if (!file_exists($configFile)) {
172
            throw new Exception('Config file '.(realpath($configFile) ? realpath($configFile)
173
                        : $configFile).' does not exist');
174
        }
175
        $configuration = json_decode(file_get_contents($configFile), true);
176
        foreach ($configuration as $configKey => $configValue) {
177
            if ($defineConstants && (strtoupper($configKey) == $configKey) && (!defined($configKey))) {
178
                define($configKey, $configValue);
179
            } else {
180
                $this->setConfigValue($configKey, $configValue);
181
            }
182
            $this->configuration[$configKey] = $configValue;
183
        }
184
185
        if (array_key_exists('debug', $this->configuration)) {
186
            $this->debug = boolval($this->configuration['debug']);
187
        }
188
189
        return $this->configuration;
190
    }
191
}
192