Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make extending a missing element throw an exception

A layout extending a missing layout throws a missing-layout exception
A view extendinga missing view throws a missing-view exception
Now, an element extending a missing element throws a logic exception

in addition "absolute" paths can be used such that (using elements as an
example)

$this->extend('foo') - extends View/Elements/foo.ctp
$this->extend('/foo') - extends View/foo.ctp

Closes #2504
  • Loading branch information...
commit ecbe337052e27f1c97b2c12561d9f32c92da596d 1 parent 154b001
@AD7six AD7six authored
View
12 lib/Cake/Test/Case/View/ViewTest.php
@@ -1392,7 +1392,6 @@ public function testExtendLoop() {
$this->View->render('extend_loop');
}
-
/**
* Test extend() in an element and a view.
*
@@ -1412,6 +1411,17 @@ public function testExtendElement() {
}
/**
+ * Extending an element which doesn't exist should throw a missing view exception
+ *
+ * @expectedException LogicException
+ * @return void
+ */
+ public function testExtendMissingElement() {
+ $this->View->layout = false;
+ $this->View->render('extend_missing_element');
+ }
+
+/**
* Test that setting arbitrary properties still works.
*
* @return void
View
2  lib/Cake/Test/test_app/View/Elements/extended_missing_element.ctp
@@ -0,0 +1,2 @@
+<?php $this->extend('noneexistent_parent_element'); ?>
+Element content.
View
1  lib/Cake/Test/test_app/View/Posts/extend_missing_element.ctp
@@ -0,0 +1 @@
+<?php echo $this->element('extended_missing_element'); ?>
View
38 lib/Cake/View/View.php
@@ -662,24 +662,34 @@ public function end() {
* @param string $name The view or element to 'extend' the current one with.
* @return void
* @throws LogicException when you extend a view with itself or make extend loops.
+ * @throws LogicException when you extend an element which doesn't exist
*/
public function extend($name) {
- switch ($this->_currentType) {
- case self::TYPE_VIEW:
- $parent = $this->_getViewFileName($name);
- break;
- case self::TYPE_ELEMENT:
- $parent = $this->_getElementFileName($name);
- break;
- case self::TYPE_LAYOUT:
- $parent = $this->_getLayoutFileName($name);
- break;
-
+ if ($name[0] === '/' || $this->_currentType === self::TYPE_VIEW) {
+ $parent = $this->_getViewFileName($name);
+ } else {
+ switch ($this->_currentType) {
+ case self::TYPE_ELEMENT:
+ $parent = $this->_getElementFileName($name);
+ if (!$parent) {
+ list($plugin, $name) = $this->_pluginSplit($name);
+ $paths = $this->_paths($plugin);
+ $defaultPath = $paths[0] . 'Elements' . DS;
+ throw new LogicException(__d(
+ 'cake_dev',
+ 'You cannot extend an element which does not exist (%s).',
+ $defaultPath . $name . $this->ext
+ ));
+ }
+ break;
+ case self::TYPE_LAYOUT:
+ $parent = $this->_getLayoutFileName($name);
+ break;
+ default:
+ $parent = $this->_getViewFileName($name);
+ }
}
- if (!$parent) {
- throw new LogicException(__d('cake_dev', 'The parent %s you specified doesn\'t exist.', $this->_currentType));
- }
if ($parent == $this->_current) {
throw new LogicException(__d('cake_dev', 'You cannot have views extend themselves.'));
}
Please sign in to comment.
Something went wrong with that request. Please try again.