ProviderManager.php 3.2 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Provider;

flyguide's avatar
flyguide committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script 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.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Manager for extension providers
 */
31
class ProviderManager implements \TYPO3\CMS\Core\SingletonInterface
32
{
33

34
    /**
35
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36
37
     */
    protected $objectManager;
38

39
40
41
42
    /**
     * @var array
     */
    protected $providers;
43
44


45
46
    /**
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
47
48
     * @return void
     */
49
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
50
51
52
    {
        $this->objectManager = $objectManager;
    }
53

54

55
56
57
58
    /**
     * Get an instance of a concrete extension provider
     *
     * @param string $name Name of the provider
59
     * @return \T3o\TerFe2\Provider\ProviderInterface Extension provider
60
61
62
63
     */
    public function getProvider($name)
    {
        if (empty($name)) {
64
            throw new \Exception('No empty name allowed for an extension provider');
65
        }
66

67
        $name = strtolower(trim($name));
68

69
70
71
        if (!empty($this->providers[$name])) {
            return $this->providers[$name];
        }
72

73
        if (empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$name])) {
74
            throw new \Exception('No configuration found for an extension provider with name "' . $name . '"');
75
        }
76

77
78
        $configuration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$name];
        if (empty($configuration['class'])) {
79
            throw new \Exception('No class name found in configuration for the extension provider with name "' . $name . '"');
80
        }
81

82
        $provider = $this->objectManager->get($configuration['class']);
83
        if (!($provider instanceof \T3o\TerFe2\Provider\ProviderInterface)) {
84
            throw new \Exception('Provider "' . $name . '" does not implement the interface "\T3o\TerFe2\Provider\ProviderInterface"');
85
86
87
88
89
90
91
        }
        if (!empty($configuration['configuration']) && method_exists($provider, 'setConfiguration')) {
            $provider->setConfiguration($configuration['configuration']);
        }
        if (method_exists($provider, 'initializeProvider')) {
            $provider->initializeProvider();
        }
92

93
94
        return $this->providers[$name] = $provider;
    }
flyguide's avatar
flyguide committed
95
96

}