Skip to content

Commit

Permalink
Make extending a missing element throw an exception
Browse files Browse the repository at this point in the history
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 ecbe337
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
12 changes: 11 additions & 1 deletion lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -1392,7 +1392,6 @@ public function testExtendLoop() {
$this->View->render('extend_loop');
}


/**
* Test extend() in an element and a view.
*
Expand All @@ -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.
*
Expand Down
@@ -0,0 +1,2 @@
<?php $this->extend('noneexistent_parent_element'); ?>
Element content.
@@ -0,0 +1 @@
<?php echo $this->element('extended_missing_element'); ?>
38 changes: 24 additions & 14 deletions lib/Cake/View/View.php
Expand Up @@ -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.'));
}
Expand Down

0 comments on commit ecbe337

Please sign in to comment.