[TASK] Use name-resolution instead of strings where possible: 7
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / MediaWizard / MediaWizardProviderManager.php
1 <?php
2 namespace TYPO3\CMS\Frontend\MediaWizard;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Manager to register and call registered media wizard providers
19 *
20 * @author Ernesto Baschny <ernst@cron-it.de>
21 * @static
22 */
23 class MediaWizardProviderManager {
24
25 /**
26 * @var array All class names of registered providers
27 */
28 static protected $providers = array();
29
30 /**
31 * @var array Instances of registered providers, set up on first call of getValidMediaWizardProvider()
32 */
33 static protected $providerObjects = array();
34
35 /**
36 * Allows extensions to register themselves as media wizard providers
37 *
38 * @param string $className A class implementing MediaWizardProviderInterface
39 * @throws \UnexpectedValueException
40 * @return void
41 */
42 static public function registerMediaWizardProvider($className) {
43 self::$providers[] = $className;
44 }
45
46 /**
47 * Instantiate all registered media wizard providers
48 *
49 * @throws \UnexpectedValueException
50 */
51 static protected function instantiateMediaWizardProviders() {
52 $providerClassNames = array_unique(self::$providers);
53 foreach ($providerClassNames as $className) {
54 if (!isset(self::$providerObjects[$className])) {
55 $provider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
56 if (!$provider instanceof MediaWizardProviderInterface) {
57 throw new \UnexpectedValueException($className . ' is registered as a mediaWizardProvider, so it must implement interface ' . \TYPO3\CMS\Frontend\MediaWizard\MediaWizardProviderInterface::class, 1285022360);
58 }
59 self::$providerObjects[$className] = $provider;
60 }
61 }
62 }
63
64 /**
65 * Return a media wizard provider that can handle given URL
66 *
67 * @param string $url
68 * @return MediaWizardProviderInterface|NULL A valid mediaWizardProvider that can handle this URL
69 */
70 static public function getValidMediaWizardProvider($url) {
71 self::instantiateMediaWizardProviders();
72 // Go through registered providers in reverse order (last one registered wins)
73 $providers = array_reverse(self::$providerObjects, TRUE);
74 foreach ($providers as $provider) {
75 /** @var $provider MediaWizardProviderInterface */
76 if ($provider->canHandle($url)) {
77 return $provider;
78 }
79 }
80 // No provider found
81 return NULL;
82 }
83
84 }