‪TYPO3CMS  10.4
DriverRegistry.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
24 {
28  protected ‪$drivers = [];
29 
33  protected ‪$driverConfigurations = [];
34 
38  public function ‪__construct()
39  {
40  ‪$driverConfigurations = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'];
41  foreach (‪$driverConfigurations as $shortName => $driverConfig) {
42  $shortName = $shortName ?: $driverConfig['shortName'] ?? '';
43  $this->‪registerDriverClass($driverConfig['class'] ?? '', $shortName, $driverConfig['label'] ?? '', $driverConfig['flexFormDS'] ?? '');
44  }
45  }
46 
57  public function ‪registerDriverClass($className, $shortName = null, $label = null, $flexFormDataStructurePathAndFilename = null)
58  {
59  // todo: Default of $shortName must be empty string, not null.
60  $shortName = (string)$shortName;
61 
62  // check if the class is available for TYPO3 before registering the driver
63  if (!class_exists($className)) {
64  throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1314979197);
65  }
66 
67  if (!in_array(DriverInterface::class, class_implements($className), true)) {
68  throw new \InvalidArgumentException('Driver ' . $className . ' needs to implement the DriverInterface.', 1387619575);
69  }
70  if ($shortName === '') {
71  $shortName = $className;
72  }
73  if (array_key_exists($shortName, $this->drivers)) {
74  // Return immediately without changing configuration
75  if ($this->drivers[$shortName] === $className) {
76  return true;
77  }
78  throw new \InvalidArgumentException('Driver ' . $shortName . ' is already registered.', 1314979451);
79  }
80  $this->drivers[$shortName] = $className;
81  $this->driverConfigurations[$shortName] = [
82  'class' => $className,
83  'shortName' => $shortName,
84  'label' => $label,
85  'flexFormDS' => $flexFormDataStructurePathAndFilename
86  ];
87  return true;
88  }
89 
94  public function ‪addDriversToTCA()
95  {
96  $driverFieldConfig = &‪$GLOBALS['TCA']['sys_file_storage']['columns']['driver']['config'];
97  $configurationFieldConfig = &‪$GLOBALS['TCA']['sys_file_storage']['columns']['configuration']['config'];
98  foreach ($this->driverConfigurations as $driver) {
99  $label = $driver['label'] ?: $driver['class'];
100  $driverFieldConfig['items'][$driver['shortName']] = [$label, $driver['shortName']];
101  if ($driver['flexFormDS']) {
102  $configurationFieldConfig['ds'][$driver['shortName']] = $driver['flexFormDS'];
103  }
104  }
105  }
106 
114  public function ‪getDriverClass($shortName)
115  {
116  if (in_array($shortName, $this->drivers) && class_exists($shortName)) {
117  return $shortName;
118  }
119  if (!array_key_exists($shortName, $this->drivers)) {
120  throw new \InvalidArgumentException(
121  'Desired storage "' . $shortName . '" is not in the list of available storages.',
122  1314085990
123  );
124  }
125  return $this->drivers[$shortName];
126  }
127 
134  public function ‪driverExists($shortName)
135  {
136  return array_key_exists($shortName, $this->drivers);
137  }
138 }
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\$drivers
‪array $drivers
Definition: DriverRegistry.php:27
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\addDriversToTCA
‪addDriversToTCA()
Definition: DriverRegistry.php:92
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\getDriverClass
‪string getDriverClass($shortName)
Definition: DriverRegistry.php:112
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\$driverConfigurations
‪array $driverConfigurations
Definition: DriverRegistry.php:31
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry
Definition: DriverRegistry.php:24
‪TYPO3\CMS\Core\Resource\Driver
Definition: AbstractDriver.php:16
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\driverExists
‪bool driverExists($shortName)
Definition: DriverRegistry.php:132
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\__construct
‪__construct()
Definition: DriverRegistry.php:36
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry\registerDriverClass
‪bool registerDriverClass($className, $shortName=null, $label=null, $flexFormDataStructurePathAndFilename=null)
Definition: DriverRegistry.php:55