[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Driver / DriverRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Driver;
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 * Registry for driver classes.
19 */
20 class DriverRegistry implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * @var array
24 */
25 protected $drivers = [];
26
27 /**
28 * @var array
29 */
30 protected $driverConfigurations = [];
31
32 /**
33 * Creates this object by detecting all available drivers registered in $TYPO3_CONF_VARS.
34 */
35 public function __construct()
36 {
37 $driverConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'];
38 foreach ($driverConfigurations as $shortName => $driverConfig) {
39 $shortName = $shortName ?: $driverConfig['shortName'];
40 $this->registerDriverClass($driverConfig['class'], $shortName, $driverConfig['label'], $driverConfig['flexFormDS']);
41 }
42 }
43
44 /**
45 * Registers a driver class with an optional short name.
46 *
47 * @param string $className
48 * @param string $shortName
49 * @param string $label
50 * @param string $flexFormDataStructurePathAndFilename
51 * @return bool TRUE if registering succeeded
52 * @throws \InvalidArgumentException
53 */
54 public function registerDriverClass($className, $shortName = null, $label = null, $flexFormDataStructurePathAndFilename = null)
55 {
56 // check if the class is available for TYPO3 before registering the driver
57 if (!class_exists($className)) {
58 throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1314979197);
59 }
60
61 if (!in_array(DriverInterface::class, class_implements($className), true)) {
62 throw new \InvalidArgumentException('Driver ' . $className . ' needs to implement the DriverInterface.', 1387619575);
63 }
64 if ($shortName === '') {
65 $shortName = $className;
66 }
67 if (array_key_exists($shortName, $this->drivers)) {
68 // Return immediately without changing configuration
69 if ($this->drivers[$shortName] === $className) {
70 return true;
71 }
72 throw new \InvalidArgumentException('Driver ' . $shortName . ' is already registered.', 1314979451);
73 }
74 $this->drivers[$shortName] = $className;
75 $this->driverConfigurations[$shortName] = [
76 'class' => $className,
77 'shortName' => $shortName,
78 'label' => $label,
79 'flexFormDS' => $flexFormDataStructurePathAndFilename
80 ];
81 return true;
82 }
83
84 /**
85 * Adds the TCA information so the registered drivers can be selected when creating a sys_file_storage
86 * in the TYPO3 Backend.
87 */
88 public function addDriversToTCA()
89 {
90 $driverFieldConfig = &$GLOBALS['TCA']['sys_file_storage']['columns']['driver']['config'];
91 $configurationFieldConfig = &$GLOBALS['TCA']['sys_file_storage']['columns']['configuration']['config'];
92 foreach ($this->driverConfigurations as $driver) {
93 $label = $driver['label'] ?: $driver['class'];
94 $driverFieldConfig['items'][$driver['shortName']] = [$label, $driver['shortName']];
95 if ($driver['flexFormDS']) {
96 $configurationFieldConfig['ds'][$driver['shortName']] = $driver['flexFormDS'];
97 }
98 }
99 }
100
101 /**
102 * Returns a class name for a given class name or short name.
103 *
104 * @param string $shortName
105 * @return string The class name
106 * @throws \InvalidArgumentException
107 */
108 public function getDriverClass($shortName)
109 {
110 if (in_array($shortName, $this->drivers) && class_exists($shortName)) {
111 return $shortName;
112 }
113 if (!array_key_exists($shortName, $this->drivers)) {
114 throw new \InvalidArgumentException(
115 'Desired storage "' . $shortName . '" is not in the list of available storages.',
116 1314085990
117 );
118 }
119 return $this->drivers[$shortName];
120 }
121
122 /**
123 * Checks if the given driver exists
124 *
125 * @param string $shortName Name of the driver
126 * @return bool TRUE if the driver exists, FALSE otherwise
127 */
128 public function driverExists($shortName)
129 {
130 return array_key_exists($shortName, $this->drivers);
131 }
132 }