From 93194bb39617d4b11a0a84b8cd4caf0491155961 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <thomas.mueller@tmit.eu>
Date: Tue, 20 Aug 2013 17:21:14 +0200
Subject: [PATCH] Introducing IContainer into public api

---
 .../dependencyinjection/dicontainer.php       | 22 +++++-----
 lib/appframework/utility/simplecontainer.php  | 44 +++++++++++++++++++
 tests/lib/appframework/classloader.php        |  9 ++++
 3 files changed, 63 insertions(+), 12 deletions(-)
 create mode 100644 lib/appframework/utility/simplecontainer.php

diff --git a/lib/appframework/dependencyinjection/dicontainer.php b/lib/appframework/dependencyinjection/dicontainer.php
index 69c645b1be..88ad2cd414 100644
--- a/lib/appframework/dependencyinjection/dicontainer.php
+++ b/lib/appframework/dependencyinjection/dicontainer.php
@@ -30,19 +30,11 @@ use OC\AppFramework\Http\Dispatcher;
 use OC\AppFramework\Core\API;
 use OC\AppFramework\Middleware\MiddlewareDispatcher;
 use OC\AppFramework\Middleware\Security\SecurityMiddleware;
+use OC\AppFramework\Utility\SimpleContainer;
 use OC\AppFramework\Utility\TimeFactory;
 
-// register 3rdparty autoloaders
-require_once __DIR__ . '/../../../3rdparty/Pimple/Pimple.php';
 
-
-/**
- * This class extends Pimple (http://pimple.sensiolabs.org/) for reusability
- * To use this class, extend your own container from this. Should you require it
- * you can overwrite the dependencies with your own classes by simply redefining
- * a dependency
- */
-class DIContainer extends \Pimple {
+class DIContainer extends SimpleContainer {
 
 
 	/**
@@ -61,8 +53,14 @@ class DIContainer extends \Pimple {
 		 * Http
 		 */
 		$this['Request'] = $this->share(function($c) {
-			$params = json_decode(file_get_contents('php://input'), true);
-			$params = is_array($params) ? $params: array();
+
+			$params = array();
+
+			// we json decode the body only in case of content type json
+			if (isset($_SERVER['CONTENT_TYPE']) && stripos($_SERVER['CONTENT_TYPE'],'json') === true ) {
+				$params = json_decode(file_get_contents('php://input'), true);
+				$params = is_array($params) ? $params: array();
+			}
 
 			return new Request(
 				array(
diff --git a/lib/appframework/utility/simplecontainer.php b/lib/appframework/utility/simplecontainer.php
new file mode 100644
index 0000000000..04b6cd727b
--- /dev/null
+++ b/lib/appframework/utility/simplecontainer.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace OC\AppFramework\Utility;
+
+// register 3rdparty autoloaders
+require_once __DIR__ . '/../../../3rdparty/Pimple/Pimple.php';
+
+/**
+ * Class SimpleContainer
+ *
+ * SimpleContainer is a simple implementation of IContainer on basis of \Pimple
+ */
+class SimpleContainer extends \Pimple implements \OCP\Core\IContainer {
+
+	/**
+	 * @param string $name name of the service to query for
+	 * @return object registered service for the given $name
+	 */
+	public function query($name) {
+		return $this->offsetGet($name);
+	}
+
+	function registerParameter($name, $value)
+	{
+		$this[$name] = $value;
+	}
+
+	/**
+	 * The given closure is call the first time the given service is queried.
+	 * The closure has to return the instance for the given service.
+	 * Created instance will be cached in case $shared is true.
+	 *
+	 * @param string $name name of the service to register another backend for
+	 * @param callable $closure the closure to be called on service creation
+	 */
+	function registerService($name, \Closure $closure, $shared = true)
+	{
+		if ($shared) {
+			$this[$name] = \Pimple::share($closure);
+		} else {
+			$this[$name] = $closure;
+		}
+	}
+}
diff --git a/tests/lib/appframework/classloader.php b/tests/lib/appframework/classloader.php
index ae485e67b2..cd9f893df3 100644
--- a/tests/lib/appframework/classloader.php
+++ b/tests/lib/appframework/classloader.php
@@ -32,6 +32,15 @@ spl_autoload_register(function ($className){
 		}
 	}
 
+	if (strpos($className, 'OCP\\') === 0) {
+		$path = strtolower(str_replace('\\', '/', substr($className, 3)) . '.php');
+		$relPath = __DIR__ . '/../../../lib/public' . $path;
+
+		if(file_exists($relPath)){
+			require_once $relPath;
+		}
+	}
+
 	// FIXME: this will most probably not work anymore
 	if (strpos($className, 'OCA\\') === 0) {
 
-- 
GitLab