a2eada1a1f05dd78ccfeb53bb6ac24ed4f782110
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Driver / DriverRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Driver;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Registry for driver classes.
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class DriverRegistry implements \TYPO3\CMS\Core\SingletonInterface {
37
38 /**
39 * @var array
40 */
41 protected $drivers = array();
42
43 /**
44 * @var array
45 */
46 protected $driverConfigurations = array();
47
48 /**
49 * Creates this object.
50 */
51 public function __construct() {
52 $driverConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'];
53 foreach ($driverConfigurations as $shortName => $driverConfig) {
54 $shortName = $shortName ?: $driverConfig['shortName'];
55 $this->registerDriverClass($driverConfig['class'], $shortName, $driverConfig['label'], $driverConfig['flexFormDS']);
56 }
57 }
58
59 /**
60 * Registers a driver class with an optional short name.
61 *
62 * @param string $className
63 * @param string $shortName
64 * @param string $label
65 * @param string $flexFormDataStructurePathAndFilename
66 * @return boolean TRUE if registering succeeded
67 */
68 public function registerDriverClass($className, $shortName = NULL, $label = NULL, $flexFormDataStructurePathAndFilename = NULL) {
69 // check if the class is available for TYPO3 before registering the driver
70 if (!class_exists($className)) {
71 throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1314979197);
72 }
73 if ($shortName === '') {
74 $shortName = $className;
75 }
76 if (array_key_exists($shortName, $this->drivers)) {
77 // Return immediately without changing configuration
78 if ($this->drivers[$shortName] === $className) {
79 return TRUE;
80 } else {
81 throw new \InvalidArgumentException('Driver ' . $shortName . ' is already registered.', 1314979451);
82 }
83 }
84 $this->drivers[$shortName] = $className;
85 $this->driverConfigurations[$shortName] = array(
86 'class' => $className,
87 'shortName' => $shortName,
88 'label' => $label,
89 'flexFormDS' => $flexFormDataStructurePathAndFilename
90 );
91 return TRUE;
92 }
93
94 /**
95 * @return void
96 */
97 public function addDriversToTCA() {
98 // Add driver to TCA of sys_file_storage
99 if (TYPO3_MODE !== 'BE') {
100 return;
101 }
102 foreach ($this->driverConfigurations as $driver) {
103 $label = $driver['label'] ?: $driver['class'];
104 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA('sys_file_storage');
105 $driverFieldConfig =& $GLOBALS['TCA']['sys_file_storage']['columns']['driver']['config'];
106 $driverFieldConfig['items'][] = array($label, $driver['shortName']);
107 if ($driver['flexFormDS']) {
108 $configurationFieldConfig =& $GLOBALS['TCA']['sys_file_storage']['columns']['configuration']['config'];
109 $configurationFieldConfig['ds'][$driver['shortName']] = $driver['flexFormDS'];
110 }
111 }
112 }
113
114 /**
115 * Returns a class name for a given class name or short name.
116 *
117 * @param string $shortName
118 * @return string The class name
119 */
120 public function getDriverClass($shortName) {
121 if (class_exists($shortName) && in_array($shortName, $this->drivers)) {
122 return $shortName;
123 }
124 if (!array_key_exists($shortName, $this->drivers)) {
125 throw new \InvalidArgumentException('Desired storage is not in the list of available storages.', 1314085990);
126 }
127 return $this->drivers[$shortName];
128 }
129
130 /**
131 * Checks if the given driver exists
132 *
133 * @param string $shortName Name of the driver
134 * @return boolean TRUE if the driver exists, FALSE otherwise
135 */
136 public function driverExists($shortName) {
137 return array_key_exists($shortName, $this->drivers);
138 }
139 }
140
141
142 ?>