N°2060 [WIP] Initialisation of the portal application:

- Refactor kernel bootstrapping:
  - Make bin/console from SF work
  - Make iTopPortalEditUrlMaker / iTopPortalViewUrlMaker work again
- Add classmap to /application in composer.json
This commit is contained in:
Molkobain
2019-07-05 15:53:05 +02:00
parent ab3024d98a
commit 322ea1870d
7 changed files with 164 additions and 120 deletions

View File

@@ -22,45 +22,16 @@
* This allows to make a portal directly from the ITSM Designer. * This allows to make a portal directly from the ITSM Designer.
*/ */
// Load current environment
if (file_exists(__DIR__ . '/../../approot.inc.php')) if (file_exists(__DIR__ . '/../../approot.inc.php'))
{ {
require_once __DIR__ . '/../../approot.inc.php'; // When in env-xxxx folder require_once __DIR__ . '/../../approot.inc.php'; // When in env-xxxx folder
} }
else else
{ {
require_once __DIR__ . '/../../../approot.inc.php'; // When in datamodels/x.x folder require_once __DIR__ . '/../../../approot.inc.php'; // When in datamodels/x.x folder
} }
require_once APPROOT . '/application/startup.inc.php'; require_once APPROOT . 'application/startup.inc.php';
// If PORTAL_ID is not already defined, we look for it in a parameter // Load frontal
if(!defined('PORTAL_ID')) require_once MODULESROOT . 'itop-portal-base/portal/public/index.php';
{
// Retrieving portal id from request params
$sPortalId = utils::ReadParam('portal_id', '');
if ($sPortalId == '')
{
echo "Missing argument 'portal_id'";
exit;
}
// Defining portal constants
define('PORTAL_ID', $sPortalId);
}
// Set debug mode only when necessary
if (utils::ReadParam('debug', 'false') === 'true')
{
$_SERVER['APP_DEBUG'] = true;
}
define('PORTAL_CACHE_PATH', utils::GetCachePath() . '/portals/' . PORTAL_ID . '/');
// Constants to be used in templates and others
define('COMBODO_CURRENT_ENVIRONMENT', utils::GetCurrentEnvironment());
define('COMBODO_ABSOLUTE_URL', utils::GetAbsoluteUrlAppRoot());
define('COMBODO_MODULES_ABSOLUTE_URL', utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment());
define('COMBODO_PORTAL_BASE_ABSOLUTE_URL', utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/public/');
define('COMBODO_PORTAL_BASE_ABSOLUTE_PATH', MODULESROOT . '/itop-portal-base/portal/public/');
define('COMBODO_PORTAL_INSTANCE_ABSOLUTE_URL', utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment() . '/' . PORTAL_ID . '/');
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/public/index.php';

View File

@@ -1,10 +1,9 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use App\Kernel; use Combodo\iTop\Portal\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
if (false === in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) { if (false === in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL; echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL;
@@ -12,13 +11,36 @@ if (false === in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
set_time_limit(0); set_time_limit(0);
require dirname(__DIR__).'/vendor/autoload.php'; if(!defined('APPROOT'))
{
if (file_exists(__DIR__ . '/../../../../approot.inc.php'))
{
require_once __DIR__ . '/../../../../approot.inc.php'; // When in env-xxxx folder
}
else
{
require_once __DIR__ . '/../../../../../approot.inc.php'; // When in datamodels/x.x folder
}
}
require_once APPROOT . 'lib/composer-vendor/autoload.php';
if (!class_exists(Application::class)) { if (!class_exists(Application::class)) {
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.'); throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
} }
$input = new ArgvInput(); // Remove --portal_id from CLI params to avoid SF CLI conflicts
// Note: The parameter is needed when calling the bin/console to determine which portal to select
$aCleanedArgv = $_SERVER['argv'];
foreach($aCleanedArgv as $iArg => $sArg)
{
if (preg_match('/^--portal_id=(.*)$/', $sArg, $aMatches))
{
unset($aCleanedArgv[$iArg]);
break;
}
}
$input = new ArgvInput($aCleanedArgv);
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) { if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
} }
@@ -27,15 +49,7 @@ if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
} }
require dirname(__DIR__).'/config/bootstrap.php'; require_once MODULESROOT . 'itop-portal-base/portal/config/bootstrap.php';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
if (class_exists(Debug::class)) {
Debug::enable();
}
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$application = new Application($kernel); $application = new Application($kernel);

View File

