Initial commit
This commit is contained in:
90
engine/Main/trait.events.php
Normal file
90
engine/Main/trait.events.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
// Работа с отслеживанием данных
|
||||
|
||||
defined('ROOT_DIR') || exit;
|
||||
|
||||
trait Events {
|
||||
private array $events = array();
|
||||
|
||||
// Добавить функцию в Событие
|
||||
public function event_add($name, $function_name, $weight = 10, $lock = false): bool
|
||||
{
|
||||
$event_name = strtolower($name);
|
||||
$this->event_create($name);
|
||||
|
||||
if($this->events[$event_name]["lock"]) return false;
|
||||
if($lock) $this->events[$event_name]["events"] = array();
|
||||
|
||||
$this->events[$event_name]["events"][] = array("function_name" => $function_name, "weight" => $weight);
|
||||
$this->events[$event_name]["lock"] = $lock;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Запустить Событие
|
||||
public function event_start($name, $args = array())
|
||||
{
|
||||
$event_name = strtolower($name);
|
||||
$this->event_create($name);
|
||||
|
||||
if(!is_array($this->events[$event_name])) return false;
|
||||
$events = $this->events[$event_name]["events"];
|
||||
|
||||
if($this->events[$event_name]["lock"]) {
|
||||
$func_name = $events[0]["function_name"];
|
||||
if(is_callable($func_name)) {
|
||||
return $func_name(...$args);
|
||||
}
|
||||
if(!function_exists($func_name)) return false;
|
||||
return $func_name(...$args);
|
||||
}
|
||||
|
||||
$results = array();
|
||||
foreach ($events as $key => $event) {
|
||||
$func_name = $event["function_name"];
|
||||
if(is_callable($func_name)) {
|
||||
$results[] = $func_name(...$args);
|
||||
} else {
|
||||
if(!function_exists($func_name)) return false;
|
||||
$results[] = $func_name(...$args);
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
// Поймать Событие
|
||||
public function event_form_capture(): bool
|
||||
{
|
||||
$values = array_replace_recursive($_GET, $_POST);
|
||||
foreach ($_FILES as $key => $field) {
|
||||
$file = $this->files_field_formate($key);
|
||||
if($file === false) continue;
|
||||
if(isset($values[$key])) $values[$key] = array_replace_recursive($values[$key], $file);
|
||||
else $values[$key] = $file;
|
||||
}
|
||||
if(!isset($values["b_event"])) return false;
|
||||
$this->event_start($values["b_event"], array($values));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Создать Событие для формы
|
||||
public function event_form($name)
|
||||
{
|
||||
$meta_dom = new DOM("input", false);
|
||||
$meta_dom->setAttribute("type", "hidden");
|
||||
$meta_dom->setAttribute("name", "b_event");
|
||||
$meta_dom->setAttribute("value", $name);
|
||||
echo $meta_dom->render();
|
||||
}
|
||||
|
||||
// Создать Событие
|
||||
private function event_create($name): bool
|
||||
{
|
||||
if(isset($this->events[$name])) return true;
|
||||
$this->events[$name] = array();
|
||||
$this->events[$name]["events"] = array();
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user