TYPO3 CMS  TYPO3_7-6
DriverRegistry.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $drivers = [];
26 
30  protected $driverConfigurations = [];
31 
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 
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(\TYPO3\CMS\Core\Resource\Driver\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  } else {
72  throw new \InvalidArgumentException('Driver ' . $shortName . ' is already registered.', 1314979451);
73  }
74  }
75  $this->drivers[$shortName] = $className;
76  $this->driverConfigurations[$shortName] = [
77  'class' => $className,
78  'shortName' => $shortName,
79  'label' => $label,
80  'flexFormDS' => $flexFormDataStructurePathAndFilename
81  ];
82  return true;
83  }
84 
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 
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  return $this->drivers[$shortName];
119  }
120 
127  public function driverExists($shortName)
128  {
129  return array_key_exists($shortName, $this->drivers);
130  }
131 }
registerDriverClass($className, $shortName=null, $label=null, $flexFormDataStructurePathAndFilename=null)
$driver
Definition: server.php:36
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']