Completed
Push — master ( f6aa8b...0d64f5 )
by Thomas
19:48 queued 05:52
created

ShutDownManager::register()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Shutdown;
23
24
use OCP\Shutdown\IShutdownManager;
25
26
class ShutDownManager implements IShutdownManager {
27
28
	/** @var array[] */
29
	private $callbacks = [];
30
31
	/**
32
	 * ShutDownManager constructor.
33
	 *
34
	 * @codeCoverageIgnore
35
	 */
36
	public function __construct() {
37
		\register_shutdown_function([$this, 'run']);
38
	}
39
	
40
	/**
41
	 * Any kind of cleanup should be added with priority LOW.
42
	 * Anything which shall be processed before the cleanup routes better
43
	 * uses a value smaller then LOW
44
	 *
45
	 * @param \Closure $callback
46
	 * @param int $priority - the lower the number the higher is the priority
47
	 * @return void
48
	 * @since 11.0.0
49
	 */
50
	public function register(\Closure $callback, int $priority = self::LOW): void {
51
		if (!isset($this->callbacks[$priority])) {
52
			$this->callbacks[$priority] = [];
53
		}
54
		$this->callbacks[$priority][] = $callback;
55
	}
56
57
	public function run(): void {
58
		\ksort($this->callbacks);
59
		foreach ($this->callbacks as $callbacks) {
60
			foreach ($callbacks as $callback) {
61
				$callback();
62
			}
63
		}
64
	}
65
}
66