Kaynağa Gözat

Internal: context tags to (programmatically) identify the context of the execution.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@4259 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 9 yıl önce
ebeveyn
işleme
4431bfe450

+ 2 - 0
application/itopwebpage.class.inc.php

@@ -43,11 +43,13 @@ class iTopWebPage extends NiceWebPage implements iTabbedPage
 	protected $sBreadCrumbEntryDescription;
 	protected $sBreadCrumbEntryUrl;
 	protected $sBreadCrumbEntryIcon;
+	protected $oCtx;
 
 	public function __construct($sTitle, $bPrintable = false)
 	{
 		parent::__construct($sTitle, $bPrintable);
 		$this->m_oTabs = new TabManager();
+		$this->oCtx = new ContextTag('GUI:Console');
 
 		ApplicationContext::SetUrlMakerClass('iTopStandardURLMaker');
 

+ 3 - 0
application/portalwebpage.class.inc.php

@@ -49,9 +49,12 @@ class PortalWebPage extends NiceWebPage
 	 */
 	protected $m_sWelcomeMsg;
 	protected $m_aMenuButtons;
+	protected $m_oCtx;
+	
 	
     public function __construct($sTitle, $sAlternateStyleSheet = '')
     {
+    	$this->m_oCtx = new ContextTag('GUI:Portal');
     	$this->m_sWelcomeMsg = '';
     	$this->m_aMenuButtons = array();
         parent::__construct($sTitle);

+ 1 - 0
application/startup.inc.php

@@ -26,6 +26,7 @@
 
 require_once(APPROOT.'/core/cmdbobject.class.inc.php');
 require_once(APPROOT.'/application/utils.inc.php');
+require_once(APPROOT.'/core/contexttag.class.inc.php');
 session_name('itop-'.md5(APPROOT));
 session_start();
 $sSwitchEnv = utils::ReadParam('switch_env', null);

+ 75 - 0
core/contexttag.class.inc.php

@@ -0,0 +1,75 @@
+<?php
+// Copyright (C) 2016 Combodo SARL
+//
+//   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
+//   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
+//   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/>
+
+
+/**
+ * Simple helper class for keeping track of the context inside the call stack
+ * 
+ * To check (anywhere in the code) if a particular context tag is present
+ * in the call stack simply do:
+ * 
+ * if (ContextTag::Check(<the_tag>)) ...
+ * 
+ * For example to know if the code is being executed in the context of a portal do:
+ * 
+ * if (ContextTag::Check('GUI:Portal'))
+ *
+ * @copyright   Copyright (C) 2016 Combodo SARL
+ * @license     http://opensource.org/licenses/AGPL-3.0
+ */
+
+class ContextTag
+{
+	protected static $aStack;
+	
+	/**
+	 * Store a context tag on the stack
+	 * @param string $sTag
+	 */
+	public function __construct($sTag)
+	{
+		static::$aStack[] = $sTag;
+	}
+	
+	/**
+	 * Cleanup the context stack
+	 */
+	public function __destruct()
+	{
+		array_pop(static::$aStack);
+	}
+	
+	/**
+	 * Check if a given tag is present in the stack
+	 * @param string $sTag
+	 * @return bool
+	 */
+	public static function Check($sTag)
+	{
+		return in_array($sTag, static::$aStack);
+	}
+	
+	/**
+	 * Get the whole stack as an array
+	 * @return hash
+	 */
+	public static function GetStack()
+	{
+		return static::$aStack;
+	}
+}

+ 3 - 0
datamodels/2.x/itop-portal-base/portal/web/index.php

@@ -49,6 +49,9 @@ if (UserRights::GetContactId() == 0)
 	die(Dict::S('Portal:ErrorNoContactForThisUser'));
 }
 
+$oCtx = new ContextTag('GUI:Portal');
+$oCtx2 = new ContextTag('Portal:'.PORTAL_MODULE_ID);
+
 // Checking if debug param is on
 $bDebug = (isset($_REQUEST['debug']) && ($_REQUEST['debug'] === 'true') );
 

+ 5 - 1
synchro/synchrodatasource.class.inc.php

@@ -2244,7 +2244,9 @@ class SynchroExecution
 	protected $m_aReconciliationKeys = array();
 	protected $m_aAttributes = array();
 	protected $m_iCountAllReplicas = 0;
-
+	protected $m_oCtx;
+	protected $m_oCtx1;
+	
 	/**
 	 * Constructor
 	 * @param SynchroDataSource $oDataSource Synchronization task
@@ -2255,6 +2257,8 @@ class SynchroExecution
 	{
 		$this->m_oDataSource = $oDataSource;
 		$this->m_oLastFullLoadStartDate = $oLastFullLoadStartDate;
+		$this->m_oCtx = new ContextTag('Synchro');
+		$this->m_oCtx1 = new ContextTag('Synchro:'.$oDataSource->GetRawName()); // More precise context information
 	}
 
 	/**

+ 1 - 1
webservices/cron.php

@@ -39,7 +39,7 @@ if (!file_exists($sConfigFile))
 
 require_once(APPROOT.'/application/startup.inc.php');
 
-
+$oCtx = new ContextTag('CRON');
 
 function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
 {

+ 1 - 0
webservices/rest.php

@@ -91,6 +91,7 @@ class RestResultListOperations extends RestResult
 // Main
 //
 $oP = new ajax_page('rest');
+$oCtx = new ContextTag('REST/JSON');
 
 $sVersion = utils::ReadParam('version', null, false, 'raw_data');
 $sOperation = utils::ReadParam('operation', null);