Skip to content
Permalink
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...
AD7six committed Jan 25, 2012
1 parent 154b001 commit ecbe337052e27f1c97b2c12561d9f32c92da596d
@@ -1392,7 +1392,6 @@ public function testExtendLoop() {
$this->View->render('extend_loop');
}
/**
* Test extend() in an element and a view.
*
@@ -1411,6 +1410,17 @@ public function testExtendElement() {
$this->assertEquals($expected, $content);
}
/**
* 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.
*
@@ -0,0 +1,2 @@
<?php $this->extend('noneexistent_parent_element'); ?>
Element content.
@@ -0,0 +1 @@
<?php echo $this->element('extended_missing_element'); ?>
@@ -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.'));
}

0 comments on commit ecbe337

Please sign in to comment.
You can’t perform that action at this time.