浏览代码

Merge pull request #5271 from ADmad/3.0-object-registry

3.0 object registry
José Lorenzo Rodríguez 11 年之前
父节点
当前提交
0bd39dd2e4
共有 4 个文件被更改,包括 43 次插入16 次删除
  1. 37 10
      src/Core/ObjectRegistry.php
  2. 1 1
      src/Log/Log.php
  3. 4 4
      src/ORM/BehaviorRegistry.php
  4. 1 1
      src/ORM/Table.php

+ 37 - 10
src/Core/ObjectRegistry.php

@@ -155,25 +155,42 @@ abstract class ObjectRegistry {
 	abstract protected function _create($class, $alias, $config);
 
 /**
- * Get the loaded object list, or check whether or not a given object is loaded.
+ * Get the list of loaded objects.
  *
- * @param null|string $name The object name to get or null.
- * @return array|\Cake\View\Helper Either a list of object names, or a loaded object.
+ * @return array List of object names.
  */
-	public function loaded($name = null) {
-		if (!empty($name)) {
-			return isset($this->_loaded[$name]);
+	public function loaded() {
+		if (func_num_args() > 0) {
+			$class = get_class($this);
+			trigger_error(
+				sprintf(
+					"%s::loaded() doesn't take object name as argument any more. Use %s::has() instead.",
+					$class,
+					$class
+				),
+				E_USER_ERROR
+			);
 		}
 		return array_keys($this->_loaded);
 	}
 
 /**
- * Provide public read access to the loaded objects
+ * Check whether or not a given object is loaded.
  *
- * @param string $name Name of property to read
- * @return mixed
+ * @param string $name The object name to check for.
+ * @return bool True is object is loaded else false.
  */
-	public function __get($name) {
+	public function has($name) {
+		return isset($this->_loaded[$name]);
+	}
+
+/**
+ * Get loaded object instance.
+ *
+ * @param string $name Name of object.
+ * @return object|null Object instance if loaded else null.
+ */
+	public function get($name) {
 		if (isset($this->_loaded[$name])) {
 			return $this->_loaded[$name];
 		}
@@ -181,6 +198,16 @@ abstract class ObjectRegistry {
 	}
 
 /**
+ * Provide public read access to the loaded objects
+ *
+ * @param string $name Name of property to read
+ * @return mixed
+ */
+	public function __get($name) {
+		return $this->get($name);
+	}
+
+/**
  * Provide isset access to _loaded
  *
  * @param string $name Name of object being checked.

+ 1 - 1
src/Log/Log.php

@@ -191,7 +191,7 @@ class Log {
 			if (isset($properties['engine'])) {
 				$properties['className'] = $properties['engine'];
 			}
-			if (!static::$_registry->loaded($name)) {
+			if (!static::$_registry->has($name)) {
 				static::$_registry->load($name, $properties);
 			}
 		}

+ 4 - 4
src/ORM/BehaviorRegistry.php

@@ -134,7 +134,7 @@ class BehaviorRegistry extends ObjectRegistry {
 		$methods = array_change_key_case($instance->implementedMethods());
 
 		foreach ($finders as $finder => $methodName) {
-			if (isset($this->_finderMap[$finder]) && $this->loaded($this->_finderMap[$finder][0])) {
+			if (isset($this->_finderMap[$finder]) && $this->has($this->_finderMap[$finder][0])) {
 				$duplicate = $this->_finderMap[$finder];
 				$error = sprintf(
 					'%s contains duplicate finder "%s" which is already provided by "%s"',
@@ -148,7 +148,7 @@ class BehaviorRegistry extends ObjectRegistry {
 		}
 
 		foreach ($methods as $method => $methodName) {
-			if (isset($this->_methodMap[$method]) && $this->loaded($this->_methodMap[$method][0])) {
+			if (isset($this->_methodMap[$method]) && $this->has($this->_methodMap[$method][0])) {
 				$duplicate = $this->_methodMap[$method];
 				$error = sprintf(
 					'%s contains duplicate method "%s" which is already provided by "%s"',
@@ -202,7 +202,7 @@ class BehaviorRegistry extends ObjectRegistry {
  */
 	public function call($method, array $args = []) {
 		$method = strtolower($method);
-		if ($this->hasMethod($method) && $this->loaded($this->_methodMap[$method][0])) {
+		if ($this->hasMethod($method) && $this->has($this->_methodMap[$method][0])) {
 			list($behavior, $callMethod) = $this->_methodMap[$method];
 			return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args);
 		}
@@ -223,7 +223,7 @@ class BehaviorRegistry extends ObjectRegistry {
 	public function callFinder($type, array $args = []) {
 		$type = strtolower($type);
 
-		if ($this->hasFinder($type) && $this->loaded($this->_finderMap[$type][0])) {
+		if ($this->hasFinder($type) && $this->has($this->_finderMap[$type][0])) {
 			list($behavior, $callMethod) = $this->_finderMap[$type];
 			return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args);
 		}

+ 1 - 1
src/ORM/Table.php

@@ -542,7 +542,7 @@ class Table implements RepositoryInterface, EventListenerInterface {
  * @return array
  */
 	public function hasBehavior($name) {
-		return $this->_behaviors->loaded($name);
+		return $this->_behaviors->has($name);
 	}
 
 /**