From e1113676d7f1f4ea7ea2cba7511bafe436a88ce1 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Fri, 19 Sep 2014 15:49:03 +0200
Subject: [PATCH] rel #16325 : InspectorGadget is born

---
 .../WebService/SIGB/AbstractRESTService.php   | 18 +++--
 .../Class/WebService/SIGB/AbstractService.php | 14 ++++
 .../Controller/Plugin/InspectorGadget.php     | 74 +++++++++++++++++++
 library/startup.php                           |  1 +
 4 files changed, 99 insertions(+), 8 deletions(-)
 create mode 100644 library/ZendAfi/Controller/Plugin/InspectorGadget.php

diff --git a/library/Class/WebService/SIGB/AbstractRESTService.php b/library/Class/WebService/SIGB/AbstractRESTService.php
index 69bc2ed0b16..0989be3e641 100644
--- a/library/Class/WebService/SIGB/AbstractRESTService.php
+++ b/library/Class/WebService/SIGB/AbstractRESTService.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebService_SIGB_AbstractService {
@@ -82,13 +82,15 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
 	public function httpGet($options) {
 		$url = $this->buildQueryURL($options);
 		try {
-			return $this->getWebClient()->open_url($url);
+			$response = $this->getWebClient()->open_url($url);
+			$this->log();
+			return $response;
 		} catch (Exception $e) {
 			return '';
 		}
 	}
 
-	
+
 	/**
 	 * @param string $xml
 	 * @param string $tag
@@ -124,7 +126,7 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
 																$reader);
 	}
 
- 
+
 	/**
 	 * @param array $params
 	 * @param Class_WebService_SIGB_AbstractILSDIPatronInfoReader $reader
@@ -168,7 +170,7 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
 
 		return false;
 	}
-		
+
 
 	/**
 	 * @param array $params
@@ -202,7 +204,7 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
 	public function ilsdiRenewLoan($params, $error_tag='error') {
 		return $this->ilsdiAction('RenewLoan', $params, $error_tag, 'Prolongation impossible');
 	}
-	
+
 
 	/**
 	 * @param string $xml
@@ -211,12 +213,12 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
 		return $this->ilsdiCheckXml($xml, $error_tag, 'Mise à jour impossible');
 	}
 
-		
+
 	public function ilsdiAction($name, $params, $error_tag, $error_message) {
 		$params = array_merge(array('service' => $name), $params);
 		return $this->ilsdiCheckXml($this->httpGet($params), $error_tag, $error_message);
 	}
-	
+
 
 	public function ilsdiCheckXml($xml, $error_tag, $error_message) {
 		if (!$xml)
diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php
index aebe8404272..504d932d801 100644
--- a/library/Class/WebService/SIGB/AbstractService.php
+++ b/library/Class/WebService/SIGB/AbstractService.php
@@ -20,6 +20,7 @@
  */
 
 abstract class Class_WebService_SIGB_AbstractService {
+	protected static $_logger;
 	protected $_notice_cache;
 
 	/**
@@ -30,6 +31,19 @@ abstract class Class_WebService_SIGB_AbstractService {
 	}
 
 
+	public static function setLogger($logger) {
+		static::$_logger = $logger;
+	}
+
+
+	public static function log() {
+		if (!static::$_logger)
+			return;
+
+		static::$_logger->log();
+	}
+
+
 	public function getReservationsOf($emprunteur) {
 		return array();
 	}
diff --git a/library/ZendAfi/Controller/Plugin/InspectorGadget.php b/library/ZendAfi/Controller/Plugin/InspectorGadget.php
new file mode 100644
index 00000000000..3f62960ec64
--- /dev/null
+++ b/library/ZendAfi/Controller/Plugin/InspectorGadget.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_Abstract {
+	protected $_enabled = false;
+	protected $_calls = [];
+
+	public function preDispatch(Zend_Controller_Request_Abstract  $request) {
+		/*if (!$request->getParam('inspector_gadget', false))
+			return;*/
+
+		$this->beEnabled();
+		Class_WebService_SIGB_AbstractService::setLogger($this);
+	}
+
+
+	protected function beEnabled() {
+		$this->_enabled = true;
+	}
+
+
+	public function postDispatch(Zend_Controller_Request_Abstract $request) {
+		if (!$this->_enabled)
+			return;
+
+		$html = '<ul>';
+		foreach($this->_calls as $call) {
+			$html .= sprintf('<li>%s, %s, %s</li>',
+											 $call->request, $call->response_code, $call->response_body);
+		}
+
+		if (false === strpos('</body>', $this->_response->getBody())) {
+			$this->_response->setBody($html . $this->_response->getBody());
+			return;
+		}
+
+		$this->_response->setBody(str_ireplace('</body>', $html . '</ul></body>',
+																					 $this->_response->getBody()));
+	}
+
+
+	public function log() {
+		$httpClient = Zend_Registry::get('httpClient');
+		$call = new StdClass();
+		$call->request = $httpClient->getLastRequest();
+
+		if ($response = $httpClient->getLastResponse()) {
+			$call->response_code = $response->getStatus();
+			$call->response_body = $response->getBody();
+		}
+
+		$this->_calls[] = $call;
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/library/startup.php b/library/startup.php
index c828e1efc4b..28f9dc64049 100644
--- a/library/startup.php
+++ b/library/startup.php
@@ -278,6 +278,7 @@ function setupFrontController($cfg) {
 		->registerPlugin(new ZendAfi_Controller_Plugin_TogetherJS())
 		->registerPlugin(new ZendAfi_Controller_Plugin_CustomFields())
 		->registerPlugin(new ZendAfi_Controller_Plugin_Lectura())
+		->registerPlugin(new ZendAfi_Controller_Plugin_InspectorGadget())
 		->setParam('useDefaultControllerAlways', true);
 
 
-- 
GitLab