@@ -27,7 +27,8 @@
"Combodo\\iTop\\Portal\\": "src/" "Combodo\\iTop\\Portal\\": "src/"
}, },
"classmap": [ "classmap": [
"../../../../core" "../../../../core",
"../../../../application"
] ]
}, },
"autoload-dev": { "autoload-dev": {

View File

@@ -1,8 +1,23 @@
<?php <?php
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\Dotenv\Dotenv;
require APPROOT.'/lib/composer-vendor/autoload.php'; require_once APPROOT.'/lib/composer-vendor/autoload.php';
// Load current environment if necessary
if(!defined('MODULESROOT'))
{
if (file_exists(__DIR__ . '/../../../../approot.inc.php'))
{
require_once __DIR__ . '/../../../../approot.inc.php'; // When in env-xxxx folder
}
else
{
require_once __DIR__ . '/../../../../../approot.inc.php'; // When in datamodels/x.x folder
}
require_once APPROOT . '/application/startup.inc.php';
}
// Load cached env vars if the .env.local.php file exists // Load cached env vars if the .env.local.php file exists
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2) // Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
@@ -45,7 +60,56 @@ if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
} }
} }
// Set debug mode only when necessary
if (utils::ReadParam('debug', 'false') === 'true')
{
$_SERVER['APP_DEBUG'] = true;
}
$_SERVER += $_ENV; $_SERVER += $_ENV;
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = (isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) ?: 'prod'; $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = (isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) ?: 'prod';
$_SERVER['APP_DEBUG'] = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : (isset($_ENV['APP_DEBUG']) ? $_ENV['APP_DEBUG'] : ('prod' !== $_SERVER['APP_ENV'])); $_SERVER['APP_DEBUG'] = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : (isset($_ENV['APP_DEBUG']) ? $_ENV['APP_DEBUG'] : ('prod' !== $_SERVER['APP_ENV']));
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
if (class_exists(Debug::class)) {
Debug::enable();
}
}
// If PORTAL_ID is not already defined, we look for it in a parameter
if(!defined('PORTAL_ID'))
{
// Retrieving portal id from request params
$sPortalId = utils::ReadParam('portal_id', '', true);
if ($sPortalId == '')
{
echo "Missing argument 'portal_id'";
exit;
}
// Defining portal constants
define('PORTAL_ID', $sPortalId);
}
else
{
@trigger_error(
sprintf(
'Usage of legacy "PORTAL_ID" constant ("%s") is deprecated. You should pass "portal_id" in the URL as GET parameter.',
PORTAL_ID
),
E_USER_DEPRECATED
);
}
define('PORTAL_CACHE_PATH', utils::GetCachePath() . '/portals/' . PORTAL_ID . '/');
// Constants to be used in templates and others
define('COMBODO_CURRENT_ENVIRONMENT', utils::GetCurrentEnvironment());
define('COMBODO_ABSOLUTE_URL', utils::GetAbsoluteUrlAppRoot());
define('COMBODO_MODULES_ABSOLUTE_URL', utils::GetAbsoluteUrlModulesRoot());
define('COMBODO_PORTAL_BASE_ABSOLUTE_URL', utils::GetAbsoluteUrlModulesRoot() . 'itop-portal-base/portal/public/');
define('COMBODO_PORTAL_BASE_ABSOLUTE_PATH', MODULESROOT . '/itop-portal-base/portal/public/');
define('COMBODO_PORTAL_INSTANCE_ABSOLUTE_URL', utils::GetAbsoluteUrlModulesRoot() . PORTAL_ID . '/');

View File

