File phpboost/extension-provider/ExtensionPointProvider.class.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:
<?php
/**
* This Class allow you to call methods on a ExtensionPointProvider extended class
* that you're not sure of the method's availality. It also provides a set of
* generic methods that you could use to integrate your module with others, or
* allow your module to share services.
* @package PHPBoost
* @subpackage Extension-provider
* @copyright © 2005-2019 PHPBoost
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
* @author Loic ROUCHON <horn@phpboost.com>
* @version PHPBoost 5.2 - last update: 2014 12 22
* @since PHPBoost 2.0 - 2008 01 15
*/
abstract class ExtensionPointProvider
{
/**
* @var string the module identifier
*/
private $id;
/**
* @var string[] list of the extensions points provided
*/
private $extensions_points = array();
/**
* ExtensionPointProvider constructor
* @param string $extension_provider_id the provider id. It's the name of the folder in witch
* the extension provider is
*/
public function __construct($extension_provider_id = '')
{
$this->id = $extension_provider_id;
$this->extensions_points = $this->get_provider_extensions_points($this);
}
/**
* @return string Return the id of the EtensionPoint
*/
public function get_id()
{
return $this->id;
}
/**
* Check the existance of the extension point and if exists call it.
* @param string $extension_point the name of the method you want to call
* @param mixed $args the args you want to pass to the $extension_point method
* @return mixed the $extension_point returns
* @throws ExtensionPointNotFoundException
*/
public function get_extension_point($extension_point, $args = null)
{
if ($this->has_extension_point($extension_point))
{
return $this->$extension_point($args);
}
throw new ExtensionPointNotFoundException($extension_point);
}
/**
* Check the availability of the extension_point (hook)
* @param string $extension_point the name of the method you want to check the availability
* @return bool true if the extension point exists, false otherwise
*/
public function has_extension_point($extension_point)
{
return in_array($extension_point, $this->extensions_points);
}
/**
* Check the availability of the extensions points (hook)
* @param string[] $extensions_points the names of the methods you want to check the availability
* @return bool true if all extensions points exist, false otherwise
*/
public function has_extensions_points(array $extensions_points)
{
foreach($extensions_points as $extension_point)
{
if (!$this->has_extension_point($extension_point))
{
return false;
}
}
return true;
}
private function get_provider_extensions_points($provider)
{
$module_methods = get_class_methods($provider);
$generics_methods = get_class_methods('ExtensionPointProvider');
return array_values(array_diff($module_methods, $generics_methods));
}
}
?>