Passed
Push — master ( fa90af...614c44 )
by Alexey
05:19
created

Inji::unBlockParallel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 6
1
<?php
2
3
/**
4
 * Inji core
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Inji {
12
13
    /**
14
     * Static storage for core object
15
     *
16
     * @var Inji
17
     */
18
    public static $inst = null;
19
20
    /**
21
     * Dynamic events listeners
22
     *
23
     * @var array
24
     */
25
    private $_listeners = [];
26
27
    /**
28
     * Core config
29
     *
30
     * @var array
31
     */
32
    public static $config = [];
33
34
    /**
35
     * Static storage for anything
36
     *
37
     * @var array
38
     */
39
    public static $storage = [];
40
41
    /**
42
     * Stop executing code if this true after use Inji::$inst->stop() constuction in code
43
     *
44
     * @var boolean
45
     */
46
    public $exitOnStop = true;
47
    public $parallelLockFileStream = null;
48
49
    /**
50
     * Add event listener
51
     *
52
     * @param string $eventName
53
     * @param string $listenCode
54
     * @param array|string|closure $callback
55
     * @param boolean $save
56
     */
57 3
    public function listen($eventName, $listenCode, $callback, $save = false) {
58 3
        if ($save) {
59 1
            $config = Config::custom(App::$primary->path . '/config/events.php');
60 1
            $config[$eventName][$listenCode] = serialize($callback);
61 1
            Config::save(App::$primary->path . '/config/events.php', $config);
62 1
        } else {
63 2
            $this->_listeners[$eventName][$listenCode] = $callback;
64
        }
65 3
    }
66
67
    /**
68
     * Throw event
69
     *
70
     * @param string $eventName
71
     * @param mixed $eventObject
72
     * @return mixed
73
     */
74 3
    public function event($eventName, $eventObject = null) {
75
        $event = [
76 3
            'eventName' => $eventName,
77 3
            'eventObject' => $eventObject,
78 3
        ];
79
80 3
        $listeners = [];
81 3
        if (!empty($this->_listeners[$eventName])) {
82 2
            $listeners = $this->_listeners[$eventName];
83 2
        }
84 3
        $config = Config::custom(App::$primary->path . '/config/events.php');
85 3
        if (!empty($config[$eventName])) {
86 1
            foreach ($config[$eventName] as $listenCode => $callback) {
87 1
                $listeners[$listenCode] = (@unserialize($callback) !== false) ? unserialize($callback) : $callback;
88 1
            }
89 1
        }
90 3
        if ($listeners) {
91 3
            $iteration = 0;
92 3
            $calledBefore = [];
93 3
            foreach ($listeners as $listenCode => $callback) {
94 3
                $event['iteration'] = ++$iteration;
95 3
                $event['calledBefore'] = $calledBefore;
96 3
                if (is_callable($callback)) {
97 2
                    $event['eventObject'] = $callback($event);
98 3
                } elseif (is_array($callback) && isset($callback['callback'])) {
99 1
                    $event['eventObject'] = $callback['callback']($event, $callback);
100 1
                } else {
101
                    $event['eventObject'] = App::$cur->{$callback['module']}->{$callback['method']}($event, $callback);
102
                }
103 3
                $calledBefore[$iteration] = $listenCode;
104 3
            }
105 3
        }
106 3
        return $event['eventObject'];
107
    }
108
109
    /**
110
     * Unlisten event
111
     *
112
     * @param string $eventName
113
     * @param string $listenCode
114
     * @param boolean $save
115
     */
116 2
    public function unlisten($eventName, $listenCode, $save = false) {
117 2
        if ($save) {
118 1
            $config = Config::custom(App::$primary->path . '/config/events.php');
119 1
            if (!empty($config[$eventName][$listenCode])) {
120 1
                unset($config[$eventName][$listenCode]);
121 1
                Config::save(App::$primary->path . '/config/events.php', $config);
122 1
            }
123 1
        }
124 2
        if (!empty($this->_listeners[$eventName][$listenCode])) {
125 1
            unset($this->_listeners[$eventName][$listenCode]);
126 1
        }
127 2
    }
128
129
    public function stop() {
130
        if ($this->exitOnStop) {
131
            exit();
132
        }
133
        return false;
134
    }
135
136
    public function blockParallel() {
137
        $this->parallelLockFileStream = fopen('lock.file', 'w+');
138
        if (!flock($this->parallelLockFileStream, LOCK_EX | LOCK_NB)) {
139
            return false;
140
        }
141
        return true;
142
    }
143
144
    public function unBlockParallel() {
145
        if (is_resource($this->parallelLockFileStream)) {
146
            flock($this->parallelLockFileStream, LOCK_UN);
147
            fclose($this->parallelLockFileStream);
148
        }
149
    }
150
}