From fb321fa034c12369658ecd9c36ee4ec7751bea75 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 26 Feb 2021 16:47:02 +0100 Subject: [PATCH] Fix ContextTag usage, and add some explanations on the declaration of ContextTag --- core/contexttag.class.inc.php | 18 ++++++++++++++++++ datamodels/2.x/itop-core-update/ajax.php | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/contexttag.class.inc.php b/core/contexttag.class.inc.php index 488ad1fa1..5176c3b52 100644 --- a/core/contexttag.class.inc.php +++ b/core/contexttag.class.inc.php @@ -20,6 +20,24 @@ /** * Simple helper class for keeping track of the context inside the call stack * + * Beware: + * As the destructor removes the last context, don't use the same variable if you want to keep your context. + * $oCtx = new ContextTag("Tag1"); + * $oCtx = new ContextTag("Tag2"); // Bad the destructor will remove "Tag2" + * $oCtx = new ContextTag("Tag3"); // Bad the destructor will remove "Tag3" + * + * Instead, use separate variables: + * $oCtx1 = new ContextTag("Tag1"); + * $oCtx2 = new ContextTag("Tag2"); + * $oCtx3 = new ContextTag("Tag3"); + * + * And don't forget that any destructor (of $oCtx1, $oCtx2 or $oCtx3) will remove the LAST added tag ("Tag3" in our example) + * + * If you want to declare permanent contexts (request lifetime) you should use: + * ContextTag::AddContext("Tag1"); + * ContextTag::AddContext("Tag2"); + * ContextTag::AddContext("Tag3"); + * * To check (anywhere in the code) if a particular context tag is present * in the call stack simply do: * diff --git a/datamodels/2.x/itop-core-update/ajax.php b/datamodels/2.x/itop-core-update/ajax.php index ce60866ef..6a644b5de 100644 --- a/datamodels/2.x/itop-core-update/ajax.php +++ b/datamodels/2.x/itop-core-update/ajax.php @@ -23,7 +23,7 @@ require_once(MODULESROOT.'itop-core-update/src/Controller/AjaxController.php'); MetaModel::LoadConfig(utils::GetConfig()); -new ContextTag(ContextTag::TAG_SETUP); +$oCtxCoreUpdate = new ContextTag(ContextTag::TAG_SETUP); $oUpdateController = new AjaxController(MODULESROOT.'itop-core-update/view', 'itop-core-update'); $oUpdateController->DisableInDemoMode(); @@ -32,3 +32,5 @@ $oUpdateController->AllowOnlyAdmin(); // Allow parallel execution of ajax requests session_write_close(); $oUpdateController->HandleAjaxOperation(); + +unset($oCtxCoreUpdate);