EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_Process.php
gehe zur Dokumentation dieser Datei
1 <?php
8 class Ego_Process {
9  private $id;
10  private $pid = 0;
11  private $path;
12  private $ttl;
13  private $format = 'Y-m-d H:i:s';
14  private $destruct = true;
15 
22  public function __construct(string $id = '', int $ttl = 3600) {
23  $this->id = $id ?: md5(microtime());
24  $this->path = $GLOBALS['egotec_conf']['tmp_dir'] . "pid_{$this->id}";
25  $this->ttl = $ttl;
26 
27  if ($id == '') {
28  // Aktuelle Prozess-ID verwenden
29  $this->pid = getmypid();
30 
31  // Neuen Prozess-Stempel anlegen
32  Ego_System::file_put_contents($this->path, json_encode([
33  'created' => date($this->format),
34  'pid' => $this->pid
35  ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
36 
37  // ...und am Ende des Prozesses löschen
38  register_shutdown_function([$this, 'remove']);
39  } else {
40  $value = $this->read();
41  if (!empty($value)) {
42  $this->pid = $value['pid'];
43  $this->destruct = false;
44  }
45  }
46  }
47 
51  public function __destruct() {
52  if ($this->destruct) {
53  $this->remove();
54  }
55  }
56 
62  public function id() : string {
63  return $this->id;
64  }
65 
71  public function pid() : int {
72  return $this->pid;
73  }
74 
80  private function read() : array {
81  if (Ego_System::file_exists($this->path)) {
82  $value = Ego_System::file_get_contents($this->path);
83  if (!empty($value)) {
84  $value = json_decode($value, true);
85  if (!empty($value)) {
86  return $value;
87  }
88  }
89  }
90  return [];
91  }
92 
100  public function exists() : bool {
101  $value = $this->read();
102  if (!empty($value)) {
103  if (function_exists('posix_getpgid') && !posix_getpgid($value['pid'])) {
104  return false;
105  }
106  return date($this->format, strtotime($value['created']) + $this->ttl) > date($this->format);
107  }
108  return false;
109  }
110 
116  public function remove() : void {
117  // Prozess-Stempel löschen
118  @unlink($this->path);
119  }
120 }
__construct(string $id='', int $ttl=3600)
Definition: Ego_Process.php:22
static file_put_contents($filename, $data, $flags=0, $context=null)
static file_get_contents($filename, $utf8=true, $context=null)