From b9b61426d2cb0d8a5e3ff92463afcf526e78b59f Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 30 Jan 2019 15:12:16 +0200
Subject: [PATCH] Make sure not to concatenate non-existent files. (#1308)

- Logs an error message when missing files are encountered.
---
 .../VuFindTheme/View/Helper/ConcatTrait.php   | 21 +++++++++++++++----
 .../src/VuFindTheme/View/Helper/HeadLink.php  |  2 ++
 .../VuFindTheme/View/Helper/HeadScript.php    |  2 ++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
index 72ef3705df4..80cefc32938 100644
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
@@ -145,10 +145,10 @@ trait ConcatTrait
 
     /**
      * Initialize class properties related to concatenation of resources.
-     * All of the elements to be concatenated into ($this->concatItems)
-     * and those that need to remain on their own ($this->otherItems).
+     * All of the elements to be concatenated into groups and
+     * and those that need to remain on their own special group 'other'.
      *
-     * @return void
+     * @return bool True if there are items
      */
     protected function filterItems()
     {
@@ -167,10 +167,23 @@ trait ConcatTrait
                 continue;
             }
 
+            $path = $this->getFileType() . '/' . $this->getResourceFilePath($item);
             $details = $this->themeInfo->findContainingTheme(
-                $this->getFileType() . '/' . $this->getResourceFilePath($item),
+                $path,
                 ThemeInfo::RETURN_ALL_DETAILS
             );
+            // Deal with special case: $path was not found in any theme.
+            if (null === $details) {
+                $errorMsg = "Could not find file '$path' in theme files";
+                method_exists($this, 'logError')
+                    ? $this->logError($errorMsg) : error_log($errorMsg);
+                $this->groups[] = [
+                    'other' => true,
+                    'item' => $item
+                ];
+                $groupTypes[] = 'other';
+                continue;
+            }
 
             $type = $this->getType($item);
             $index = array_search($type, $groupTypes);
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
index e11cfa4d0f0..949ee27d36d 100644
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
@@ -39,8 +39,10 @@ use VuFindTheme\ThemeInfo;
  * @link     https://vufind.org/wiki/development Wiki
  */
 class HeadLink extends \Zend\View\Helper\HeadLink
+    implements \Zend\Log\LoggerAwareInterface
 {
     use ConcatTrait;
+    use \VuFind\Log\LoggerAwareTrait;
 
     /**
      * Theme information service
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadScript.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadScript.php
index d2328c77956..0ce0bb835d6 100644
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadScript.php
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadScript.php
@@ -39,10 +39,12 @@ use VuFindTheme\ThemeInfo;
  * @link     https://vufind.org/wiki/development Wiki
  */
 class HeadScript extends \Zend\View\Helper\HeadScript
+    implements \Zend\Log\LoggerAwareInterface
 {
     use ConcatTrait {
         getMinifiedData as getBaseMinifiedData;
     }
+    use \VuFind\Log\LoggerAwareTrait;
 
     /**
      * Theme information service
-- 
GitLab