From 8deb7add00da2d027835e7f5206f7199de6e6928 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 15 Jun 2018 10:29:43 -0400
Subject: [PATCH] Improvements to IlsAwareTrait. - A new config.ini setting
 allows the user to specify which search backends contain ILS records. -
 SolrMarcFactory has been replaced with IlsAwareDelegatorFactory to make
 composing the trait with other classes easier.

---
 config/vufind/config.ini                      |   7 +-
 .../RecordDriver/IlsAwareDelegatorFactory.php | 100 ++++++++++++++++++
 .../src/VuFind/RecordDriver/IlsAwareTrait.php |  26 ++++-
 .../src/VuFind/RecordDriver/PluginManager.php |  16 ++-
 .../VuFind/RecordDriver/SolrMarcFactory.php   |  70 ------------
 5 files changed, 143 insertions(+), 76 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/RecordDriver/IlsAwareDelegatorFactory.php
 delete mode 100644 module/VuFind/src/VuFind/RecordDriver/SolrMarcFactory.php

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 0d5b2c16020..5082e57a6b6 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -227,9 +227,14 @@ session_name = VUFIND_SESSION
 [Catalog]
 driver          = Sample
 
-;loadNoILSOnFailure - Whether or not to load the NoILS driver if the main driver fails
+; loadNoILSOnFailure - Whether or not to load the NoILS driver if the main driver fails
 loadNoILSOnFailure = false
 
+; List of search backends that contain records from your ILS (defaults to Solr
+; unless set otherwise). You can set ilsBackends = false to disable ILS status
+; loading entirely.
+;ilsBackends[] = Solr
+
 ; This setting determines how and when hold / recall links are displayed.
 ; Legal values:
 ; - all (Show links for all items - Place Hold for Available Items and Place Recall
diff --git a/module/VuFind/src/VuFind/RecordDriver/IlsAwareDelegatorFactory.php b/module/VuFind/src/VuFind/RecordDriver/IlsAwareDelegatorFactory.php
new file mode 100644
index 00000000000..87cb42735a8
--- /dev/null
+++ b/module/VuFind/src/VuFind/RecordDriver/IlsAwareDelegatorFactory.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * ILS aware delegator factory
+ *
+ * Copyright (C) Villanova University 2018.
+ *
+ * PHP version 7
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * @category VuFind
+ * @package  RecordDrivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
+ */
+namespace VuFind\RecordDriver;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
+
+/**
+ * ILS aware delegator factory
+ *
+ * @category VuFind
+ * @package  RecordDrivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
+ */
+class IlsAwareDelegatorFactory implements DelegatorFactoryInterface
+{
+    /**
+     * Invokes this factory.
+     *
+     * @param ContainerInterface $container Service container
+     * @param string             $name      Service name
+     * @param callable           $callback  Service callback
+     * @param array|null         $options   Service options
+     *
+     * @return SecureDelegator
+     */
+    public function __invoke(ContainerInterface $container, $name,
+        callable $callback, array $options = null
+    ) {
+        /**
+         * The wrapped session handler.
+         *
+         * @var HandlerInterface $handler
+         */
+        $driver = call_user_func($callback);
+
+        // Attach the ILS if at least one backend supports it:
+        $ilsBackends = $this->getIlsBackends($container);
+        if (!empty($ilsBackends) && $container->has('VuFind\ILS\Connection')) {
+            $driver->attachILS(
+                $container->get('VuFind\ILS\Connection'),
+                $container->get('VuFind\ILS\Logic\Holds'),
+                $container->get('VuFind\ILS\Logic\TitleHolds')
+            );
+            $driver->setIlsBackends($ilsBackends);
+        }
+
+        return $driver;
+    }
+
+    /**
+     * Get the ILS backend configuration.
+     *
+     * @param ContainerInterface $container Service container
+     *
+     * @return string[]
+     */
+    protected function getIlsBackends(ContainerInterface $container)
+    {
+        // Get a list of ILS-compatible backends.
+        static $ilsBackends = null;
+        if (!is_array($ilsBackends)) {
+            $config = $container->get('VuFind\Config\PluginManager')->get('config');
+            $settings = isset($config->Catalog) ? $config->Catalog->toArray() : [];
+
+            // If the setting is missing, default to the default backend; if it
+            // is present but empty, don't put an empty string in the final array!
+            $rawSetting = $settings['ilsBackends'] ?? [DEFAULT_SEARCH_BACKEND];
+            $ilsBackends = empty($rawSetting) ? [] : (array)$rawSetting;
+        }
+        return $ilsBackends;
+    }
+}
diff --git a/module/VuFind/src/VuFind/RecordDriver/IlsAwareTrait.php b/module/VuFind/src/VuFind/RecordDriver/IlsAwareTrait.php
index 1760af62bda..323ff5cbee4 100644
--- a/module/VuFind/src/VuFind/RecordDriver/IlsAwareTrait.php
+++ b/module/VuFind/src/VuFind/RecordDriver/IlsAwareTrait.php
@@ -48,19 +48,26 @@ trait IlsAwareTrait
      */
     protected $ils = null;
 
+    /**
+     * Backends with ILS integration.
+     *
+     * @var string[]
+     */
+    protected $ilsBackends = [];
+
     /**
      * Hold logic
      *
      * @var \VuFind\ILS\Logic\Holds
      */
-    protected $holdLogic;
+    protected $holdLogic = null;
 
     /**
      * Title hold logic
      *
      * @var \VuFind\ILS\Logic\TitleHolds
      */
-    protected $titleHoldLogic;
+    protected $titleHoldLogic = null;
 
     /**
      * Attach an ILS connection and related logic to the driver
@@ -87,7 +94,8 @@ trait IlsAwareTrait
      */
     protected function hasILS()
     {
-        return null !== $this->ils;
+        return null !== $this->ils
+            && in_array($this->getSourceIdentifier(), $this->ilsBackends);
     }
 
     /**
@@ -141,6 +149,18 @@ trait IlsAwareTrait
         return false;
     }
 
+    /**
+     * Set the list of backends that support ILS integration.
+     *
+     * @param array $backends List of backends that support ILS integration
+     *
+     * @return string[]
+     */
+    public function setIlsBackends($backends)
+    {
+        $this->ilsBackends = $backends;
+    }
+
     /**
      * Returns true if the record supports real-time AJAX status lookups.
      *
diff --git a/module/VuFind/src/VuFind/RecordDriver/PluginManager.php b/module/VuFind/src/VuFind/RecordDriver/PluginManager.php
index 7b734e1fea7..6c8cc22c134 100644
--- a/module/VuFind/src/VuFind/RecordDriver/PluginManager.php
+++ b/module/VuFind/src/VuFind/RecordDriver/PluginManager.php
@@ -61,6 +61,18 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
         'worldcat' => 'VuFind\RecordDriver\WorldCat',
     ];
 
+    /**
+     * Default delegator factories.
+     *
+     * @var string[][]|\Zend\ServiceManager\Factory\DelegatorFactoryInterface[][]
+     */
+    protected $delegators = [
+        'VuFind\RecordDriver\SolrMarc' =>
+            ['VuFind\RecordDriver\IlsAwareDelegatorFactory'],
+        'VuFind\RecordDriver\SolrMarcRemote' =>
+            ['VuFind\RecordDriver\IlsAwareDelegatorFactory'],
+    ];
+
     /**
      * Default plugin factories.
      *
@@ -81,9 +93,9 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
             'VuFind\RecordDriver\SolrDefaultWithoutSearchServiceFactory',
         'VuFind\RecordDriver\SolrDefault' =>
             'VuFind\RecordDriver\SolrDefaultFactory',
-        'VuFind\RecordDriver\SolrMarc' => 'VuFind\RecordDriver\SolrMarcFactory',
+        'VuFind\RecordDriver\SolrMarc' => 'VuFind\RecordDriver\SolrDefaultFactory',
         'VuFind\RecordDriver\SolrMarcRemote' =>
-            'VuFind\RecordDriver\SolrMarcFactory',
+            'VuFind\RecordDriver\SolrDefaultFactory',
         'VuFind\RecordDriver\SolrReserves' =>
             'VuFind\RecordDriver\SolrDefaultWithoutSearchServiceFactory',
         'VuFind\RecordDriver\SolrWeb' => 'VuFind\RecordDriver\SolrWebFactory',
diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrMarcFactory.php b/module/VuFind/src/VuFind/RecordDriver/SolrMarcFactory.php
deleted file mode 100644
index 4fd9463a239..00000000000
--- a/module/VuFind/src/VuFind/RecordDriver/SolrMarcFactory.php
+++ /dev/null
@@ -1,70 +0,0 @@
-<?php
-/**
- * Factory for SolrMarc record drivers.
- *
- * PHP version 7
- *
- * Copyright (C) Villanova University 2018.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * @category VuFind
- * @package  RecordDrivers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     https://vufind.org/wiki/development Wiki
- */
-namespace VuFind\RecordDriver;
-
-use Interop\Container\ContainerInterface;
-
-/**
- * Factory for SolrMarc record drivers.
- *
- * @category VuFind
- * @package  RecordDrivers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     https://vufind.org/wiki/development Wiki
- */
-class SolrMarcFactory extends SolrDefaultFactory
-{
-    /**
-     * Create an object
-     *
-     * @param ContainerInterface $container     Service manager
-     * @param string             $requestedName Service being created
-     * @param null|array         $options       Extra options (optional)
-     *
-     * @return object
-     *
-     * @throws ServiceNotFoundException if unable to resolve the service.
-     * @throws ServiceNotCreatedException if an exception is raised when
-     * creating a service.
-     * @throws ContainerException if any other error occurs
-     */
-    public function __invoke(ContainerInterface $container, $requestedName,
-        array $options = null
-    ) {
-        $driver = parent::__invoke($container, $requestedName, $options);
-        if ($container->has('VuFind\ILS\Connection')) {
-            $driver->attachILS(
-                $container->get('VuFind\ILS\Connection'),
-                $container->get('VuFind\ILS\Logic\Holds'),
-                $container->get('VuFind\ILS\Logic\TitleHolds')
-            );
-        }
-        return $driver;
-    }
-}
-- 
GitLab