Private GIT

Skip to content
Snippets Groups Projects
Commit 68ae2a76 authored by Lionel Assepo's avatar Lionel Assepo
Browse files

* Update modules develoment documentation

     * Add Module generator to easy bootstrap a module development
parent 5cca2f33
No related branches found
No related tags found
No related merge requests found
......@@ -33,14 +33,16 @@
* For more information : contact@centreon.com
*
*/
namespace Centreon\Commands;
namespace Centreon\Commands\Module;
use \Centreon\Internal\Module\Informations;
/**
* Description of ModuleCommand
* Description of ManageCommand
*
* @author lionel
*/
class ModuleCommand extends \Centreon\Internal\Command\AbstractCommand
class ManageCommand extends \Centreon\Internal\Command\AbstractCommand
{
/**
*
......@@ -84,7 +86,7 @@ class ModuleCommand extends \Centreon\Internal\Command\AbstractCommand
*/
public function installAction($moduleName)
{
$moduleInstaller = $this->getModuleInstaller($moduleName);
$moduleInstaller = Informations::getModuleInstaller($moduleName);
$moduleInstaller->install();
}
......@@ -94,11 +96,11 @@ class ModuleCommand extends \Centreon\Internal\Command\AbstractCommand
*/
public function reinstallAction($moduleName)
{
$moduleId = \Centreon\Internal\Module\Informations::getModuleIdByName($moduleName);
$moduleInstaller = $this->getModuleInstaller($moduleName, $moduleId);
$moduleId = Informations::getModuleIdByName($moduleName);
$moduleInstaller = Informations::getModuleInstaller($moduleName, $moduleId);
$moduleInstaller->remove();
unset($moduleInstaller);
$moduleInstaller = $this->getModuleInstaller($moduleName);
$moduleInstaller = Informations::getModuleInstaller($moduleName);
$moduleInstaller->install();
}
......@@ -117,42 +119,9 @@ class ModuleCommand extends \Centreon\Internal\Command\AbstractCommand
*/
public function removeAction($moduleName)
{
$moduleInstaller = $this->getModuleInstaller($moduleName);
$moduleInstaller = Informations::getModuleInstaller($moduleName);
$moduleInstaller->remove();
}
/**
*
* @param type $moduleName
* @return \Centreon\Commands\classCall
* @throws \Exception
*/
private function getModuleInstaller($moduleName, $moduleId = null)
{
$config = $this->di->get('config');
$centreonPath = rtrim($config->get('global', 'centreon_path'), '/');
$commonName = str_replace(' ', '', ucwords(str_replace('-', ' ', $moduleName)));
$moduleDirectory = $centreonPath
. '/modules/'
. $commonName
. 'Module/';
if (!file_exists(realpath($moduleDirectory . 'install/config.json'))) {
throw new \Exception("The module is not valid because of a missing configuration file");
}
$moduleInfo = json_decode(file_get_contents($moduleDirectory . 'install/config.json'), true);
// Launched Install
$classCall = '\\'.$commonName.'\\Install\\Installer';
if (isset($moduleId)) {
$moduleInfo['id'] = $moduleId;
}
$moduleInstaller = new $classCall($moduleDirectory, $moduleInfo);
return $moduleInstaller;
}
}
<?php
/*
* Copyright 2005-2014 MERETHIS
* Centreon is developped by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation ; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <http://www.gnu.org/licenses>.
*
* Linking this program statically or dynamically with other modules is making a
* combined work based on this program. Thus, the terms and conditions of the GNU
* General Public License cover the whole combination.
*
* As a special exception, the copyright holders of this program give MERETHIS
* permission to link this program with independent modules to produce an executable,
* regardless of the license terms of these independent modules, and to copy and
* distribute the resulting executable under terms of MERETHIS choice, provided that
* MERETHIS also meet, for each linked independent module, the terms and conditions
* of the license of that module. An independent module is a module which is not
* derived from this program. If you modify this program, you may extend this
* exception to your version of the program, but you are not obliged to do so. If you
* do not wish to do so, delete this exception statement from your version.
*
* For more information : contact@centreon.com
*
*/
namespace Centreon\Commands\Module;
use \Centreon\Internal\Utils\String\CamelCaseTransformation;
use \Centreon\Internal\Module\Generator;
use \Centreon\Internal\Module\Informations;
/**
* Description of Generate
*
* @author lionel
*/
class ToolsCommand extends \Centreon\Internal\Command\AbstractCommand
{
public function generateAction()
{
// Set Module Name
echo _("Type the module name here => ");
$userAnswer = trim(fgets(STDIN));
$moduleCanonicalName = CamelCaseTransformation::CustomToCamelCase($userAnswer, " ");
$moduleShortname = strtolower(CamelCaseTransformation::CamelCaseToCustom($moduleCanonicalName, "-"));
$moduleGenerator = new Generator($moduleCanonicalName);
$moduleGenerator->setModuleShortName($moduleShortname);
$moduleGenerator->setModuleDisplayName($userAnswer);
// Set Module ShortName
echo _(
"Type the module shortname here (seperate by -) ["
. $moduleShortname
."] => ");
unset($userAnswer);
$userAnswer = trim(fgets(STDIN));
if (!empty($userAnswer)) {
$moduleShortname = $userAnswer;
}
// Type User Name
echo _("Type your name here => ");
unset($userAnswer);
$moduleAuthor = trim(fgets(STDIN));
$moduleGenerator->setModuleAuthor($moduleAuthor);
// Ask For generating Directory Structure
echo _("Generating module full structure... ");
$moduleGenerator->generateFileStructure();
$moduleGenerator->generateConfigFile();
$moduleGenerator->createSampleInstaller();
echo _("Done\n");
// Ask For sample Controller/View
echo _("Generate sample controller/view (yes/no)? [yes] ");
$generateController = strtolower(trim(fgets(STDIN)));
if (empty($generateController) || $generateController == "yes" || $generateController == "y") {
$moduleGenerator->createSampleController();
$moduleGenerator->createSampleView();
}
// Ask to install the module
echo _("Install the module(yes/no)? [no] ");
$installModule = strtolower(trim(fgets(STDIN)));
if (!empty($installModule) && ($installModule == "yes" || $installModule == "y")) {
$moduleInstaller = Informations::getModuleInstaller($moduleShortname);
$moduleInstaller->install();
}
}
}
<?php
/*
* Copyright 2005-2014 MERETHIS
* Centreon is developped by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation ; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <http://www.gnu.org/licenses>.
*
* Linking this program statically or dynamically with other modules is making a
* combined work based on this program. Thus, the terms and conditions of the GNU
* General Public License cover the whole combination.
*
* As a special exception, the copyright holders of this program give MERETHIS
* permission to link this program with independent modules to produce an executable,
* regardless of the license terms of these independent modules, and to copy and
* distribute the resulting executable under terms of MERETHIS choice, provided that
* MERETHIS also meet, for each linked independent module, the terms and conditions
* of the license of that module. An independent module is a module which is not
* derived from this program. If you modify this program, you may extend this
* exception to your version of the program, but you are not obliged to do so. If you
* do not wish to do so, delete this exception statement from your version.
*
* For more information : contact@centreon.com
*
*/
namespace Centreon\Internal\Module;
use \Centreon\Internal\Di;
use \Centreon\Internal\Informations;
/**
* Description of Generate
*
* @author lionel
*/
class Generator
{
/**
*
* @var type
*/
private $moduleDisplayName;
/**
*
* @var type
*/
private $moduleCanonicalName;
/**
*
* @var type
*/
private $moduleShortName;
/**
*
* @var type
*/
private $moduleAuthor;
/**
*
* @var type
*/
private $moduleFolderPath;
/**
*
* @var type
*/
private $licensePath;
/**
*
* @var type
*/
private $moduleFolderStructure;
/**
*
* @param type $moduleCanonicalName
*/
public function __construct($moduleCanonicalName)
{
$this->moduleCanonicalName = $moduleCanonicalName;
$config = Di::getDefault()->get('config');
$centreonPath = rtrim($config->get('global', 'centreon_path'), '/');
$this->licensePath = $centreonPath . '/infos/header.txt';
$this->moduleFolderPath = $centreonPath . '/modules/' . $moduleCanonicalName . 'Module';
$this->moduleFolderStructure= array(
'api' => $this->moduleFolderPath . '/api',
'apiInternal' => $this->moduleFolderPath . '/api/internal',
'apiRest' => $this->moduleFolderPath . '/api/rest',
'apiSoap' => $this->moduleFolderPath . '/api/soap',
'commands' => $this->moduleFolderPath . '/commands',
'config' => $this->moduleFolderPath . '/config',
'controllers' => $this->moduleFolderPath . '/controllers',
'customs' => $this->moduleFolderPath . '/customs',
'events' => $this->moduleFolderPath . '/events',
'install' => $this->moduleFolderPath . '/install',
'installDb' => $this->moduleFolderPath . '/install/db',
'installDbCentreon' => $this->moduleFolderPath . '/install/db/centreon',
'forms' => $this->moduleFolderPath . '/forms',
'internal' => $this->moduleFolderPath . '/internal',
'models' => $this->moduleFolderPath . '/models',
'repositories' => $this->moduleFolderPath . '/repositories',
'tests' => $this->moduleFolderPath . '/tests',
'views' => $this->moduleFolderPath . '/views'
);
}
/**
*
* @param type $shortname
*/
public function setModuleShortName($shortname)
{
$this->moduleShortName = $shortname;
}
/**
*
* @param type $displayName
*/
public function setModuleDisplayName($displayName)
{
$this->moduleDisplayName = $displayName;
}
/**
*
* @param type $author
*/
public function setModuleAuthor($author)
{
$this->moduleAuthor = $author;
}
/**
*
*/
public function generateConfigFile()
{
$moduleConfig = array(
'name' => $this->moduleDisplayName,
'shortname' => $this->moduleShortName,
'version' => '1.0.0',
'author' => array($this->moduleAuthor),
'isuninstallable' => 1,
'isdisableable' => 1,
'url' => "",
'description' => $this->moduleDisplayName,
'core version' => Informations::getCentreonVersion(),
'dependencies' => array(
array(
'name' => 'centreon-administration',
'version' => Informations::getCentreonVersion()
)
),
'optionnal dependencies' => array(),
'php module dependencies' => array(),
'program dependencies' => array()
);
file_put_contents(
$this->moduleFolderStructure['install'] . '/config.json',
json_encode($moduleConfig, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK)
);
}
/**
*
*/
public function generateFileStructure()
{
mkdir($this->moduleFolderPath, 0777, true);
foreach ($this->moduleFolderStructure as $subFolder) {
mkdir($subFolder, 0777, true);
}
}
/**
*
* @param type $withLicense
*/
public function createSampleInstaller($withLicense = true)
{
$installerClass = "<?php\n\n";
if ($withLicense) {
$installerClass .= file_get_contents($this->licensePath);
}
$installerClass .= "namespace $this->moduleCanonicalName\Install;\n\n";
$installerClass .= "class Installer extends \Centreon\Internal\Module\Installer\n";
$installerClass .= "{\n";
$installerClass .= $this->indent() . 'public function __construct($moduleDirectory, $moduleInfo)' . "\n";
$installerClass .= $this->indent() . "{\n";
$installerClass .= $this->indent(2) . 'parent::__construct($moduleDirectory, $moduleInfo);';
$installerClass .= $this->indent() . "\n}\n";
$installerClass .= $this->indent() . 'public function customPreInstall()' . "\n";
$installerClass .= $this->indent() . "{\n" . $this->indent(2) . "\n}\n";
$installerClass .= $this->indent() . 'public function customInstall()' . "\n";
$installerClass .= $this->indent() . "{\n" . $this->indent(2) . "\n}\n";
$installerClass .= $this->indent() . 'public function customRemove()' . "\n";
$installerClass .= $this->indent() . "{\n" . $this->indent(2) . "\n}\n";
$installerClass .= "}\n\n";
file_put_contents($this->moduleFolderStructure['install'] . '/Installer.php', $installerClass);
}
/**
*
* @param type $withLicense
*/
public function createSampleController($withLicense = true)
{
$controllerClass = "<?php\n\n";
if ($withLicense) {
$controllerClass .= file_get_contents($this->licensePath);
}
$controllerClass .= "namespace $this->moduleCanonicalName\Controllers;\n\n";
$controllerClass .= "class SampleController extends \Centreon\Internal\Controller\n";
$controllerClass .= "{\n";
$controllerClass .= $this->indent() . 'public static $moduleName = ' . "'$this->moduleCanonicalName';\n\n";
$controllerClass .= $this->indent() . "/**\n";
$controllerClass .= $this->indent() . " * @method get\n";
$controllerClass .= $this->indent() . " * @route /$this->moduleShortName/sample\n";
$controllerClass .= $this->indent() . " */\n";
$controllerClass .= $this->indent() . "public function sampleAction()\n";
$controllerClass .= $this->indent() . "{\n";
$controllerClass .= $this->indent(2) . '$this->assignVarToTpl(\'centreonVersion\', \'Centreon 3.0\');' . "\n";
$controllerClass .= $this->indent(2) . '$this->assignVarToTpl(\'centreonVersion\', \'Centreon 3.0\');' . "\n";
$controllerClass .= $this->indent(2) . '$this->display("sample.tpl");' . "\n";
$controllerClass .= $this->indent() . "}\n}\n";
$controllerClass .= "\n";
file_put_contents($this->moduleFolderStructure['controllers'] . '/SampleController.php', $controllerClass);
}
/**
*
*/
public function createSampleView()
{
$viewContent = '{extends file="file:[Core]viewLayout.tpl"}' . "\n\n";
$viewContent .= '{block name="title"}Sample{/block}' . "\n\n";
$viewContent .= '{block name="content"}Welcome to my {$centreonVersion} module{/block}' . "\n\n";
file_put_contents($this->moduleFolderStructure['views'] . '/sample.tpl', $viewContent);
}
/**
*
* @param type $occurence
* @return string
*/
private function indent($occurence = 1)
{
$finalIndent = "";
for ($i=0; $i<$occurence; $i++) {
$finalIndent .= " ";
}
return $finalIndent;
}
}
......@@ -207,7 +207,7 @@ class Informations
{
$menus = null;
$db = \Centreon\Internal\Di::getDefault()->get('db_centreon');
$db = Di::getDefault()->get('db_centreon');
if (is_null($menus)) {
$sql = "SELECT menu_id, short_name FROM cfg_menus";
$stmt = $db->prepare($sql);
......@@ -267,4 +267,39 @@ class Informations
}
}
}
/**
*
* @param type $moduleName
* @return \Centreon\Commands\classCall
* @throws \Exception
*/
public static function getModuleInstaller($moduleName, $moduleId = null)
{
$config = Di::getDefault()->get('config');
$centreonPath = rtrim($config->get('global', 'centreon_path'), '/');
$commonName = str_replace(' ', '', ucwords(str_replace('-', ' ', $moduleName)));
$moduleDirectory = $centreonPath
. '/modules/'
. $commonName
. 'Module/';
if (!file_exists(realpath($moduleDirectory . 'install/config.json'))) {
throw new \Exception("The module is not valid because of a missing configuration file");
}
$moduleInfo = json_decode(file_get_contents($moduleDirectory . 'install/config.json'), true);
// Launched Install
$classCall = '\\'.$commonName.'\\Install\\Installer';
if (isset($moduleId)) {
$moduleInfo['id'] = $moduleId;
}
$moduleInstaller = new $classCall($moduleDirectory, $moduleInfo);
return $moduleInstaller;
}
}
......@@ -3,7 +3,8 @@ Comment écrire d'un module pour Centreon 3
**Centreon** permet la modification et l'ajout de fonctionnalité par un système de module.
Pour écrite un module, le module doit suivre l'architecture suivante et respecter les règles de nommages.
Pour écrire un module, le module doit suivre l'architecture suivante et respecter les règles de nommages.
Centreon 3.0 embarque u générateur de modules en ligne de commande qui créé un module simple prêt à être utilisé.
Règle de nommage
----------------
......@@ -22,7 +23,7 @@ Le répertoire du module doit contenir les répertoires suivants.
- **/config** : contains the module specific configuration files
- **/controllers** : contains the modules controllers (mandatory)
- **/customs** : contains modules' external libraireis, custom functions
- **/internals** : contains modules' specific classes
- **/internal** : contains modules' specific classes
- **/models** : contains modules' models
- **/respositories** : contains modules' repositories (business logic sql request)
- **/views** : contains modules' HTML templates
......@@ -34,6 +35,12 @@ Le répertoire du module doit contenir les répertoires suivants.
- **/forms** : les définitions des formulaires pour ce modules
- menu.json : les liens dans le menu
Générateur de modules en ligne de commande
------------------------------------------
A l'aide de la commande ./centreonConsole core:module:tools:generate, vous pouvez gérer un embryon de module fonctionnel.
Il suffit de suivre les instructions affichées.
Partie installation
-------------------
......@@ -42,7 +49,7 @@ Partie installation
how_to_write_module/installation
Ajouter de page
Ajout de page
---------------
Les pages sont desservies par les contrôleurs.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment