Browse Source

Add MissingViewException.

This exception is thrown when a specified view class cannot be loaded.
ADmad 11 years ago
parent
commit
632167d948

+ 56 - 0
src/Template/Error/missing_view.ctp

@@ -0,0 +1,56 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+use Cake\Core\Plugin;
+use Cake\Core\Configure;
+
+$pluginPath = Configure::read('App.paths.plugins.0');
+
+$pluginDot = empty($plugin) ? null : $plugin . '.';
+if (empty($plugin)) {
+	$filePath = APP_DIR . DS;
+}
+if (!empty($plugin) && Plugin::loaded($plugin)) {
+	$filePath = Plugin::classPath($plugin);
+}
+if (!empty($plugin) && !Plugin::loaded($plugin)) {
+	$filePath = $pluginPath . h($plugin) . DS . 'src' . DS;
+}
+?>
+<h2>Missing View</h2>
+<p class="error">
+	<strong>Error: </strong>
+	<?= sprintf('<em>%s</em> could not be found.', h($pluginDot . $class)); ?>
+	<?php
+		if (!empty($plugin) && !Plugin::loaded($plugin)):
+			echo sprintf('Make sure your plugin <em>%s</em> is in the %s directory and was loaded.', h($plugin), $pluginPath);
+		endif;
+	?>
+</p>
+<p class="error">
+	<strong>Error: </strong>
+	<?= sprintf('Create the class <em>%s</em> below in file: %s', h($class), $filePath . 'View' . DS . h($class) . '.php'); ?>
+</p>
+<pre>
+&lt;?php
+class <?= h($class); ?> extends View {
+
+}
+</pre>
+<p class="notice">
+	<strong>Notice: </strong>
+	<?= sprintf('If you want to customize this error message, create %s', APP_DIR . DS . 'Template' . DS . 'Error' . DS . 'missing_view.ctp'); ?>
+</p>
+
+<?= $this->element('exception_stack_trace'); ?>

+ 27 - 0
src/View/Exception/MissingViewException.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\View\Exception;
+
+use Cake\Core\Exception\Exception;
+
+/**
+ * Used when a view class file cannot be found.
+ *
+ */
+class MissingViewException extends Exception {
+
+	protected $_messageTemplate = 'View class "%s" is missing.';
+
+}

+ 4 - 4
src/View/ViewVarsTrait.php

@@ -36,7 +36,7 @@ trait ViewVarsTrait {
  *
  * @param string $viewClass View class name or null to use $viewClass
  * @return \Cake\View\View
- * @throws \RuntimeException If view class was not found.
+ * @throws \Cake\View\Exception\MissingViewException If view class was not found.
  */
 	public function getView($viewClass = null) {
 		if ($viewClass === null && $this->View) {
@@ -59,7 +59,7 @@ trait ViewVarsTrait {
 		$this->viewClass = $viewClass;
 		$className = App::className($this->viewClass, 'View', 'View');
 		if (!$className) {
-			throw new \RuntimeException(sprintf('View class "%s" was not found.', $viewClass));
+			throw new Exception\MissingViewException([$viewClass]);
 		}
 
 		if ($this->View && $this->View instanceof $className) {
@@ -74,7 +74,7 @@ trait ViewVarsTrait {
  *
  * @param string $viewClass Optional namespaced class name of the View class to instantiate.
  * @return \Cake\View\View
- * @throws \RuntimeException If view class was not found.
+ * @throws \Cake\View\Exception\MissingViewException If view class was not found.
  */
 	public function createView($viewClass = null) {
 		if ($viewClass === null) {
@@ -86,7 +86,7 @@ trait ViewVarsTrait {
 			$className = App::className($viewClass, 'View', 'View');
 		}
 		if (!$className) {
-			throw new \RuntimeException(sprintf('View class "%s" was not found.', $viewClass));
+			throw new Exception\MissingViewException([$viewClass]);
 		}
 		$viewOptions = array_intersect_key(get_object_vars($this), array_flip($this->_validViewOptions));
 		return new $className($this->request, $this->response, $this->eventManager(), $viewOptions);

+ 4 - 4
tests/TestCase/View/ViewVarsTraitTest.php

@@ -139,8 +139,8 @@ class ViewVarsTraitTest extends TestCase {
 /**
  * test getView() throws exception if view class cannot be found
  *
- * @expectedException RuntimeException
- * @expectedExceptionMessage View class "Foo" was not found.
+ * @expectedException Cake\View\Exception\MissingViewException
+ * @expectedExceptionMessage View class "Foo" is missing.
  * @return void
  */
 	public function testGetViewException() {
@@ -150,8 +150,8 @@ class ViewVarsTraitTest extends TestCase {
 /**
  * test createView() throws exception if view class cannot be found
  *
- * @expectedException RuntimeException
- * @expectedExceptionMessage View class "Foo" was not found.
+ * @expectedException Cake\View\Exception\MissingViewException
+ * @expectedExceptionMessage View class "Foo" is missing.
  * @return void
  */
 	public function testCreateViewException() {