Skip to content
Snippets Groups Projects
Commit 644761a2 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

plugins.class: php8 only; added variable types

parent d8580ffc
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
......@@ -17,89 +17,93 @@
*
*
* @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=[]) {
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){
public function setGlobalCustoms(array $aGlobals): bool
{
$this->_aGlobals = $aGlobals;
return true;
}
......@@ -108,10 +112,11 @@ 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(){
public function getPluginTypes(): array
{
$aReturn = [];
foreach (glob($this->_sPlugindir . '/*', GLOB_ONLYDIR) as $sMydir) {
$aReturn[] = basename($sMydir);
......@@ -120,12 +125,13 @@ class ciplugins {
}
/**
* 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){
public function getPlugins(string $sType = ''): array
{
$aReturn = [];
if ($sType) {
if (!$this->setType($sType)) {
......@@ -139,21 +145,23 @@ class ciplugins {
}
/**
* 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)) {
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) {
......@@ -180,23 +188,27 @@ 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;
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(){
protected function _resetPluginData(): bool
{
$this->_sPluginname = false;
$this->_sSelfdir = false;
$this->_sSelfurl = false;
......@@ -206,18 +218,20 @@ class ciplugins {
}
/**
* 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)) {
......@@ -237,14 +251,15 @@ class ciplugins {
}
/**
* 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){
public function setPlugin(string $sPluginName, string $sType = ''): bool
{
$sFile = $this->testPlugin($sPluginName, $sType);
if (!$sFile) {
return false;
......@@ -256,19 +271,22 @@ 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(){
public function getPluginConfig(): array
{
if (count($this->_aConfig)) {
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(){
public function getPluginClassname(): string
{
return $this->_sType . '_' . $this->_sPluginname;
}
public function initPlugin__unused(){
$sClassname=$this->_sType.'_'.$this->_sPlugindir;
$TmpRolloutPlugin = new $sClassname([]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment