Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
refacto(front): Rework dynamic components + build process (#7255)
Browse files Browse the repository at this point in the history
  • Loading branch information
kduret authored Mar 13, 2019
1 parent a78c60a commit cca6800
Show file tree
Hide file tree
Showing 44 changed files with 5,776 additions and 4,566 deletions.
27 changes: 27 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"exclude": [],
"presets": [
[
"@babel/preset-react"
],
[
"@babel/preset-env",
{
"targets": {
"esmodules": false
}
}
],
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-arrow-functions",
"@babel/plugin-transform-destructuring",
"@babel/plugin-transform-function-name",
"@babel/plugin-transform-parameters",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-classes",
"@babel/plugin-transform-shorthand-properties",
"@babel/plugin-transform-regenerator"
]
}
9,142 changes: 5,034 additions & 4,108 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 24 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
"version": "18.10.0",
"description": "centreon web package",
"scripts": {
"build": "npm run build:clean && npm run build:sass && npm run build:app && npm run build:post",
"build": "npm run build:clean && npm run build:app && npm run build:post",
"build:clean": "rm -rf www/static && rm -rf www/template",
"build:sass": "node-sass -r --output-style compressed ./www/front_src/src -o ./www/front_src/src",
"build:app": "cd ./www/front_src && PUBLIC_URL=\"/_CENTREON_PATH_PLACEHOLDER_/\" webpack --mode production",
"build:post": "mv ./www/static ./www/template",
"watch:sass": "node-sass -r --output-style compressed ./www/front_src/src -o ./www/front_src/src && npm run build:sass && npm run build:sass -- -w"
"build:app": "webpack --mode production",
"build:post": "mv ./www/static ./www/template && chown -R apache:apache ./www/index.html ./www/template"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-arrow-functions": "^7.2.0",
"@babel/plugin-transform-regenerator": "^7.3.4",
"@babel/plugin-transform-shorthand-properties": "^7.2.0",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.0",
"css-loader": "^2.1.0",
"expose-loader": "^0.7.5",
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.9.3",
"webpack": "^4.12.0",
"postcss-loader": "^3.0.0",
"postcss-safe-parser": "^4.0.1",
"resolve-url-loader": "^3.0.1",
"sass-loader": "^7.1.0",
"terser-webpack-plugin": "^1.2.3",
"url-loader": "^1.1.2",
"webpack": "^4.29.5",
"webpack-cli": "^3.0.8"
},
"dependencies": {
"axios": "^0.18.0",
"class-name": "^0.1.3",
"cross-env": "^5.2.0",
"dom-serializer": "^0.1.0",
"install": "^0.12.2",
"loaders.css": "^0.1.2",
"moment-timezone": "^0.5.21",
"numeral": "^2.0.6",
"query-string": "^6.2.0",
"query-string": "^5.1.1",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-full-screen": "^0.2.3",
Expand All @@ -45,7 +56,9 @@
"redux-form": "^7.4.2",
"redux-logger": "^3.0.6",
"redux-saga": "^0.16.2",
"redux-thunk": "^2.3.0"
"redux-thunk": "^2.3.0",
"systemjs": "^3.0.0",
"systemjs-plugin-css": "^0.1.37"
},
"jest": {
"testResultsProcessor": "jest-junit",
Expand Down
120 changes: 120 additions & 0 deletions src/Centreon/Application/Webservice/CentreonFrontendComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/*
* Copyright 2005-2019 Centreon
* Centreon is developed 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 Centreon
* 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 Centreon choice, provided that
* Centreon 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 : [email protected]
*
*/
namespace Centreon\Application\Webservice;

use CentreonRemote\Application\Webservice\CentreonWebServiceAbstract;

class CentreonFrontendComponent extends CentreonWebServiceAbstract
{
/**
* Name of web service object
*
* @return string
*/
public static function getName(): string
{
return 'centreon_frontend_component';
}

/**
* @SWG\Get(
* path="/centreon/api/internal.php",
* operationId="getComponents",
* @SWG\Parameter(
* in="query",
* name="object",
* type="string",
* description="the name of the API object class",
* required=true,
* enum="centreon_configuration_remote",
* ),
* @SWG\Parameter(
* in="query",
* name="action",
* type="string",
* description="the name of the action in the API class",
* required=true,
* enum="components",
* ),
* @SWG\Response(
* response=200,
* description="JSON with the external react components (pages, hooks)"
* )
* )
*
* Get list with remote components
*
* @return array
* @example [
* ['pages' => [
* '/my/module/route' => [
* 'js' => '<my_module_path>/static/pages/my/module/route/index.js',
* 'css' => '<my_module_path>/static/pages/my/module/route/index.css'
* ]
* ]],
* ['hooks' => [
* '/header/topCounter' => [
* [
* 'js' => '<my_module_path>/static/hooks/header/topCounter/index.js',
* 'css' => '<my_module_path>/static/hooks/header/topCounter/index.css'
* ]
* ]
* ]]
* ]
*/
public function getComponents(): array
{
$pages = $this->getDi()['centreon.frontend_component_service']->getPages();
$hooks = $this->getDi()['centreon.frontend_component_service']->getHooks();

return [
'pages' => $pages,
'hooks' => $hooks
];
}

/**
* Authorize to access to the action
*
* @param string $action The action name
* @param array $user The current user
* @param boolean $isInternal If the api is call in internal
* @return boolean If the user has access to the action
*/
public function authorize($action, $user, $isInternal = false)
{
return true;
}
}
155 changes: 155 additions & 0 deletions src/Centreon/Domain/Service/FrontendComponentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/*
* Copyright 2005-2019 Centreon
* Centreon is developed 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 Centreon
* 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 Centreon choice, provided that
* Centreon 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 : [email protected]
*
*/
namespace Centreon\Domain\Service;

use Pimple\Container;

/**
* Class to manage external frontend components provided by modules and widgets
*/
class FrontendComponentService
{
/**
* @var Container
*/
private $di;

/**
* FrontendComponentService constructor
*
* @param string $di The dependency injector
*/
public function __construct(Container $di)
{
$this->di = $di;
}

/**
* Dependency injector getter
*
* @return string The dependency injector
*/
private function getDi(): Container
{
return $this->di;
}

/**
* Get directory files grouped by directory matching regex
*
* @param string $dir the directory to explore
* @param array $results the found files
* @param string $regex the regex to match
* @return array
*/
private function getDirContents(string $dir, array &$results = [], string $regex = '/.*/'): array
{
$files = [];
if (is_dir($dir)) {
$files = scandir($dir);
}

foreach ($files as $key => $value) {
$path = $dir . DIRECTORY_SEPARATOR . $value;
if (!is_dir($path) && preg_match($regex, $path)) {
// group files by directory
$results[dirname($path)][] = basename($path);
} elseif ($value != "." && $value != "..") {
$this->getDirContents($path, $results, $regex);
}
}

return $results;
}

/**
* Get status for centreon instance (is remote or is not remote)
*
* @return array The list of hooks (js and css)
*/
public function getHooks(): array
{
// get installed modules
// @todo create serviceprovider in CentreonLegacy namespace
$utilsFactory = new \CentreonLegacy\Core\Utils\Factory($this->di);
$utils = $utilsFactory->newUtils();
$moduleFactory = new \CentreonLegacy\Core\Module\Factory($this->di, $utils);
$module = $moduleFactory->newInformation();
$installedModules = $module->getInstalledList();

// search in each installed modules if there are hooks
$hooks = [];
foreach (array_keys($installedModules) as $installedModule) {
$modulePath = __DIR__ . '/../../../../www/modules/' . $installedModule . '/static/hooks';
$files = [];
$this->getDirContents($modulePath, $files, '/\.(js|css)$/');
foreach ($files as $path => $hookFiles) {
if (preg_match('/\/static\/hooks(\/.+)$/', $path, $hookMatches)) {
// parse hook name by removing beginning of the path
$hookName = $hookMatches[1];
// set relative path
$hookPath = str_replace(__DIR__ . '/../../../../www', '', $path);

// add hook parameters (js and css files)
$hookParameters = [];
foreach ($hookFiles as $hookFile) {
if (preg_match('/\.js$/', $hookFile)) {
$hookParameters['js'] = $hookPath . '/' . $hookFile;
} elseif (preg_match('/\.css$/', $hookFile)) {
$hookParameters['css'] = $hookPath . '/' . $hookFile;
}
}

if (!empty($hookParameters)) {
$hooks[$hookName][] = $hookParameters;
}
}
}
}

return $hooks;
}

/**
* Get status for centreon instance (is master or is not master)
*
* @return array The list of pages (routes, js and css)
*/
public function getPages(): array
{
// @todo get external pages from modules
return [];
}
}
10 changes: 10 additions & 0 deletions src/Centreon/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

use Pimple\Container;
use Pimple\Psr11\ServiceLocator;
use Centreon\Application\Webservice;
use Centreon\Infrastructure\Provider\AutoloadServiceProviderInterface;
use Centreon\Infrastructure\Service\CentreonWebserviceService;
use Centreon\Infrastructure\Service\CentreonClapiService;
use Centreon\Infrastructure\Service\CentcoreConfigService;
use Centreon\Infrastructure\Service\CentreonDBManagerService;
use Centreon\Domain\Service\FrontendComponentService;
use Centreon\Domain\Service\AppKeyGeneratorService;
use Centreon\Domain\Service\BrokerConfigurationService;
use Centreon\Domain\Repository\CfgCentreonBrokerRepository;
Expand All @@ -30,6 +32,14 @@ public function register(Container $pimple): void
return $service;
};

// add webservice to get frontend hooks and pages installed by modules and widgets
$pimple['centreon.webservice']->add(Webservice\CentreonFrontendComponent::class);

$pimple['centreon.frontend_component_service'] = function (Container $pimple): FrontendComponentService {
$service = new FrontendComponentService($pimple);
return $service;
};

$pimple['centreon.clapi'] = function(Container $container): CentreonClapiService {
$service = new CentreonClapiService;

Expand Down
Loading

0 comments on commit cca6800

Please sign in to comment.