Test Failed
Push — master ( 3f7c95...98e7c7 )
by Vítězslav
03:01
created

Shared::locale()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 10
rs 10
ccs 0
cts 2
cp 0
crap 12
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
     * Informuje zdali je objekt spuštěn v prostředí webové stránky nebo jako script.
38
     *
39
     * @var string web|cli
40
     */
41
    public $runType = null;
42
43
    /**
44
     * Odkaz na instanci objektu uživatele.
45
     *
46
     * @var User|Anonym
47
     */
48
    public $user = null;
49
50
    /**
51
     * Saves obejct instace (singleton...).
52
     *
53
     * @var Shared
54
     */
55
    private static $instance = null;
56
57
    /**
58
     * Název položky session s objektem uživatele.
59
     *
60
     * @var string
61
     */
62
    public static $userSessionName = 'User';
63
64
    /**
65
     * Logger live here
66
     * @var Logger\ToFile|Logger\ToMemory|Logger\ToSyslog
67
     */
68
    public static $log = null;
69
70
    /**
71
     * Array of Status Messages
72
     * @var array of Logger\Message
73
     */
74
    public $messages = [];
75
76
    /**
77
     * Inicializace sdílené třídy.
78
     */
79
    public function __construct()
80
    {
81
        $cgiMessages = [];
82
        $webMessages = [];
83
        $prefix      = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
84
        $msgFile     = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
85
        if (file_exists($msgFile) && is_readable($msgFile) && filesize($msgFile)
86
            && is_writable($msgFile)) {
87
            $cgiMessages = unserialize(file_get_contents($msgFile));
88
            file_put_contents($msgFile, '');
89
        }
90
91
        if (defined('EASE_APPNAME')) {
92
            if (isset($_SESSION[constant('EASE_APPNAME')]['EaseMessages'])) {
93
                $webMessages = $_SESSION[constant('EASE_APPNAME')]['EaseMessages'];
94
                unset($_SESSION[constant('EASE_APPNAME')]['EaseMessages']);
95
            }
96
        } else {
97
            if (isset($_SESSION['EaseMessages'])) {
98
                $webMessages = $_SESSION['EaseMessages'];
99
                unset($_SESSION['EaseMessages']);
100
            }
101
        }
102
        $this->statusMessages = is_array($cgiMessages) ? array_merge($cgiMessages,
103
                $webMessages) : $webMessages;
104
    }
105
106
    /**
107
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako konstruktor)
108
     * se bude v ramci behu programu pouzivat pouze jedna jeho Instance (ta prvni).
109
     *
110
     * @param string $class název třídy jenž má být zinstancována
111
     *
112
     * @link   http://docs.php.net/en/language.oop5.patterns.html Dokumentace a priklad
113
     *
114
     * @return \Ease\Shared
115
     */
116
    public static function singleton()
117
    {
118
        if (!isset(self::$instance)) {
119
            self::$instance = new self();
120
        }
121
        return self::$instance;
122
    }
123
124
    /**
125
     * Vrací se.
126
     *
127
     * @return Shared
128
     */
129
    public static function &instanced()
130
    {
131
        $easeShared = self::singleton();
132
133
        return $easeShared;
134
    }
135
136
    /**
137
     * Nastavuje hodnotu konfiguračního klíče.
138
     *
139
     * @param string $configName  klíč
140
     * @param mixed  $configValue hodnota klíče
141
     */
142
    public function setConfigValue($configName, $configValue)
143
    {
144
        $this->configuration[$configName] = $configValue;
145
    }
146
147
    /**
148
     * Vrací konfigurační hodnotu pod klíčem.
149
     *
150
     * @param string $configName klíč
151
     *
152
     * @return mixed
153
     */
154
    public function getConfigValue($configName)
155
    {
156
        return array_key_exists($configName, $this->configuration) ? $this->configuration[$configName]
157
                : null;
158
    }
159
160
    /**
161
     * Vrací instanci objektu logování.
162
     *
163
     * @return Logger
164
     */
165
    public static function logger()
166
    {
167
        return Logger\Regent::singleton();
168
    }
169
170
    /**
171
     * Take message to print / log
172
     * 
173
     * @param Logger\Message $message
174
     * 
175
     * @return boolean message accepted
176
     */
177
    public function takeMessage($message)
178
    {
179
        $this->messages[] = $message;
180
        $this->logger()->addToLog($message->caller, $message->body,
181
            $message->type);
182
        return $this->addStatusMessage($message->body, $message->type);
183
    }
184
185
    /**
186
     * Write remaining messages to temporary file.
187
     */
188
    public function __destruct()
189
    {
190
        if (php_sapi_name() == 'cli') {
191
            $prefix       = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
192
            $messagesFile = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
193
            file_put_contents($messagesFile, serialize($this->statusMessages));
194
        } else {
195
            if (defined('EASE_APPNAME')) {
196
                $_SESSION[constant('EASE_APPNAME')]['EaseMessages'] = $this->statusMessages;
197
            } else {
198
                $_SESSION['EaseMessages'] = $this->statusMessages;
199
            }
200
        }
201
    }
202
203
    /**
204
     * Load Configuration values from json file $this->configFile and define UPPERCASE keys
205
     *
206
     * @param string  $configFile      Path to file with configuration
207
     * @param boolean $defineConstants false to do not define constants
208
     *
209
     * @return array full configuration array
210
     */
211
    public function loadConfig($configFile, $defineConstants = false)
212
    {
213
        if (!file_exists($configFile)) {
214
            throw new Exception('Config file '.(realpath($configFile) ? realpath($configFile)
215
                        : $configFile).' does not exist');
216
        }
217
        $configuration = json_decode(file_get_contents($configFile), true);
218
        foreach ($configuration as $configKey => $configValue) {
219
            if ($defineConstants && (strtoupper($configKey) == $configKey) && (!defined($configKey))) {
220
                define($configKey, $configValue);
221
            } else {
222
                $this->setConfigValue($configKey, $configValue);
223
            }
224
            $this->configuration[$configKey] = $configValue;
225
        }
226
227
        if (array_key_exists('debug', $this->configuration)) {
228
            $this->debug = boolval($this->configuration['debug']);
229
        }
230
231
        return $this->configuration;
232
    }
233
}
234