Browse Source

make use of the instance config trait

AD7six 12 years ago
parent
commit
b81ebffeb4

+ 5 - 57
src/Controller/Component.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Controller;
 
+use Cake\Core\InstanceConfigTrait;
 use Cake\Core\Object;
 use Cake\Event\Event;
 use Cake\Event\EventListener;
@@ -64,6 +65,8 @@ use Cake\Utility\Hash;
  */
 class Component extends Object implements EventListener {
 
+	use InstanceConfigTrait;
+
 /**
  * Component registry class used to lazy load components.
  *
@@ -79,13 +82,6 @@ class Component extends Object implements EventListener {
 	public $components = array();
 
 /**
- * Runtime config for this Component
- *
- * @var array
- */
-	protected $_config = [];
-
-/**
  * Default config
  *
  * These are merged with user-provided config when the component is used.
@@ -110,9 +106,9 @@ class Component extends Object implements EventListener {
 	public function __construct(ComponentRegistry $registry, $config = []) {
 		$this->_registry = $registry;
 
-		$this->_config = array_merge($this->_defaultConfig, $config);
+		$this->config($config);
 
-		$this->_set($this->_config); //@TODO get rid of public properties and remove this
+		$this->_set($this->config()); //@TODO get rid of public properties and remove this
 
 		if (!empty($this->components)) {
 			$this->_componentMap = $registry->normalizeArray($this->components);
@@ -136,54 +132,6 @@ class Component extends Object implements EventListener {
 	}
 
 /**
- * Component config getter and setter
- *
- * Usage:
- * {{{
- * $instance->config(); will return full config
- * $instance->config('foo'); will return configured foo
- * $instance->config('notset'); will return null
- * $instance->config('foo', $x); will set ['foo' $x] to the existing config
- * $instance->config('foo.bar', $x); will set/add ['foo' => ['bar' => $x]] to the existing config
- * }}}
- *
- * @param string|null $key to return
- * @param mixed $val value to set
- * @return mixed array or config value
- */
-	public function config($key = null, $val = null) {
-		if ($key === null) {
-			return $this->_config;
-		}
-
-		if ($val !== null) {
-			return $this->_configSet([$key => $val]);
-		} elseif (is_array($key)) {
-			return $this->_configSet($key);
-		}
-
-		return array_key_exists($key, $this->_config) ? $this->_config[$key] : null;
-	}
-
-/**
- * Update config with passed argument
- *
- * Overriden in subclasses if the component config shouldn't be modified at runtime
- *
- * @param array $config
- * @return void
- */
-	protected function _configSet($config) {
-		foreach ($config as $key => $val) {
-			if (strpos($key, '.')) {
-				$this->_config = Hash::insert($this->_config, $key, $val);
-			} else {
-				$this->_config[$key] = $val;
-			}
-		}
-	}
-
-/**
  * Get the Controller callbacks this Component is interested in.
  *
  * Uses Conventions to map controller events to standard component

+ 4 - 28
src/Controller/Component/Auth/AbstractPasswordHasher.php

@@ -16,18 +16,15 @@
  */
 namespace Cake\Controller\Component\Auth;
 
+use Cake\Core\InstanceConfigTrait;
+
 /**
  * Abstract password hashing class
  *
  */
 abstract class AbstractPasswordHasher {
 
-/**
- * Runtime config for this object
- *
- * @var array
- */
-	protected $_config = array();
+	use InstanceConfigTrait;
 
 /**
  * Default config
@@ -44,28 +41,7 @@ abstract class AbstractPasswordHasher {
  * @param array $config Array of config.
  */
 	public function __construct($config = array()) {
-		$this->_config = array_merge($this->_defaultConfig, $config);
-	}
-
-/**
- * config getter and setter
- *
- * Usage:
- * {{{
- * $instance->config(); will return full config
- * $instance->config('foo'); will return configured foo
- * $instance->config('notset'); will return null
- * }}}
- *
- * @param string|null $key to return
- * @return mixed array or config value
- */
-	public function config($key = null) {
-		if ($key === null) {
-			return $this->_config;
-		}
-
-		return array_key_exists($key, $this->_config) ? $this->_config[$key] : null;
+		$this->config($config);
 	}
 
 /**

+ 3 - 51
src/Controller/Component/Auth/BaseAuthenticate.php

@@ -16,6 +16,7 @@ namespace Cake\Controller\Component\Auth;
 use Cake\Controller\ComponentRegistry;
 use Cake\Controller\Component\Auth\AbstractPasswordHasher;
 use Cake\Core\App;
+use Cake\Core\InstanceConfigTrait;
 use Cake\Error;
 use Cake\Network\Request;
 use Cake\Network\Response;
@@ -29,12 +30,7 @@ use Cake\Utility\Security;
  */
 abstract class BaseAuthenticate {
 
-/**
- * Runtime config for this object
- *
- * @var array
- */
-	protected $_config = [];
+	use InstanceConfigTrait;
 
 /**
  * Default config for this object.
@@ -83,7 +79,7 @@ abstract class BaseAuthenticate {
  */
 	public function __construct(ComponentRegistry $registry, $config) {
 		$this->_registry = $registry;
-		$this->_config = Hash::merge($this->_defaultConfig, $config);
+		$this->config($config);
 	}
 
 /**
@@ -136,50 +132,6 @@ abstract class BaseAuthenticate {
 	}
 
 /**
- * config getter and setter
- *
- * Usage:
- * {{{
- * $instance->config(); will return full config
- * $instance->config('foo'); will return configured foo
- * $instance->config('notset'); will return null
- * }}}
- *
- * @param string|null $key to return
- * @param mixed $val value to set
- * @return mixed array or config value
- */
-	public function config($key = null, $val = null) {
-		if ($key === null) {
-			return $this->_config;
-		}
-
-		if ($val !== null) {
-			return $this->_configSet([$key => $val]);
-		} elseif (is_array($key)) {
-			return $this->_configSet($key);
-		}
-
-		return Hash::get($this->_config, $key);
-	}
-
-/**
- * Update config with passed argument
- *
- * @param array $config
- * @return void
- */
-	protected function _configSet($config) {
-		foreach ($config as $key => $val) {
-			if (strpos($key, '.')) {
-				$this->_config = Hash::insert($this->_config, $key, $val);
-			} else {
-				$this->_config[$key] = $val;
-			}
-		}
-	}
-
-/**
  * Return password hasher object
  *
  * @return AbstractPasswordHasher Password hasher instance

+ 4 - 52
src/Controller/Component/Auth/BaseAuthorize.php

@@ -18,6 +18,7 @@ namespace Cake\Controller\Component\Auth;
 
 use Cake\Controller\ComponentRegistry;
 use Cake\Controller\Controller;
+use Cake\Core\InstanceConfigTrait;
 use Cake\Error;
 use Cake\Network\Request;
 use Cake\Network\Response;
@@ -31,6 +32,8 @@ use Cake\Utility\Inflector;
  */
 abstract class BaseAuthorize {
 
+	use InstanceConfigTrait;
+
 /**
  * Controller for the request.
  *
@@ -46,13 +49,6 @@ abstract class BaseAuthorize {
 	protected $_registry;
 
 /**
- * Runtime config for this object
- *
- * @var array
- */
-	protected $_config = [];
-
-/**
  * Default config for authorize objects.
  *
  * - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
@@ -85,7 +81,7 @@ abstract class BaseAuthorize {
 		$this->_registry = $registry;
 		$controller = $registry->getController();
 		$this->controller($controller);
-		$this->_config = Hash::merge($this->_defaultConfig, $config);
+		$this->config($config);
 	}
 
 /**
@@ -98,50 +94,6 @@ abstract class BaseAuthorize {
 	abstract public function authorize($user, Request $request);
 
 /**
- * config getter and setter
- *
- * Usage:
- * {{{
- * $instance->config(); will return full config
- * $instance->config('foo'); will return configured foo
- * $instance->config('notset'); will return null
- * }}}
- *
- * @param string|null $key to return
- * @param mixed $val value to set
- * @return mixed array or config value
- */
-	public function config($key = null, $val = null) {
-		if ($key === null) {
-			return $this->_config;
-		}
-
-		if ($val !== null) {
-			return $this->_configSet([$key => $val]);
-		} elseif (is_array($key)) {
-			return $this->_configSet($key);
-		}
-
-		return Hash::get($this->_config, $key);
-	}
-
-/**
- * Update config with passed argument
- *
- * @param array $config
- * @return void
- */
-	protected function _configSet($config) {
-		foreach ($config as $key => $val) {
-			if (strpos($key, '.')) {
-				$this->_config = Hash::insert($this->_config, $key, $val);
-			} else {
-				$this->_config[$key] = $val;
-			}
-		}
-	}
-
-/**
  * Accessor to the controller object.
  *
  * @param Controller $controller null to get, a controller to set.

+ 5 - 3
src/Controller/Component/Auth/CrudAuthorize.php

@@ -82,7 +82,9 @@ class CrudAuthorize extends BaseAuthorize {
  * @return boolean
  */
 	public function authorize($user, Request $request) {
-		if (!isset($this->_config['actionMap'][$request->params['action']])) {
+		$mapped = $this->config('actionMap.' . $request->params['action']);
+
+		if (!$mapped) {
 			trigger_error(sprintf(
 				'CrudAuthorize::authorize() - Attempted access of un-mapped action "%1$s" in controller "%2$s"',
 				$request->action,
@@ -92,12 +94,12 @@ class CrudAuthorize extends BaseAuthorize {
 			);
 			return false;
 		}
-		$user = array($this->_config['userModel'] => $user);
+		$user = array($this->config('userModel') => $user);
 		$Acl = $this->_registry->load('Acl');
 		return $Acl->check(
 			$user,
 			$this->action($request, ':controller'),
-			$this->_config['actionMap'][$request->params['action']]
+			$mapped
 		);
 	}
 

+ 1 - 1
src/Controller/Component/Auth/DigestAuthenticate.php

@@ -110,7 +110,7 @@ class DigestAuthenticate extends BasicAuthenticate {
 			return false;
 		}
 
-		$field = $this->_config['fields']['password'];
+		$field = $this->config('fields.password');
 		$password = $user[$field];
 		unset($user[$field]);
 

+ 5 - 3
src/Controller/Component/Auth/SimplePasswordHasher.php

@@ -26,11 +26,13 @@ use Cake\Utility\Security;
 class SimplePasswordHasher extends AbstractPasswordHasher {
 
 /**
- * Config for this object.
+ * Default config for this object.
  *
  * @var array
  */
-	protected $_config = array('hashType' => null);
+	protected $_defaultConfig = [
+		'hashType' => null
+	];
 
 /**
  * Generates password hash.
@@ -40,7 +42,7 @@ class SimplePasswordHasher extends AbstractPasswordHasher {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#hashing-passwords
  */
 	public function hash($password) {
-		return Security::hash($password, $this->_config['hashType'], true);
+		return Security::hash($password, $this->config('hashType'), true);
 	}
 
 /**