From 1972e57468beffc9d1c33d447cf2eae06ca5a2ee Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Tue, 9 Oct 2018 11:28:57 -0400
Subject: [PATCH] Set up formatters as services.

---
 module/VuFindApi/config/module.config.php     |  7 ++
 .../src/VuFindApi/Controller/Factory.php      | 12 ++--
 .../Formatter/RecordFormatterFactory.php      | 70 +++++++++++++++++++
 3 files changed, 82 insertions(+), 7 deletions(-)
 create mode 100644 module/VuFindApi/src/VuFindApi/Formatter/RecordFormatterFactory.php

diff --git a/module/VuFindApi/config/module.config.php b/module/VuFindApi/config/module.config.php
index 8b72c900a32..bdd1bdea5a6 100644
--- a/module/VuFindApi/config/module.config.php
+++ b/module/VuFindApi/config/module.config.php
@@ -12,6 +12,13 @@ $config = [
             'SearchApi' => 'VuFindApi\Controller\SearchApiController',
         ],
     ],
+    'service_manager' => [
+        'allow_override' => true,
+        'factories' => [
+            'VuFindApi\Formatter\FacetFormatter' => 'Zend\ServiceManager\Factory\InvokableFactory',
+            'VuFindApi\Formatter\RecordFormatter' => 'VuFindApi\Formatter\RecordFormatterFactory',
+        ],
+    ],
     'router' => [
         'routes' => [
             'apiHome' => [
diff --git a/module/VuFindApi/src/VuFindApi/Controller/Factory.php b/module/VuFindApi/src/VuFindApi/Controller/Factory.php
index 0f95d5a5d1b..970b5ee254c 100644
--- a/module/VuFindApi/src/VuFindApi/Controller/Factory.php
+++ b/module/VuFindApi/src/VuFindApi/Controller/Factory.php
@@ -28,7 +28,6 @@
 namespace VuFindApi\Controller;
 
 use VuFindApi\Formatter\FacetFormatter;
-use VuFindApi\Formatter\RecordFormatter;
 use Zend\ServiceManager\ServiceManager;
 
 /**
@@ -67,11 +66,10 @@ class Factory
      */
     public static function getSearchApiController(ServiceManager $sm)
     {
-        $recordFields = $sm->get('VuFind\Config\YamlReader')
-            ->get('SearchApiRecordFields.yaml');
-        $helperManager = $sm->get('ViewHelperManager');
-        $rf = new RecordFormatter($recordFields, $helperManager);
-        $controller = new SearchApiController($sm, $rf, new FacetFormatter());
-        return $controller;
+        return new SearchApiController(
+            $sm,
+            $sm->get('VuFindApi\Formatter\RecordFormatter'),
+            $sm->get('VuFindApi\Formatter\FacetFormatter')
+        );
     }
 }
diff --git a/module/VuFindApi/src/VuFindApi/Formatter/RecordFormatterFactory.php b/module/VuFindApi/src/VuFindApi/Formatter/RecordFormatterFactory.php
new file mode 100644
index 00000000000..84b1cc4c7d6
--- /dev/null
+++ b/module/VuFindApi/src/VuFindApi/Formatter/RecordFormatterFactory.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Record Formatter factory.
+ *
+ * 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  API_Formatter
+ * @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 VuFindApi\Formatter;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * Record Formatter factory.
+ *
+ * @category VuFind
+ * @package  API_Formatter
+ * @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 RecordFormatterFactory implements FactoryInterface
+{
+    /**
+     * 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
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options sent to factory.');
+        }
+
+        $recordFields = $container->get('VuFind\Config\YamlReader')
+            ->get('SearchApiRecordFields.yaml');
+        $helperManager = $container->get('ViewHelperManager');
+        return new $requestedName($recordFields, $helperManager);
+    }
+}
-- 
GitLab