Files
fusionpbx/core/events/resources/classes/events.php
frytimo 08001488f4 Allow namespace in auto loader (#7307)
* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove class_exists wrapper for class definitions

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove include statement of class file

* remove closing tag

* remove invalid method params

* remove closing tag

* remove closing tag

* Update auto_loader to load each class file in the project
Update the auto_loader class to use an include statement on each file in the project to load the class within the file. This will allow mismatched names within the file to be loaded and mapped according to the declaration instead of the filename. The class is then checked against the parsed classes from the PHP engine so that namespaces are available and mapped to the file they were declared in. An update was also made to the search algorithm used to find a file that was not already loaded by collapsing the array to have only valid matches to increase performance on a cache miss. Logging within the auto_loader has been moved to a function.
Multiple files were modified to allow the include statement. When the class has the `if(class_exists())` statement, the auto_loader is called to check for the class. This caused an infinite loop scenario so all wrappers have been removed. The auto_loader will now break the loop by directly modifying the internal classes array instead of trying to restart with the 'reload_classes' method.

- APCu is used to cache classes so any loading of the classes is done only once. To clear the APCu cache, restart php-fpm or call the auto_loader::clear_cache() function.
- Cache file is used when APCu is not available. To clear the cache remove it from the tmp folder or call the auto_loader::clear_cache() function.
- All classes must no longer have a class_exists wrapper to benefit from the performance boost.
- Classes should not be directly included when the auto_loader is used.

* remove include statement of class file

* Update destinations.php
2025-03-12 13:55:47 -06:00

152 lines
3.6 KiB
PHP

<?php
/**
* events class provides an event system
*/
class events {
/**
* @var obj $db Database connnection object
* @var array $plugins Store available plugin classes
* @var array $methods store methods found on each plugin
* @var array $headers headers provide information about the events
* @var array $required array of items that are required
* @var string $content optional additional data about the event
*/
public $db;
private $plugins = array();
private $methods = array();
public $headers = array();
public $required = array();
private $content;
/**
* Called when the object is created
* Creates the database connection object
*/
public function __construct() {
//create the database connection
//includes files
$database = database::new();
$database->connect();
$this->db = $database->db;
return $this->db = $database->db;
//load the plugins
$this->load_plugins();
//add values to the required array
$this->required['headers'][] = "content-type";
$this->required['headers'][] = "date";
$this->required['headers'][] = "host";
$this->required['headers'][] = "status";
$this->required['headers'][] = "app_name";
$this->required['headers'][] = "app_uuid";
$this->required['headers'][] = "domain_uuid";
$this->required['headers'][] = "user_uuid";
}
/**
* This function will load all available plugins into the memory
* Rules:
* plugins are stored in ./plugins
* plugin class is named plugin_<name>
* php file is named <name>.php
*/
private function load_plugins() {
$base = realpath(dirname(__FILE__)) . "/plugins";
$this->plugins = glob($base . "/*.php");
foreach($this->plugins as $plugin) {
//include the plugin php file and define the class name
include_once $plugin;
$plugin_name = basename($plugin, ".php");
$class_name = "plugin_".$plugin_name;
//create the plugin object so that it can be stored and called later
$obj = new $class_name();
$this->plugins[$plugin_name] = $obj;
//store all methods found in the plugin
foreach (get_class_methods($obj) as $method ) {
$this->methods[$method] = $plugin_name;
}
}
}
/**
* Run the plugin method
* @param strint $method
* @param string $args
*
*/
public function __call($method, $args) {
if (! key_exists($method, $this->methods)) {
throw new Exception ("Call to undefined method: " . $method);
}
array_unshift($args, $this);
try {
$obj = call_user_func_array(array($this->plugins[$this->methods[$method]], $method), $args);
}
catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
return $obj;
}
/**
* Set a new event header
* @param string $category
* @param string $name
* @param string $value
*/
public function set_header($category, $name, $value) {
$this->headers[$category][$name] = $value;
}
/**
* check for required headers
* @param string $category
* @return boolean $value
*/
public function check_required($category) {
foreach ($this->required['headers'] as $header) {
if ($category == $header) {
return true;
}
}
return false;
}
/**
* Send the event
*/
public function send() {
//check for required headers are present return false if any are missing
foreach ($this->headers as $header) {
if (!$this->check_required($header)) {
return false;
}
}
//$this->content;
}
/**
* Serialize the event headers
* @param string $type values: array, json
*/
public function serialize($type) {
$array = $this->headers;
if ($type == "array") {
return $array;
} elseif ($type == "json") {
return json_encode($array);
}
}
}
?>