Completed
Pull Request — master (#8662)
by Joas
18:55
created
lib/private/AppFramework/Utility/SimpleContainer.php 1 patch
Indentation   +135 added lines, -135 removed lines patch added patch discarded remove patch
@@ -43,143 +43,143 @@
 block discarded – undo
43 43
 class SimpleContainer extends Container implements IContainer {
44 44
 
45 45
 
46
-	/**
47
-	 * @param ReflectionClass $class the class to instantiate
48
-	 * @return \stdClass the created class
49
-	 */
50
-	private function buildClass(ReflectionClass $class) {
51
-		$constructor = $class->getConstructor();
52
-		if ($constructor === null) {
53
-			return $class->newInstance();
54
-		} else {
55
-			$parameters = [];
56
-			foreach ($constructor->getParameters() as $parameter) {
57
-				$parameterClass = $parameter->getClass();
58
-
59
-				// try to find out if it is a class or a simple parameter
60
-				if ($parameterClass === null) {
61
-					$resolveName = $parameter->getName();
62
-				} else {
63
-					$resolveName = $parameterClass->name;
64
-				}
65
-
66
-				try {
67
-					$parameters[] = $this->query($resolveName);
68
-				} catch (\Exception $e) {
69
-					if (class_exists("PHPUnit_Framework_AssertionFailedError", false) &&
70
-						$e instanceof \PHPUnit_Framework_AssertionFailedError) {
71
-						// Easier debugging of "Your test case is not allowed to access the database."
72
-						throw $e;
73
-					}
74
-
75
-					// Service not found, use the default value when available
76
-					if ($parameter->isDefaultValueAvailable()) {
77
-						$parameters[] = $parameter->getDefaultValue();
78
-					} else if ($parameterClass !== null) {
79
-						$resolveName = $parameter->getName();
80
-						$parameters[] = $this->query($resolveName);
81
-					} else {
82
-						throw $e;
83
-					}
84
-				}
85
-			}
86
-			return $class->newInstanceArgs($parameters);
87
-		}
88
-	}
89
-
90
-
91
-	/**
92
-	 * If a parameter is not registered in the container try to instantiate it
93
-	 * by using reflection to find out how to build the class
94
-	 * @param string $name the class name to resolve
95
-	 * @return \stdClass
96
-	 * @throws QueryException if the class could not be found or instantiated
97
-	 */
98
-	public function resolve($name) {
99
-		$baseMsg = 'Could not resolve ' . $name . '!';
100
-		try {
101
-			$class = new ReflectionClass($name);
102
-			if ($class->isInstantiable()) {
103
-				return $this->buildClass($class);
104
-			} else {
105
-				throw new QueryException($baseMsg .
106
-					' Class can not be instantiated');
107
-			}
108
-		} catch(ReflectionException $e) {
109
-			throw new QueryException($baseMsg . ' ' . $e->getMessage());
110
-		}
111
-	}
112
-
113
-
114
-	/**
115
-	 * @param string $name name of the service to query for
116
-	 * @return mixed registered service for the given $name
117
-	 * @throws QueryException if the query could not be resolved
118
-	 */
119
-	public function query($name) {
120
-		$name = $this->sanitizeName($name);
121
-		if ($this->offsetExists($name)) {
122
-			return $this->offsetGet($name);
123
-		} else {
124
-			$object = $this->resolve($name);
125
-			$this->registerService($name, function () use ($object) {
126
-				return $object;
127
-			});
128
-			return $object;
129
-		}
130
-	}
131
-
132
-	/**
133
-	 * @param string $name
134
-	 * @param mixed $value
135
-	 */
136
-	public function registerParameter($name, $value) {
137
-		$this[$name] = $value;
138
-	}
139
-
140
-	/**
141
-	 * The given closure is call the first time the given service is queried.
142
-	 * The closure has to return the instance for the given service.
143
-	 * Created instance will be cached in case $shared is true.
144
-	 *
145
-	 * @param string $name name of the service to register another backend for
146
-	 * @param Closure $closure the closure to be called on service creation
147
-	 * @param bool $shared
148
-	 */
149
-	public function registerService($name, Closure $closure, $shared = true) {
150
-		$name = $this->sanitizeName($name);
151
-		if (isset($this[$name]))  {
152
-			unset($this[$name]);
153
-		}
154
-		if ($shared) {
155
-			$this[$name] = $closure;
156
-		} else {
157
-			$this[$name] = parent::factory($closure);
158
-		}
159
-	}
160
-
161
-	/**
162
-	 * Shortcut for returning a service from a service under a different key,
163
-	 * e.g. to tell the container to return a class when queried for an
164
-	 * interface
165
-	 * @param string $alias the alias that should be registered
166
-	 * @param string $target the target that should be resolved instead
167
-	 */
168
-	public function registerAlias($alias, $target) {
169
-		$this->registerService($alias, function (IContainer $container) use ($target) {
170
-			return $container->query($target);
171
-		}, false);
172
-	}
173
-
174
-	/*
46
+    /**
47
+     * @param ReflectionClass $class the class to instantiate
48
+     * @return \stdClass the created class
49
+     */
50
+    private function buildClass(ReflectionClass $class) {
51
+        $constructor = $class->getConstructor();
52
+        if ($constructor === null) {
53
+            return $class->newInstance();
54
+        } else {
55
+            $parameters = [];
56
+            foreach ($constructor->getParameters() as $parameter) {
57
+                $parameterClass = $parameter->getClass();
58
+
59
+                // try to find out if it is a class or a simple parameter
60
+                if ($parameterClass === null) {
61
+                    $resolveName = $parameter->getName();
62
+                } else {
63
+                    $resolveName = $parameterClass->name;
64
+                }
65
+
66
+                try {
67
+                    $parameters[] = $this->query($resolveName);
68
+                } catch (\Exception $e) {
69
+                    if (class_exists("PHPUnit_Framework_AssertionFailedError", false) &&
70
+                        $e instanceof \PHPUnit_Framework_AssertionFailedError) {
71
+                        // Easier debugging of "Your test case is not allowed to access the database."
72
+                        throw $e;
73
+                    }
74
+
75
+                    // Service not found, use the default value when available
76
+                    if ($parameter->isDefaultValueAvailable()) {
77
+                        $parameters[] = $parameter->getDefaultValue();
78
+                    } else if ($parameterClass !== null) {
79
+                        $resolveName = $parameter->getName();
80
+                        $parameters[] = $this->query($resolveName);
81
+                    } else {
82
+                        throw $e;
83
+                    }
84
+                }
85
+            }
86
+            return $class->newInstanceArgs($parameters);
87
+        }
88
+    }
89
+
90
+
91
+    /**
92
+     * If a parameter is not registered in the container try to instantiate it
93
+     * by using reflection to find out how to build the class
94
+     * @param string $name the class name to resolve
95
+     * @return \stdClass
96
+     * @throws QueryException if the class could not be found or instantiated
97
+     */
98
+    public function resolve($name) {
99
+        $baseMsg = 'Could not resolve ' . $name . '!';
100
+        try {
101
+            $class = new ReflectionClass($name);
102
+            if ($class->isInstantiable()) {
103
+                return $this->buildClass($class);
104
+            } else {
105
+                throw new QueryException($baseMsg .
106
+                    ' Class can not be instantiated');
107
+            }
108
+        } catch(ReflectionException $e) {
109
+            throw new QueryException($baseMsg . ' ' . $e->getMessage());
110
+        }
111
+    }
112
+
113
+
114
+    /**
115
+     * @param string $name name of the service to query for
116
+     * @return mixed registered service for the given $name
117
+     * @throws QueryException if the query could not be resolved
118
+     */
119
+    public function query($name) {
120
+        $name = $this->sanitizeName($name);
121
+        if ($this->offsetExists($name)) {
122
+            return $this->offsetGet($name);
123
+        } else {
124
+            $object = $this->resolve($name);
125
+            $this->registerService($name, function () use ($object) {
126
+                return $object;
127
+            });
128
+            return $object;
129
+        }
130
+    }
131
+
132
+    /**
133
+     * @param string $name
134
+     * @param mixed $value
135
+     */
136
+    public function registerParameter($name, $value) {
137
+        $this[$name] = $value;
138
+    }
139
+
140
+    /**
141
+     * The given closure is call the first time the given service is queried.
142
+     * The closure has to return the instance for the given service.
143
+     * Created instance will be cached in case $shared is true.
144
+     *
145
+     * @param string $name name of the service to register another backend for
146
+     * @param Closure $closure the closure to be called on service creation
147
+     * @param bool $shared
148
+     */
149
+    public function registerService($name, Closure $closure, $shared = true) {
150
+        $name = $this->sanitizeName($name);
151
+        if (isset($this[$name]))  {
152
+            unset($this[$name]);
153
+        }
154
+        if ($shared) {
155
+            $this[$name] = $closure;
156
+        } else {
157
+            $this[$name] = parent::factory($closure);
158
+        }
159
+    }
160
+
161
+    /**
162
+     * Shortcut for returning a service from a service under a different key,
163
+     * e.g. to tell the container to return a class when queried for an
164
+     * interface
165
+     * @param string $alias the alias that should be registered
166
+     * @param string $target the target that should be resolved instead
167
+     */
168
+    public function registerAlias($alias, $target) {
169
+        $this->registerService($alias, function (IContainer $container) use ($target) {
170
+            return $container->query($target);
171
+        }, false);
172
+    }
173
+
174
+    /*
175 175
 	 * @param string $name
176 176
 	 * @return string
177 177
 	 */
178
-	protected function sanitizeName($name) {
179
-		if (isset($name[0]) && $name[0] === '\\') {
180
-			return ltrim($name, '\\');
181
-		}
182
-		return $name;
183
-	}
178
+    protected function sanitizeName($name) {
179
+        if (isset($name[0]) && $name[0] === '\\') {
180
+            return ltrim($name, '\\');
181
+        }
182
+        return $name;
183
+    }
184 184
 
185 185
 }
Please login to merge, or discard this patch.