This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacto(front): Rework dynamic components + build process (#7255)
- Loading branch information
Showing
44 changed files
with
5,776 additions
and
4,566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/Centreon/Application/Webservice/CentreonFrontendComponent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
155
src/Centreon/Domain/Service/FrontendComponentService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.