@@ -1,17 +1,9 @@
<?php <?php
use Combodo\iTop\Portal\Kernel; use Combodo\iTop\Portal\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
require dirname(__DIR__).'/config/bootstrap.php'; require_once MODULESROOT . 'itop-portal-base/portal/config/bootstrap.php';
require_once APPROOT . '/application/loginwebpage.class.inc.php';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
// Note: Manually refactored ternary condition to be PHP 5.x compatible // Note: Manually refactored ternary condition to be PHP 5.x compatible
if ($trustedProxies = isset($_SERVER['TRUSTED_PROXIES']) ? $_SERVER['TRUSTED_PROXIES'] : (isset($_ENV['TRUSTED_PROXIES']) ? $_ENV['TRUSTED_PROXIES'] : false) ) { if ($trustedProxies = isset($_SERVER['TRUSTED_PROXIES']) ? $_SERVER['TRUSTED_PROXIES'] : (isset($_ENV['TRUSTED_PROXIES']) ? $_ENV['TRUSTED_PROXIES'] : false) ) {

View File

@@ -36,6 +36,7 @@
E_USER_DEPRECATED E_USER_DEPRECATED
); );
// Load current environment
if (file_exists(__DIR__ . '/../../approot.inc.php')) if (file_exists(__DIR__ . '/../../approot.inc.php'))
{ {
require_once __DIR__ . '/../../approot.inc.php'; // When in env-xxxx folder require_once __DIR__ . '/../../approot.inc.php'; // When in env-xxxx folder
@@ -44,7 +45,7 @@ else
{ {
require_once __DIR__ . '/../../../approot.inc.php'; // When in datamodels/x.x folder require_once __DIR__ . '/../../../approot.inc.php'; // When in datamodels/x.x folder
} }
require_once APPROOT . '/application/startup.inc.php'; require_once APPROOT . 'application/startup.inc.php';
// Protection against setup in the following configuration : ITIL Ticket with Enhanced Portal selected but neither UserRequest or Incident. Which would crash the portal. // Protection against setup in the following configuration : ITIL Ticket with Enhanced Portal selected but neither UserRequest or Incident. Which would crash the portal.
if (!class_exists('UserRequest') && !class_exists('Incident')) if (!class_exists('UserRequest') && !class_exists('Incident'))
@@ -56,4 +57,5 @@ if (!class_exists('UserRequest') && !class_exists('Incident'))
$sDir = basename(__DIR__); $sDir = basename(__DIR__);
define('PORTAL_ID', $sDir); define('PORTAL_ID', $sDir);
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/index.php'; // Load frontal
require_once MODULESROOT . 'itop-portal-base/index.php';

View File

@@ -1,29 +1,38 @@
<?php <?php
// Copyright (C) 2018 Combodo SARL /**
// * Copyright (C) 2013-2019 Combodo SARL
// This file is part of iTop. *
// * This file is part of iTop.
// iTop is free software; you can redistribute it and/or modify *
// it under the terms of the GNU Affero General Public License as published by * iTop is free software; you can redistribute it and/or modify
// the Free Software Foundation, either version 3 of the License, or * it under the terms of the GNU Affero General Public License as published by
// (at your option) any later version. * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// iTop is distributed in the hope that it will be useful, *
// but WITHOUT ANY WARRANTY; without even the implied warranty of * iTop is distributed in the hope that it will be useful,
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU Affero General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License *
// along with iTop. If not, see <http://www.gnu.org/licenses/> * You should have received a copy of the GNU Affero General Public License
*
*
*/
use Combodo\iTop\Portal\Kernel;
/** /**
* main.itop-portal.php * iTopPortalEditUrlMaker
* *
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com> * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @author Bruno Da Silva <bruno.dasilva@combodo.com>
* @since 2.3.0
*/ */
class iTopPortalEditUrlMaker implements iDBObjectURLMaker class iTopPortalEditUrlMaker implements iDBObjectURLMaker
{ {
private static $oKernel;
/** /**
* Generate an (absolute) URL to an object, either in view or edit mode. * Generate an (absolute) URL to an object, either in view or edit mode.
* Returns null if the current user is not allowed to view / edit object. * Returns null if the current user is not allowed to view / edit object.
@@ -39,47 +48,19 @@ class iTopPortalEditUrlMaker implements iDBObjectURLMaker
*/ */
public static function PrepareObjectURL($sClass, $iId, $sMode) public static function PrepareObjectURL($sClass, $iId, $sMode)
{ {
require_once APPROOT . '/lib/silex/vendor/autoload.php'; require_once APPROOT . 'lib/composer-vendor/autoload.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/providers/urlgeneratorserviceprovider.class.inc.php'; require_once MODULESROOT . 'itop-portal-base/portal/config/bootstrap.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/urlgeneratorhelper.class.inc.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/providers/scopevalidatorserviceprovider.class.inc.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/scopevalidatorhelper.class.inc.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/securityhelper.class.inc.php';
require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/applicationhelper.class.inc.php';
// Using a static var allows to preserve the object through function calls $oKernel = self::GetKernelInstance();
static $oApp = null; $oContainer = $oKernel->getContainer();
static $sPortalId = null;
// Initializing Silex app (partially for faster execution) /** @var string $sPortalId */
// TODO: This should be factorised with itop-portal-base/portal/web/index.php into the ApplicationHelper class. $sPortalId = $oContainer->getParameter('combodo.portal.instance.id');
if ($oApp === null)
{
// Retrieving portal id
$sPortalId = basename(__DIR__);
// Initializing Silex framework /** @var \Combodo\iTop\Portal\Routing\UrlGenerator $oUrlGenerator */
$oApp = new Silex\Application(); $oUrlGenerator = $oContainer->get('url_generator');
// Registering optional silex components /** @var \Combodo\iTop\Portal\Helper\SecurityHelper $oSecurityHelper */
$oApp->register(new Combodo\iTop\Portal\Provider\UrlGeneratorServiceProvider()); $oSecurityHelper = $oContainer->get('security_helper');
$oApp->register(new Combodo\iTop\Portal\Provider\ScopeValidatorServiceProvider(), array(
'scope_validator.scopes_path' => utils::GetCachePath(),
'scope_validator.scopes_filename' => $sPortalId . '.scopes.php',
'scope_validator.instance_name' => $sPortalId
));
// Preparing portal foundations (partially)
// ...
Combodo\iTop\Portal\Helper\ApplicationHelper::LoadRouters();
Combodo\iTop\Portal\Helper\ApplicationHelper::RegisterRoutes($oApp);
// ...
// Loading portal scopes from the module design
Combodo\iTop\Portal\Helper\ApplicationHelper::LoadScopesConfiguration($oApp, new ModuleDesign($sPortalId));
}
/** @var \Combodo\iTop\Portal\Helper\UrlGenerator $oUrlGenerator */
$oUrlGenerator = $oApp['url_generator'];
// The object is reachable in the specified mode (edit/view) // The object is reachable in the specified mode (edit/view)
// //
@@ -88,7 +69,7 @@ class iTopPortalEditUrlMaker implements iDBObjectURLMaker
switch($sMode) switch($sMode)
{ {
case 'view': case 'view':
if(!ContextTag::Check('GUI:Portal') || Combodo\iTop\Portal\Helper\SecurityHelper::IsActionAllowed($oApp, UR_ACTION_READ, $sClass, $iId)) if(!ContextTag::Check('GUI:Portal') || $oSecurityHelper->IsActionAllowed(UR_ACTION_READ, $sClass, $iId))
{ {
$sObjectQueryString = $oUrlGenerator->generate('p_object_view', array('sObjectClass' => $sClass, 'sObjectId' => $iId)); $sObjectQueryString = $oUrlGenerator->generate('p_object_view', array('sObjectClass' => $sClass, 'sObjectId' => $iId));
} }
@@ -97,11 +78,11 @@ class iTopPortalEditUrlMaker implements iDBObjectURLMaker
case 'edit': case 'edit':
default: default:
// Checking if user is allowed to edit object, if not we check if it can at least view it. // Checking if user is allowed to edit object, if not we check if it can at least view it.
if(!ContextTag::Check('GUI:Portal') || Combodo\iTop\Portal\Helper\SecurityHelper::IsActionAllowed($oApp, UR_ACTION_MODIFY, $sClass, $iId)) if(!ContextTag::Check('GUI:Portal') || $oSecurityHelper->IsActionAllowed(UR_ACTION_MODIFY, $sClass, $iId))
{ {
$sObjectQueryString = $oUrlGenerator->generate('p_object_edit', array('sObjectClass' => $sClass, 'sObjectId' => $iId)); $sObjectQueryString = $oUrlGenerator->generate('p_object_edit', array('sObjectClass' => $sClass, 'sObjectId' => $iId));
} }
elseif(!ContextTag::Check('GUI:Portal') || Combodo\iTop\Portal\Helper\SecurityHelper::IsActionAllowed($oApp, UR_ACTION_READ, $sClass, $iId)) elseif(!ContextTag::Check('GUI:Portal') || $oSecurityHelper->IsActionAllowed(UR_ACTION_READ, $sClass, $iId))
{ {
$sObjectQueryString = $oUrlGenerator->generate('p_object_view', array('sObjectClass' => $sClass, 'sObjectId' => $iId)); $sObjectQueryString = $oUrlGenerator->generate('p_object_view', array('sObjectClass' => $sClass, 'sObjectId' => $iId));
} }
@@ -144,12 +125,31 @@ class iTopPortalEditUrlMaker implements iDBObjectURLMaker
{ {
return static::PrepareObjectURL($sClass, $iId, 'edit'); return static::PrepareObjectURL($sClass, $iId, 'edit');
} }
/**
* Returns the kernel singleton
*
* @return \Combodo\iTop\Portal\Kernel
* @since 2.7.0
*/
private static function GetKernelInstance()
{
if(self::$oKernel === null)
{
self::$oKernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
self::$oKernel->boot();
}
return self::$oKernel;
}
} }
/** /**
* Hyperlinks to the "view" of the object (vs edition) * Hyperlinks to the "view" of the object (vs edition)
* @author denis
* *
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @author Bruno Da Silva <bruno.dasilva@combodo.com>
* @since 2.3.0
*/ */
class iTopPortalViewUrlMaker extends iTopPortalEditUrlMaker class iTopPortalViewUrlMaker extends iTopPortalEditUrlMaker
{ {