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