Browse Source

Making lazy loader throw an exception for missing helpers

Jose Lorenzo Rodriguez 14 years ago
parent
commit
18b843467f
2 changed files with 23 additions and 19 deletions
  1. 10 0
      lib/Cake/Test/Case/View/HelperCollectionTest.php
  2. 13 19
      lib/Cake/View/HelperCollection.php

+ 10 - 0
lib/Cake/Test/Case/View/HelperCollectionTest.php

@@ -87,6 +87,16 @@ class HelperCollectionTest extends CakeTestCase {
 	}
 
 /**
+ * test lazy loading of helpers
+ *
+ * @expectedException MissingHelperException
+ * @return void
+ */
+	public function testLazyLoadException() {
+		$result = $this->Helpers->NotAHelper;
+	}
+
+/**
  * Tests loading as an alias
  *
  * @return void

+ 13 - 19
lib/Cake/View/HelperCollection.php

@@ -55,9 +55,20 @@ class HelperCollection extends ObjectCollection implements CakeEventListener {
 		if (parent::__isset($helper)) {
 			return true;
 		}
-		if (!$this->_loadSandbox($helper)) {
-			return $this->_View->plugin && $this->_loadSandbox($this->_View->plugin . '.' . $helper);
+
+		try {
+			$this->load($helper);
+		} catch (MissingHelperException $exception) {
+			if ($this->_View->plugin) {
+				$this->load($this->_View->plugin . '.' . $helper);
+				return true;
+			}
+		}
+
+		if (!empty($exception)) {
+			throw $exception;
 		}
+
 		return true;
 	}
 
@@ -78,23 +89,6 @@ class HelperCollection extends ObjectCollection implements CakeEventListener {
 	}
 
 /**
- * Auxiliary function used for lazy loading helpers
- * catches any MissingHelperException and converts it into
- * a boolean return
- *
- * @param string $helper The helper name to be loaded
- * @return boolean wheter the helper could be loaded or not
- **/
-	protected function _loadSandbox($helper) {
-		try {
-			$this->load($helper);
-		} catch (MissingHelperException $e) {
-			return false;
-		}
-		return true;
-	}
-
-/**
  * Loads/constructs a helper.  Will return the instance in the registry if it already exists.
  * By setting `$enable` to false you can disable callbacks for a helper.  Alternatively you
  * can set `$settings['enabled'] = false` to disable callbacks.  This alias is provided so that when