From 2f94af35a4c60f9eae70843d8d63b5112474c95d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 23 Mar 2015 15:47:36 -0400
Subject: [PATCH] Smarter way of detecting missing templates. - Replaced
 try..catch technique with more focused resolver test. - Thanks to Ere Maijala
 for suggesting this approach.

---
 .../src/VuFind/View/Helper/Root/Auth.php      |  5 +++--
 .../src/VuFind/View/Helper/Root/HelpText.php  | 19 +++++++++----------
 .../src/VuFind/View/Helper/Root/Recommend.php |  5 +++--
 .../src/VuFind/View/Helper/Root/Record.php    |  5 +++--
 .../src/VuFind/View/Helper/Root/Related.php   |  7 ++++---
 5 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Auth.php b/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
index 4c951e37332..6b10626a641 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
@@ -75,15 +75,16 @@ class Auth extends \Zend\View\Helper\AbstractHelper
         // template.
         $className = $this->getManager()->getAuthClassForTemplateRendering();
         $topClassName = $className; // for error message
+        $resolver = $this->getView()->resolver();
         while (true) {
             // Guess the template name for the current class:
             $template = 'Auth/' . $this->getBriefClass($className) . '/' . $name;
-            try {
+            if ($resolver->resolve($template)) {
                 // Try to render the template....
                 $html = $this->getView()->render($template);
                 $contextHelper($this->getView())->restore($oldContext);
                 return $html;
-            } catch (RuntimeException $e) {
+            } else {
                 // If the template doesn't exist, let's see if we can inherit a
                 // template from a parent class:
                 $className = get_parent_class($className);
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/HelpText.php b/module/VuFind/src/VuFind/View/Helper/Root/HelpText.php
index b2fc213ed4c..6ebc7097eed 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/HelpText.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/HelpText.php
@@ -27,8 +27,6 @@
  */
 namespace VuFind\View\Helper\Root;
 
-use Zend\View\Exception\RuntimeException;
-
 /**
  * "Load help text" view helper
  *
@@ -116,18 +114,19 @@ class HelpText extends \Zend\View\Helper\AbstractHelper
         // Clear warnings
         $this->warnings = [];
 
-        try {
-            $tpl = "HelpTranslations/{$this->language}/{$safe_topic}.phtml";
+        $resolver = $this->getView()->resolver();
+        $tpl = "HelpTranslations/{$this->language}/{$safe_topic}.phtml";
+        if ($resolver->resolve($tpl)) {
             $html = $this->getView()->render($tpl);
-        } catch (RuntimeException $e) {
-            try {
-                // language missing -- try default language
-                $tplFallback = 'HelpTranslations/' . $this->defaultLanguage . '/'
-                    . $safe_topic . '.phtml';
+        } else {
+            // language missing -- try default language
+            $tplFallback = 'HelpTranslations/' . $this->defaultLanguage . '/'
+                . $safe_topic . '.phtml';
+            if ($resolver->resolve($tplFallback)) {
                 $html = $this->getView()->render($tplFallback);
                 $this->warnings[] = 'Sorry, but the help you requested is '
                     . 'unavailable in your language.';
-            } catch (RuntimeException $e) {
+            } else {
                 // no translation available at all!
                 $html = false;
             }
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Recommend.php b/module/VuFind/src/VuFind/View/Helper/Root/Recommend.php
index 333452d5c6b..6e3f62870df 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Recommend.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Recommend.php
@@ -59,16 +59,17 @@ class Recommend extends AbstractHelper
         // in case we need to use a parent class' name to find the appropriate
         // template.
         $className = get_class($recommend);
+        $resolver = $this->getView()->resolver();
         while (true) {
             // Guess the template name for the current class:
             $classParts = explode('\\', $className);
             $template = 'Recommend/' . array_pop($classParts) . '.phtml';
-            try {
+            if ($resolver->resolve($template)) {
                 // Try to render the template....
                 $html = $this->getView()->render($template);
                 $contextHelper($this->getView())->restore($oldContext);
                 return $html;
-            } catch (RuntimeException $e) {
+            } else {
                 // If the template doesn't exist, let's see if we can inherit a
                 // template from a parent recommendation class:
                 $className = get_parent_class($className);
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Record.php b/module/VuFind/src/VuFind/View/Helper/Root/Record.php
index bc6edf40660..9a590b187bb 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Record.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Record.php
@@ -94,16 +94,17 @@ class Record extends AbstractHelper
         // in case we need to use a parent class' name to find the appropriate
         // template.
         $className = get_class($this->driver);
+        $resolver = $this->view->resolver();
         while (true) {
             // Guess the template name for the current class:
             $classParts = explode('\\', $className);
             $template = 'RecordDriver/' . array_pop($classParts) . '/' . $name;
-            try {
+            if ($resolver->resolve($template)) {
                 // Try to render the template....
                 $html = $this->view->render($template);
                 $this->contextHelper->restore($oldContext);
                 return $html;
-            } catch (RuntimeException $e) {
+            } else {
                 // If the template doesn't exist, let's see if we can inherit a
                 // template from a parent class:
                 $className = get_parent_class($className);
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Related.php b/module/VuFind/src/VuFind/View/Helper/Root/Related.php
index 07a1b6b8c2e..afaf9c3cb39 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Related.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Related.php
@@ -89,16 +89,17 @@ class Related extends AbstractHelper
         // in case we need to use a parent class' name to find the appropriate
         // template.
         $className = get_class($related);
+        $resolver = $this->getView()->resolver();
         while (true) {
             // Guess the template name for the current class:
             $classParts = explode('\\', $className);
             $template = 'Related/' . array_pop($classParts) . '.phtml';
-            try {
-                // Try to render the template....
+            // Try to resolve the template....
+            if ($resolver->resolve($template)) {
                 $html = $this->getView()->render($template);
                 $contextHelper($this->getView())->restore($oldContext);
                 return $html;
-            } catch (RuntimeException $e) {
+            } else {
                 // If the template doesn't exist, let's see if we can inherit a
                 // template from a parent class:
                 $className = get_parent_class($className);
-- 
GitLab