From a2b8a67dde897c244d05b4761a8c8e1f31c26be5 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Fri, 2 Mar 2018 15:34:10 +0200
Subject: [PATCH] Allow more services to function in Console mode (#1130)

- Make "Page not found", theme init and cookie manager with limit_by_path setting work.
---
 module/VuFind/src/VuFind/Bootstrapper.php     | 25 +++++++++++--------
 .../VuFind/Cookie/CookieManagerFactory.php    |  4 ++-
 .../src/VuFindTheme/Initializer.php           |  9 ++++---
 3 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index 1c6945aac8c..dfb5f08478f 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -179,17 +179,20 @@ class Bootstrapper
     {
         $callback = function ($event) {
             $serviceManager = $event->getApplication()->getServiceManager();
-            $viewModel = $serviceManager->get('ViewManager')->getViewModel();
-
-            // Grab the template name from the first child -- we can use this to
-            // figure out the current template context.
-            $children = $viewModel->getChildren();
-            if (!empty($children)) {
-                $parts = explode('/', $children[0]->getTemplate());
-                $viewModel->setVariable('templateDir', $parts[0]);
-                $viewModel->setVariable(
-                    'templateName', isset($parts[1]) ? $parts[1] : null
-                );
+            if (!Console::isConsole()) {
+                $viewModel = $serviceManager->get('ViewManager')->getViewModel();
+
+                // Grab the template name from the first child -- we can use this to
+                // figure out the current template context.
+                $children = $viewModel->getChildren();
+                if (!empty($children)) {
+                    $parts = explode('/', $children[0]->getTemplate());
+                    $viewModel->setVariable('templateDir', $parts[0]);
+                    $viewModel->setVariable(
+                        'templateName',
+                        isset($parts[1]) ? $parts[1] : null
+                    );
+                }
             }
         };
         $this->events->attach('dispatch', $callback);
diff --git a/module/VuFind/src/VuFind/Cookie/CookieManagerFactory.php b/module/VuFind/src/VuFind/Cookie/CookieManagerFactory.php
index fc5223b5890..667f9a408c9 100644
--- a/module/VuFind/src/VuFind/Cookie/CookieManagerFactory.php
+++ b/module/VuFind/src/VuFind/Cookie/CookieManagerFactory.php
@@ -28,6 +28,7 @@
 namespace VuFind\Cookie;
 
 use Interop\Container\ContainerInterface;
+use Zend\Console\Console;
 use Zend\ServiceManager\Factory\FactoryInterface;
 
 /**
@@ -66,7 +67,8 @@ class CookieManagerFactory implements FactoryInterface
         if (isset($config->Cookies->limit_by_path)
             && $config->Cookies->limit_by_path
         ) {
-            $path = $container->get('Request')->getBasePath();
+            $path = Console::isConsole()
+                ? '' : $container->get('Request')->getBasePath();
             if (empty($path)) {
                 $path = '/';
             }
diff --git a/module/VuFindTheme/src/VuFindTheme/Initializer.php b/module/VuFindTheme/src/VuFindTheme/Initializer.php
index 51f87b8696a..5ee1a14dd23 100644
--- a/module/VuFindTheme/src/VuFindTheme/Initializer.php
+++ b/module/VuFindTheme/src/VuFindTheme/Initializer.php
@@ -28,6 +28,7 @@
 namespace VuFindTheme;
 
 use Zend\Config\Config;
+use Zend\Console\Console;
 use Zend\Mvc\MvcEvent;
 use Zend\Mvc\View\Http\InjectTemplateListener as BaseInjectTemplateListener;
 use Zend\Stdlib\RequestInterface as Request;
@@ -269,10 +270,12 @@ class Initializer
     protected function sendThemeOptionsToView()
     {
         // Get access to the view model:
-        $viewModel = $this->serviceManager->get('ViewManager')->getViewModel();
+        if (!Console::isConsole()) {
+            $viewModel = $this->serviceManager->get('ViewManager')->getViewModel();
 
-        // Send down the view options:
-        $viewModel->setVariable('themeOptions', $this->getThemeOptions());
+            // Send down the view options:
+            $viewModel->setVariable('themeOptions', $this->getThemeOptions());
+        }
     }
 
     /**
-- 
GitLab