Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractPluginManagerFactory
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 getConfigKey
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 __invoke
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * VuFind Plugin Manager factory.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  ServiceManager
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\ServiceManager;
31
32use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
33use Laminas\ServiceManager\Exception\ServiceNotFoundException;
34use Laminas\ServiceManager\Factory\FactoryInterface;
35use Psr\Container\ContainerExceptionInterface as ContainerException;
36use Psr\Container\ContainerInterface;
37
38/**
39 * VuFind Plugin Manager factory.
40 *
41 * @category VuFind
42 * @package  ServiceManager
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development Wiki
46 */
47class AbstractPluginManagerFactory implements FactoryInterface
48{
49    /**
50     * Determine the configuration key for the specified class name.
51     *
52     * @param string $requestedName Service being created
53     *
54     * @return string
55     */
56    public function getConfigKey($requestedName)
57    {
58        // Extract namespace of plugin manager (chop off leading top-level
59        // namespace -- e.g. VuFind -- and trailing PluginManager class).
60        $regex = '/^[^\\\\]+\\\\(.*)\\\\PluginManager$/';
61        preg_match($regex, $requestedName, $matches);
62        return strtolower(str_replace('\\', '_', $matches[1]));
63    }
64
65    /**
66     * Create an object
67     *
68     * @param ContainerInterface $container     Service manager
69     * @param string             $requestedName Service being created
70     * @param null|array         $options       Extra options (optional)
71     *
72     * @return object
73     *
74     * @throws ServiceNotFoundException if unable to resolve the service.
75     * @throws ServiceNotCreatedException if an exception is raised when
76     * creating a service.
77     * @throws ContainerException&\Throwable if any other error occurs
78     */
79    public function __invoke(
80        ContainerInterface $container,
81        $requestedName,
82        array $options = null
83    ) {
84        if (!empty($options)) {
85            throw new \Exception('Unexpected options sent to factory.');
86        }
87        $configKey = $this->getConfigKey($requestedName);
88        if (empty($configKey)) {
89            $error = 'Problem determining config key for ' . $requestedName;
90            throw new \Exception($error);
91        }
92        $config = $container->get('Config');
93        return new $requestedName(
94            $container,
95            $config['vufind']['plugin_managers'][$configKey]
96        );
97    }
98}