On the template repository, you can find an example in the file ModuleTemplate/Setup/PbxExtensionSetup.php
You can use it for your project with some modifications.
After initializing the PbxExtensionSetup class you will have a lot of useful variables from a parent class:
/*** Module unique identify from the module.json* @varstring*/protectedstring $moduleUniqueID;/** * Module version from the module.json * @varstring */protected $version;/** * Minimal required version PBX from the module.json * @varstring */protected $min_pbx_version;/** * Module developer name from the module.json * @varstring */protected $developer;/** * Module developer's email from module.json * @varstring */protected $support_email;/** * PBX core general database * @var\Phalcon\Db\Adapter\Pdo\Sqlite */protected $db;/** * Folder with module files * @varstring */protectedstring $moduleDir;/** * Phalcon config service * @var\Phalcon\Config */protected $config;/** * Error and verbose messages * @vararray */protectedarray $messages;
Usually, you shouldn't override the PbxExtensionSetup class constructor, but if you want don't forget to leave parent class initialization.
For example, you want to add some extra variable moduleExtension into the PbxExtensionSetup class:
classPbxExtensionSetupextendsPbxExtensionSetupBase{privatestring $moduleExtension;/** * PbxExtensionSetup constructor. * * @paramstring $moduleUniqueID - the unique module identifier */publicfunction__construct(string $moduleUniqueID) {parent::__construct($moduleUniqueID);//... some extra code ... //$this->moduleExtension ='000XXXX'; }//... some extra code ... //}
As you can see the main setup function has name InstallModule. It is already written on PbxExtensionSetupBase.
After unzipping module files the PBXCoreRest interface calls the main module installation function. It calls some private functions and sets error messages into the message variable. If something goes wrong this method returns false and the user receives information from the message variable.
This function compares the current PBX version with the minimum required version specified by the module. If the current version is lower than the minimum required version, it adds a message to the messages array and returns false. Otherwise, it returns true, indicating that the PBX version is compatible.
/** * Checks if the current PBX version is compatible with the minimum required version. * * @returnbool Returns `true` if PBX version is compatible; otherwise, `false`. */publicfunctioncheckCompatibility():bool{// Get the current PBX version from the settings. $currentVersionPBX =PbxSettings::getValueByKey('PBXVersion');// Remove any '-dev' suffix from the version. $currentVersionPBX =str_replace('-dev','', $currentVersionPBX);if (version_compare($currentVersionPBX,$this->min_pbx_version)<0) {// The current PBX version is lower than the required version.// Add a message indicating the compatibility issue.$this->messages[] ="Module depends minimum PBX ver $this->min_pbx_version"// Return false to indicate incompatibility.returnfalse; }// The current PBX version is compatible.returntrue;}
activateLicense
This function we use only for commercial modules. It can add some trials to your license keys within the installation process.
/*** Executes license activation only for commercial modules** @returnbool result of license activation*/publicfunctionactivateLicense():bool{ $lic =PbxSettings::getValueByKey('PBXLicense');if (empty($lic)) {$this->messges[] ='License key not found...';returnfalse; }// Add trial license for module with an id equal 3$this->license->addtrial('3');returntrue;}
You can skip this procedure if you make a non-commercial module or read more information about licensing here.
installFiles
Thisfunctionwe use for making copies of some files, folders and create symlinks for module's files. If your module doesn't have anything special you haven't overriding this procedure.
installDB
We use this function to manage some changes with a database structure and data stored in the module database or system database.
Every module has its own sqlite3 database stored on the DB folder within the module folder.
We do not recommend you to create and put any sqlite3 database files into your distributive. The best way is to describe all models and relationships between tables according to Phalcon models annotation instructions. The internal procedure createSettingsTableByModelsAnnotations creates a sqlite3 database with all tables or modifies them if you start the module upgrading process.
The next model file class describes the m_ModuleTemplate table with primary id and string textFieldcolumns:
<?phpnamespaceModules\ModuleTemplate\Models;useMikoPBX\Common\Models\Providers;useMikoPBX\Modules\Models\ModulesModelsBase;usePhalcon\Mvc\Model\Relation;classModuleTemplateextendsModulesModelsBase{/** * @Primary * @Identity * @Column(type="integer", nullable=false) */public $id;/** * Text field example * * @Column(type="string", nullable=true) */public $textField;publicfunctioninitialize():void {$this->setSource('m_ModuleTemplate'); parent::initialize(); }}
More information about models file
After calling the createSettingsTableByModelsAnnotations you can manipulate module settings by the model class.
To register the module within the PBX system you have to call the registerNewModule function. It adds a record to the PbxExtensionModules table according to information provided in the module.json file.
This function changes files and folder ownerships and applies special executable rules for binary files and PHP-AGI scripts. You should keep the module folder structure according to the ModuleTemplate example.
Uninstall a module procedure
When you delete any module the MikoPBX system calls the next procedure:
// Uninstall module with keep settings and backup db$moduleClass ="\\Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup";$setup =new$moduleClass($moduleUniqueID);$setup->uninstallModule($keepSettings);
As you can see the main uninstall function has name unInstallModule. It has already written in PbxExtensionSetupBase class.
After user pushes delete or upgrade button the PBXCoreRest interface calls the unInstallModule function. It calls some private functions and sets error messages on the message variable. If something goes wrong – this method will return false and the user will be announced with information from the message variable.
You can override this function to make some manipulations with data. For example, remove links to the module from system tables.
By default, this method unregisters the module from the PbxExtensionModules table by the unregisterModule function.
The method makes a copy of a module database if the keepSettings wasset to true. Then it deletes all installed files, folders, symlinks.
If your module has some executable binaries you should kill all processes before deleting them. Also, if your module produced any temporary or log files, you should delete them as well.