/
WebPhpDemon.php
101 lines (92 loc) · 3.09 KB
/
WebPhpDemon.php
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
26
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Class for work of Demon
*
* @version 1.01
* @package WebPhpDemon
* @class WebPhpDemon
* @author Alex Maximov <alex.maximov.freelance@gmail.com>
*
*/
namespace Kymyzek\WebPhpDemon;
class WebPhpDemon
{
// Конфигурация по умолчанию
private $demon = array(
'name' => 'controller.php',//имя демона
'dir' => __DIR__,// рабочий каталог работы демона
'wait' => 1, // собственная задержка работы демона, активна при отсутствии задач, 1 сек
);
/**
* WebPhpDemon constructor.
*/
function __construct() {
$dirs = array(
'params' => WPD_WORK_DIR . DIRECTORY_SEPARATOR . 'params', //Каталог сохранения параметров скриптов
'logs' => WPD_WORK_DIR . DIRECTORY_SEPARATOR . 'logs', //Логи работы
'states' => WPD_WORK_DIR . DIRECTORY_SEPARATOR . 'states', //Каталог сохранения текущих статусов работы скриптов
);
//Создаем структуру каталогов если они отсутствуют
foreach($dirs as $key => $dir) {
if(!is_dir($dir)){
mkdir($dir, 0777);
}
}
$this->demon = array_merge($this->demon, $dirs);
}
/**
* Запуск демона
*/
public function run() {
$set = array(
'command' => 'work',
);
$this->demon = $this->setParam($set);
chdir($this->demon['dir']);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
/* Система Windows */
pclose(popen('start /B cmd /C "php '. $this->demon['name'] .' >NUL 2>NUL"', 'r'));
} else {
/* Система *nix */
$command = 'php -f ' . $this->demon['name'] . ' > /dev/null 2>&1 &';
exec($command);
}
}
/**
* Остановка демона
*/
public function stop() {
$set = array(
'command' => 'stop',
);
$this->demon = $this->setParam($set);
}
/**
* Установка параметров конфигруации
* @param $params
* @return array|mixed
*/
public function setParam($params) {
$this->demon = $this->loadConfig();
$this->demon = array_merge($this->demon, $params);
$this->__saveConfig();
return $this->loadConfig();
}
/**
* Чтение конфигурации
* @return array|mixed
*/
public function loadConfig() {
$file = WPD_WORK_DIR . DIRECTORY_SEPARATOR . 'config.json';
if (is_file($file))
return json_decode(file_get_contents($file), true);
return $this->demon;
}
/**
* Сохранение конфигурации
*/
private function __saveConfig() {
$file = WPD_WORK_DIR . DIRECTORY_SEPARATOR . 'config.json';
file_put_contents($file,json_encode($this->demon));
}
}