Skip to content
Snippets Groups Projects
Commit 56cc4458 authored by Demian Katz's avatar Demian Katz Committed by Robert Lange
Browse files

Do not require MVC event to initialize theme. (#1600)

- Allows console commands to utilize themes without Laminas-MVC.
parent d062ad50
Branches
Tags
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
*/ */
namespace VuFindTheme; namespace VuFindTheme;
use Interop\Container\ContainerInterface;
use Laminas\Config\Config; use Laminas\Config\Config;
use Laminas\Console\Console; use Laminas\Console\Console;
use Laminas\Mvc\MvcEvent; use Laminas\Mvc\MvcEvent;
...@@ -96,7 +97,8 @@ class Initializer ...@@ -96,7 +97,8 @@ class Initializer
/** /**
* Constructor * Constructor
* *
* @param Config $config Configuration object containing these keys: * @param Config $config Configuration object
* containing these keys:
* <ul> * <ul>
* <li>theme - the name of the default theme for non-mobile devices</li> * <li>theme - the name of the default theme for non-mobile devices</li>
* <li>mobile_theme - the name of the default theme for mobile devices * <li>mobile_theme - the name of the default theme for mobile devices
...@@ -111,16 +113,26 @@ class Initializer ...@@ -111,16 +113,26 @@ class Initializer
* <li>generator - a Generator value to display in the HTML header * <li>generator - a Generator value to display in the HTML header
* (optional)</li> * (optional)</li>
* </ul> * </ul>
* @param MvcEvent $event Laminas MVC Event object * @param MvcEvent|ContainerInterface $eventOrContainer Laminas MVC Event object
* OR service container object
*/ */
public function __construct(Config $config, MvcEvent $event) public function __construct(Config $config, $eventOrContainer)
{ {
// Store parameters: // Store parameters:
$this->config = $config; $this->config = $config;
$this->event = $event;
// Grab the service manager for convenience: if ($eventOrContainer instanceof MvcEvent) {
$this->serviceManager = $this->event->getApplication()->getServiceManager(); $this->event = $eventOrContainer;
$this->serviceManager = $this->event->getApplication()
->getServiceManager();
} elseif ($eventOrContainer instanceof ContainerInterface) {
$this->event = null;
$this->serviceManager = $eventOrContainer;
} else {
throw new \Exception(
'Illegal type for $eventOrContainer: ' . get_class($eventOrContainer)
);
}
// Get the cookie manager from the service manager: // Get the cookie manager from the service manager:
$this->cookieManager = $this->serviceManager $this->cookieManager = $this->serviceManager
...@@ -150,7 +162,9 @@ class Initializer ...@@ -150,7 +162,9 @@ class Initializer
self::$themeInitialized = true; self::$themeInitialized = true;
// Determine the current theme: // Determine the current theme:
$currentTheme = $this->pickTheme($this->event->getRequest()); $currentTheme = $this->pickTheme(
isset($this->event) ? $this->event->getRequest() : null
);
// Determine theme options: // Determine theme options:
$this->sendThemeOptionsToView(); $this->sendThemeOptionsToView();
...@@ -178,11 +192,12 @@ class Initializer ...@@ -178,11 +192,12 @@ class Initializer
/** /**
* Support method for init() -- figure out which theme option is active. * Support method for init() -- figure out which theme option is active.
* *
* @param Request $request Request object (for obtaining user parameters). * @param Request $request Request object (for obtaining user parameters);
* set to null if no request context is available.
* *
* @return string * @return string
*/ */
protected function pickTheme(Request $request) protected function pickTheme(?Request $request)
{ {
// Load standard configuration options: // Load standard configuration options:
$standardTheme = $this->config->theme; $standardTheme = $this->config->theme;
...@@ -193,12 +208,14 @@ class Initializer ...@@ -193,12 +208,14 @@ class Initializer
? $this->config->mobile_theme : false; ? $this->config->mobile_theme : false;
// Find out if the user has a saved preference in the POST, URL or cookies: // Find out if the user has a saved preference in the POST, URL or cookies:
$selectedUI = $request->getPost()->get( if (isset($request)) {
'ui', $request->getQuery()->get( $selectedUI = $request->getPost()->get(
'ui', isset($request->getCookie()->ui) 'ui', $request->getQuery()->get(
? $request->getCookie()->ui : null 'ui', isset($request->getCookie()->ui)
) ? $request->getCookie()->ui : null
); )
);
}
if (empty($selectedUI)) { if (empty($selectedUI)) {
$selectedUI = ($mobileTheme && $this->mobile->detect()) $selectedUI = ($mobileTheme && $this->mobile->detect())
? 'mobile' : 'standard'; ? 'mobile' : 'standard';
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment