mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
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
This commit is contained in:
parent
154b001552
commit
ecbe337052
4 changed files with 38 additions and 15 deletions
|
@ -1392,7 +1392,6 @@ TEXT;
|
|||
$this->View->render('extend_loop');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test extend() in an element and a view.
|
||||
*
|
||||
|
@ -1411,6 +1410,17 @@ TEXT;
|
|||
$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 @@ class View extends Object {
|
|||
* @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.'));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue