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

4 5
/*
 * This file is part of the TYPO3 CMS project.
flyguide's avatar
flyguide committed
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
flyguide's avatar
flyguide committed
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
flyguide's avatar
flyguide committed
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
flyguide's avatar
flyguide committed
16 17 18 19

/**
 * Manager for extension providers
 */
20
class ProviderManager implements \TYPO3\CMS\Core\SingletonInterface
21
{
22

23
    /**
24
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
25 26
     */
    protected $objectManager;
27

28 29 30 31
    /**
     * @var array
     */
    protected $providers;
32

33 34
    /**
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
35
     */
36
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
37 38 39
    {
        $this->objectManager = $objectManager;
    }
40

41 42 43 44
    /**
     * Get an instance of a concrete extension provider
     *
     * @param string $name Name of the provider
45
     * @return \T3o\TerFe2\Provider\ProviderInterface Extension provider
46
     * @throws \Exception
47 48 49 50
     */
    public function getProvider($name)
    {
        if (empty($name)) {
51
            throw new \Exception('No empty name allowed for an extension provider');
52
        }
53

54
        $name = strtolower(trim($name));
55

56 57 58
        if (!empty($this->providers[$name])) {
            return $this->providers[$name];
        }
59

60
        if (empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$name])) {
61
            throw new \Exception('No configuration found for an extension provider with name "' . $name . '"');
62
        }
63

64 65
        $configuration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$name];
        if (empty($configuration['class'])) {
66
            throw new \Exception('No class name found in configuration for the extension provider with name "' . $name . '"');
67
        }
68

69
        $provider = $this->objectManager->get($configuration['class']);
70
        if (!($provider instanceof \T3o\TerFe2\Provider\ProviderInterface)) {
71
            throw new \Exception('Provider "' . $name . '" does not implement the interface "\T3o\TerFe2\Provider\ProviderInterface"');
72 73 74 75 76 77 78
        }
        if (!empty($configuration['configuration']) && method_exists($provider, 'setConfiguration')) {
            $provider->setConfiguration($configuration['configuration']);
        }
        if (method_exists($provider, 'initializeProvider')) {
            $provider->initializeProvider();
        }
79

80 81
        return $this->providers[$name] = $provider;
    }
flyguide's avatar
flyguide committed
82
}