Browse Source

Reduce calls to config().

ADmad 12 years ago
parent
commit
e3fa02b6d2
2 changed files with 50 additions and 56 deletions
  1. 24 30
      src/Controller/Component/AuthComponent.php
  2. 26 26
      src/Controller/Component/CookieComponent.php

+ 24 - 30
src/Controller/Component/AuthComponent.php

@@ -261,9 +261,8 @@ class AuthComponent extends Component {
 			return $this->_unauthenticated($controller);
 		}
 
-		$authorize = $this->config('authorize');
 		if ($this->_isLoginAction($controller) ||
-			empty($authorize) ||
+			empty($this->_config['authorize']) ||
 			$this->isAuthorized($this->user())
 		) {
 			return true;
@@ -318,17 +317,16 @@ class AuthComponent extends Component {
 		}
 
 		if (!$controller->request->is('ajax')) {
-			$this->flash($this->config('authError'));
+			$this->flash($this->_config['authError']);
 			$this->Session->write('Auth.redirect', $controller->request->here(false));
-			$controller->redirect($this->config('loginAction'));
+			$controller->redirect($this->_config['loginAction']);
 			return false;
 		}
 
-		$ajaxLogin = $this->config('ajaxLogin');
-		if (!empty($ajaxLogin)) {
+		if (!empty($this->_config['ajaxLogin'])) {
 			$controller->response->statusCode(403);
 			$controller->viewPath = 'Element';
-			echo $controller->render($ajaxLogin, $this->RequestHandler->ajaxLayout);
+			echo $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
 			$this->_stop();
 			return false;
 		}
@@ -348,7 +346,7 @@ class AuthComponent extends Component {
 			$url = $controller->request->url;
 		}
 		$url = Router::normalize($url);
-		$loginAction = Router::normalize($this->config('loginAction'));
+		$loginAction = Router::normalize($this->_config['loginAction']);
 
 		return $loginAction === $url;
 	}
@@ -361,21 +359,19 @@ class AuthComponent extends Component {
  * @throws \Cake\Error\ForbiddenException
  */
 	protected function _unauthorized(Controller $controller) {
-		$unauthorizedRedirect = $this->config('unauthorizedRedirect');
-		if ($unauthorizedRedirect === false) {
-			throw new Error\ForbiddenException($this->config('authError'));
+		if ($this->_config['unauthorizedRedirect'] === false) {
+			throw new Error\ForbiddenException($this->_config['authError']);
 		}
 
-		$this->flash($this->config('authError'));
-		if ($unauthorizedRedirect === true) {
+		$this->flash($this->_config['authError']);
+		if ($this->_config['unauthorizedRedirect'] === true) {
 			$default = '/';
-			$loginRedirect = $this->config('loginRedirect');
-			if (!empty($loginRedirect)) {
-				$default = $loginRedirect;
+			if (!empty($this->_config['loginRedirect'])) {
+				$default = $this->_config['loginRedirect'];
 			}
 			$url = $controller->referer($default, true);
 		} else {
-			$url = $unauthorizedRedirect;
+			$url = $this->_config['unauthorizedRedirect'];
 		}
 		$controller->redirect($url, null, true);
 		return false;
@@ -399,7 +395,7 @@ class AuthComponent extends Component {
 				'action' => 'login',
 				'plugin' => null
 			],
-			'logoutRedirect' => $this->config('loginAction'),
+			'logoutRedirect' => $this->_config['loginAction'],
 			'authError' => __d('cake', 'You are not authorized to access that location.')
 		];
 
@@ -451,12 +447,11 @@ class AuthComponent extends Component {
  * @throws \Cake\Error\Exception
  */
 	public function constructAuthorize() {
-		$authorize = $this->config('authorize');
-		if (empty($authorize)) {
+		if (empty($this->_config['authorize'])) {
 			return;
 		}
 		$this->_authorizeObjects = array();
-		$authorize = Hash::normalize((array)$authorize);
+		$authorize = Hash::normalize((array)$this->_config['authorize']);
 		$global = array();
 		if (isset($authorize[AuthComponent::ALL])) {
 			$global = $authorize[AuthComponent::ALL];
@@ -604,7 +599,7 @@ class AuthComponent extends Component {
 		$this->Session->delete(static::$sessionKey);
 		$this->Session->delete('Auth.redirect');
 		$this->Session->renew();
-		return Router::normalize($this->config('logoutRedirect'));
+		return Router::normalize($this->_config['logoutRedirect']);
 	}
 
 /**
@@ -685,11 +680,11 @@ class AuthComponent extends Component {
 			$redir = $this->Session->read('Auth.redirect');
 			$this->Session->delete('Auth.redirect');
 
-			if (Router::normalize($redir) == Router::normalize($this->config('loginAction'))) {
-				$redir = $this->config('loginRedirect');
+			if (Router::normalize($redir) == Router::normalize($this->_config['loginAction'])) {
+				$redir = $this->_config['loginRedirect'];
 			}
-		} elseif ($this->config('loginRedirect')) {
-			$redir = $this->config('loginRedirect');
+		} elseif ($this->_config['loginRedirect']) {
+			$redir = $this->_config['loginRedirect'];
 		} else {
 			$redir = '/';
 		}
@@ -727,12 +722,11 @@ class AuthComponent extends Component {
  * @throws \Cake\Error\Exception
  */
 	public function constructAuthenticate() {
-		$authenticate = $this->config('authenticate');
-		if (empty($authenticate)) {
+		if (empty($this->_config['authenticate'])) {
 			return;
 		}
 		$this->_authenticateObjects = array();
-		$authenticate = Hash::normalize((array)$authenticate);
+		$authenticate = Hash::normalize((array)$this->_config['authenticate']);
 		$global = array();
 		if (isset($authenticate[AuthComponent::ALL])) {
 			$global = $authenticate[AuthComponent::ALL];
@@ -762,7 +756,7 @@ class AuthComponent extends Component {
 		if ($message === false) {
 			return;
 		}
-		$flashConfig = $this->config('flash');
+		$flashConfig = $this->_config['flash'];
 		$this->Session->setFlash(
 			$message,
 			$flashConfig['element'],

+ 26 - 26
src/Controller/Component/CookieComponent.php

@@ -125,12 +125,12 @@ class CookieComponent extends Component {
 	public function __construct(ComponentRegistry $collection, $config = array()) {
 		parent::__construct($collection, $config);
 
-		if ($this->config('key')) {
+		if (!$this->_config['key']) {
 			$this->config('key', Configure::read('Security.salt'));
 		}
 
-		if ($this->config('time')) {
-			$this->_expire($this->config('time'));
+		if ($this->_config['time']) {
+			$this->_expire($this->_config['time']);
 		}
 
 		$controller = $collection->getController();
@@ -154,9 +154,9 @@ class CookieComponent extends Component {
  * @return void
  */
 	public function startup(Event $event) {
-		$this->_expire($this->config('time'));
+		$this->_expire($this->_config['time']);
 
-		$this->_values[$this->config('name')] = array();
+		$this->_values[$this->_config['name']] = array();
 	}
 
 /**
@@ -232,30 +232,30 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
  */
 	public function read($key = null) {
-		$name = $this->config('name');
-		$values = $this->_request->cookie($name);
-		if (empty($this->_values[$name]) && $values) {
-			$this->_values[$name] = $this->_decrypt($values);
+		$cookieName = $this->config('name');
+		$values = $this->_request->cookie($cookieName);
+		if (empty($this->_values[$cookieName]) && $values) {
+			$this->_values[$cookieName] = $this->_decrypt($values);
 		}
-		if (empty($this->_values[$name])) {
-			$this->_values[$name] = array();
+		if (empty($this->_values[$cookieName])) {
+			$this->_values[$cookieName] = array();
 		}
 		if ($key === null) {
-			return $this->_values[$name];
+			return $this->_values[$cookieName];
 		}
 
 		if (strpos($key, '.') !== false) {
 			$names = explode('.', $key, 2);
 			$key = $names[0];
 		}
-		if (!isset($this->_values[$name][$key])) {
+		if (!isset($this->_values[$cookieName][$key])) {
 			return null;
 		}
 
 		if (!empty($names[1])) {
-			return Hash::get($this->_values[$name][$key], $names[1]);
+			return Hash::get($this->_values[$cookieName][$key], $names[1]);
 		}
-		return $this->_values[$name][$key];
+		return $this->_values[$cookieName][$key];
 	}
 
 /**
@@ -291,23 +291,23 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
  */
 	public function delete($key) {
-		$name = $this->config('name');
-		if (empty($this->_values[$name])) {
+		$cookieName = $this->config('name');
+		if (empty($this->_values[$cookieName])) {
 			$this->read();
 		}
 		if (strpos($key, '.') === false) {
-			if (isset($this->_values[$name][$key]) && is_array($this->_values[$name][$key])) {
-				foreach ($this->_values[$name][$key] as $idx => $val) {
+			if (isset($this->_values[$cookieName][$key]) && is_array($this->_values[$cookieName][$key])) {
+				foreach ($this->_values[$cookieName][$key] as $idx => $val) {
 					$this->_delete("[$key][$idx]");
 				}
 			}
 			$this->_delete("[$key]");
-			unset($this->_values[$name][$key]);
+			unset($this->_values[$cookieName][$key]);
 			return;
 		}
 		$names = explode('.', $key, 2);
-		if (isset($this->_values[$name][$names[0]])) {
-			$this->_values[$name][$names[0]] = Hash::remove($this->_values[$name][$names[0]], $names[1]);
+		if (isset($this->_values[$cookieName][$names[0]])) {
+			$this->_values[$cookieName][$names[0]] = Hash::remove($this->_values[$cookieName][$names[0]], $names[1]);
 		}
 		$this->_delete('[' . implode('][', $names) . ']');
 	}
@@ -448,10 +448,10 @@ class CookieComponent extends Component {
 		}
 		$prefix = "Q2FrZQ==.";
 		if ($this->_type === 'rijndael') {
-			$cipher = Security::rijndael($value, $this->config('key'), 'encrypt');
+			$cipher = Security::rijndael($value, $this->_config['key'], 'encrypt');
 		}
 		if ($this->_type === 'aes') {
-			$cipher = Security::encrypt($value, $this->config('key'));
+			$cipher = Security::encrypt($value, $this->_config['key']);
 		}
 		return $prefix . base64_encode($cipher);
 	}
@@ -492,10 +492,10 @@ class CookieComponent extends Component {
 		}
 		$value = base64_decode(substr($value, strlen($prefix)));
 		if ($this->_type === 'rijndael') {
-			$plain = Security::rijndael($value, $this->config('key'), 'decrypt');
+			$plain = Security::rijndael($value, $this->_config['key'], 'decrypt');
 		}
 		if ($this->_type === 'aes') {
-			$plain = Security::decrypt($value, $this->config('key'));
+			$plain = Security::decrypt($value, $this->_config['key']);
 		}
 		return $this->_explode($plain);
 	}