diff --git a/public_html/deployment/classes/plugins.class.php b/public_html/deployment/classes/plugins.class.php index 05e43b36ba9137886af3d9d70d1d7f9465631104..43769dc579df9f9e739ba4c7d8c7feb3ebf51380 100644 --- a/public_html/deployment/classes/plugins.class.php +++ b/public_html/deployment/classes/plugins.class.php @@ -17,90 +17,94 @@ * * * @author axel + * + * 2024-08-26 v1.1 Axel Hahn php8 only; added variable types */ -class ciplugins { - +class ciplugins +{ + /** * start path of all plugin types (as subdirs) * @var string */ - protected $_sPlugindir=false; + protected string $_sPlugindir = ''; /** * path of the currently set plugin * @var string */ - protected $_sSelfdir=false; + protected string $_sSelfdir = ''; /** * url of set plugin * @var string */ - protected $_sSelfurl=false; + protected string $_sSelfurl = ''; /** * current plugin type - can be set via setType or setPlugin * @var string */ - protected $_sType=false; + protected string $_sType = ''; /** * current plugin name - can be set via setPlugin * @var string */ - protected $_sPluginname=false; + protected string $_sPluginname = ''; /** * plugin language * @var string */ - protected $_sLang = "en-en"; + protected string $_sLang = "en-en"; /** * plugin language texts (lang*.json) * @var array */ - protected $_aLang = []; + protected array $_aLang = []; /** * plugin configuration data (config.json) * @var array */ - protected $_aConfig = []; + protected array $_aConfig = []; /** * global plugins config * see config/config_custom.php - key plugins * @var array */ - protected $_aGlobals = []; + protected array $_aGlobals = []; // --------------------------------------------------------------- // CONSTRUCTOR // --------------------------------------------------------------- /** + * Constructor * initialize plugins * * @param array $aGlobals global settings for plugins * @return boolean */ - public function __construct($aGlobals=[]) { - - $this->_sPlugindir=dirname(__DIR__).'/plugins'; + public function __construct(array $aGlobals = []) + { + $this->_sPlugindir = dirname(__DIR__) . '/plugins'; $this->setGlobalCustoms($aGlobals); - } /** - * global configs + * Global configs * see config/config_custom.php - key plugins * * @param array $aGlobals global settings for plugins * @return boolean */ - public function setGlobalCustoms($aGlobals){ - $this->_aGlobals=$aGlobals; + public function setGlobalCustoms(array $aGlobals): bool + { + $this->_aGlobals = $aGlobals; return true; } // --------------------------------------------------------------- @@ -108,61 +112,65 @@ class ciplugins { // --------------------------------------------------------------- /** - * get an array of available plugin types read from filesystem + * Get an array of available plugin types read from filesystem * @return array */ - public function getPluginTypes(){ - $aReturn=[]; - foreach(glob($this->_sPlugindir.'/*', GLOB_ONLYDIR) as $sMydir){ - $aReturn[]=basename($sMydir); + public function getPluginTypes(): array + { + $aReturn = []; + foreach (glob($this->_sPlugindir . '/*', GLOB_ONLYDIR) as $sMydir) { + $aReturn[] = basename($sMydir); } return $aReturn; } /** - * get an array of available plugins read from filesystem + * Get an array of available plugins read from filesystem * * @param string $sType set a new type of plugin; default: use current type * @return array */ - public function getPlugins($sType=false){ - $aReturn=[]; - if($sType){ - if (!$this->setType($sType)){ + public function getPlugins(string $sType = ''): array + { + $aReturn = []; + if ($sType) { + if (!$this->setType($sType)) { return $aReturn; } } - foreach(glob($this->_sPlugindir.'/'.$this->_sType.'/*', GLOB_ONLYDIR) as $sMydir){ - $aReturn[]=basename($sMydir); + foreach (glob($this->_sPlugindir . '/' . $this->_sType . '/*', GLOB_ONLYDIR) as $sMydir) { + $aReturn[] = basename($sMydir); } return $aReturn; } /** - * get an array of enabled plugins + * Get an array of enabled plugins * config/config_custom.php - key "plugins" -> [type] -> [plugin] -> enabled * and it must be physically available * * @param string $sType set a new type of plugin; default: use current type * @return array */ - public function getEnabledPlugins($sType=false){ + public function getEnabledPlugins(string $sType = ''): array + { $aReturn = []; - if($sType){ - if (!$this->setType($sType)){ + if ($sType) { + if (!$this->setType($sType)) { return $aReturn; } } - if (isset($this->_aGlobals[$this->_sType]) + if ( + isset($this->_aGlobals[$this->_sType]) && is_array($this->_aGlobals[$this->_sType]) - ){ - foreach($this->_aGlobals[$this->_sType] as $sPluginName=>$aData){ + ) { + foreach ($this->_aGlobals[$this->_sType] as $sPluginName => $aData) { if ( - isset($aData['enabled']) + isset($aData['enabled']) && $aData['enabled'] - && is_dir($this->_sPlugindir.'/'.$this->_sType.'/'.$sPluginName) - ){ - $aReturn[]=$sPluginName; + && is_dir($this->_sPlugindir . '/' . $this->_sType . '/' . $sPluginName) + ) { + $aReturn[] = $sPluginName; } } } @@ -180,73 +188,80 @@ class ciplugins { // --------------------------------------------------------------- /** - * set a type for plugins ... what is a name of a subdir in the plugins directory - * @param {string} $sType Name of a plugin type, e.g. build|rollout + * Set a type for plugins ... what is a name of a subdir in the plugins directory + * @param string $sType Name of a plugin type, e.g. build|rollout + * @return bool */ - public function setType($sType){ - $this->_sType=false; - if(!$sType || !is_dir($this->_sPlugindir.'/'.$sType)){ + public function setType(string $sType): bool + { + $this->_sType = ''; + if (!$sType || !is_dir($this->_sPlugindir . '/' . $sType)) { return false; } - return $this->_sType=$sType; + $this->_sType = $sType; + return true; } /** - * reset vars before setting a new plugin; + * Reset vars before setting a new plugin; * called in testPlugin() * @return boolean */ - protected function _resetPluginData(){ - $this->_sPluginname=false; - $this->_sSelfdir=false; - $this->_sSelfurl=false; - $this->_aLang=[]; - $this->_aConfig=[]; + protected function _resetPluginData(): bool + { + $this->_sPluginname = false; + $this->_sSelfdir = false; + $this->_sSelfurl = false; + $this->_aLang = []; + $this->_aConfig = []; return true; } /** - * set a plugin without autoload of its php class + * Test if a plugin of given type exists + * * It returns the path of php class for true * or boolean false if it does not exist * * This can be used standalone to embed html code * without loading any php code of the plugin class. * - * @param {string} $sPluginName name of the plugin - * @param {string} $sType optional: set a type + * @param string $sPluginName name of the plugin + * @param string $sType optional: set a type * @return bool|string */ - public function testPlugin($sPluginName,$sType=false){ + public function testPlugin(string $sPluginName, string $sType = ''): bool|string + { $this->_resetPluginData(); - if($sType){ - if (!$this->setType($sType)){ + if ($sType) { + if (!$this->setType($sType)) { return false; } } - $this->_sSelfdir=$this->_sPlugindir.'/'.$this->_sType.'/'.$sPluginName; - $sFile=$this->_sSelfdir.'/plugin.php'; - if(!file_exists($sFile)){ + $this->_sSelfdir = $this->_sPlugindir . '/' . $this->_sType . '/' . $sPluginName; + $sFile = $this->_sSelfdir . '/plugin.php'; + if (!file_exists($sFile)) { // die(' MISS '.$sFile); - $this->_sSelfdir=false; + $this->_sSelfdir = false; return false; } - $this->_sPluginname=$sPluginName; - $this->_sSelfurl='/deployment/plugins/'.$this->_sType.'/'.$sPluginName; + $this->_sPluginname = $sPluginName; + $this->_sSelfurl = '/deployment/plugins/' . $this->_sType . '/' . $sPluginName; return $sFile; } /** - * set a plugin with autoload of its php class + * Set a plugin with autoload of its php class * It returns a boolean * - * @param {string} $sPluginName name of the plugin - * @param {string} $sType optional: set a type + * @param string $sPluginName name of the plugin + * @param string $sType optional: set a type * @return bool */ - public function setPlugin($sPluginName,$sType=false){ - $sFile=$this->testPlugin($sPluginName,$sType); - if(!$sFile){ + public function setPlugin(string $sPluginName, string $sType = ''): bool + { + $sFile = $this->testPlugin($sPluginName, $sType); + if (!$sFile) { return false; } include_once $sFile; @@ -256,25 +271,28 @@ class ciplugins { // getter for plugin // --------------------------------------------------------------- /** - * get config entry + * Get config entry of a plugin * @param string $sKey name of config value + * @return mixed */ - public function getConfigitem($sKey){ - return isset($this->_aConfig[$sKey]) ? $this->_aConfig[$sKey] : false; - } + public function getConfigitem(string $sKey): mixed + { + return $this->_aConfig[$sKey] ?? false; + } /** * get plugin config from its config.json * works with - * - shellcmd plugin + * - shellcmd plugins * @return array */ - public function getPluginConfig(){ - if(count($this->_aConfig)){ + public function getPluginConfig(): array + { + if (count($this->_aConfig)) { return $this->_aConfig; } - $this->_aConfig=(file_exists($this->_sSelfdir.'/config.json')) - ? json_decode(file_get_contents($this->_sSelfdir.'/config.json'), 1) - : ["error" => "config.json not found in ".$this->_sSelfdir] + $this->_aConfig = (file_exists($this->_sSelfdir . '/config.json')) + ? json_decode(file_get_contents($this->_sSelfdir . '/config.json'), 1) + : ["error" => "config.json not found in " . $this->_sSelfdir] ; return $this->_aConfig; } @@ -283,18 +301,12 @@ class ciplugins { // --------------------------------------------------------------- /** - * get a location of a plugin file with full path - * @param {bool} $bAutoload flag: autoload needed plugin file + * Get the classname of a plugin (it is generated by type and plugin name) * @return string */ - public function getPluginClassname(){ - return $this->_sType.'_'.$this->_sPluginname; - } - - public function initPlugin__unused(){ - $sClassname=$this->_sType.'_'.$this->_sPlugindir; - $TmpRolloutPlugin = new $sClassname([]); - + public function getPluginClassname(): string + { + return $this->_sType . '_' . $this->_sPluginname; } }