[TASK] Update copyright year to 2013
[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-2013 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 */
34 class DriverRegistry implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var array
38 */
39 protected $drivers = array();
40
41 /**
42 * @var array
43 */
44 protected $driverConfigurations = array();
45
46 /**
47 * Creates this object.
48 */
49 public function __construct() {
50 $driverConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'];
51 foreach ($driverConfigurations as $shortName => $driverConfig) {
52 $shortName = $shortName ?: $driverConfig['shortName'];
53 $this->registerDriverClass($driverConfig['class'], $shortName, $driverConfig['label'], $driverConfig['flexFormDS']);
54 }
55 }
56
57 /**
58 * Registers a driver class with an optional short name.
59 *
60 * @param string $className
61 * @param string $shortName
62 * @param string $label
63 * @param string $flexFormDataStructurePathAndFilename
64 * @return boolean TRUE if registering succeeded
65 */
66 public function registerDriverClass($className, $shortName = NULL, $label = NULL, $flexFormDataStructurePathAndFilename = NULL) {
67 // check if the class is available for TYPO3 before registering the driver
68 if (!class_exists($className)) {
69 throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1314979197);
70 }
71 if ($shortName === '') {
72 $shortName = $className;
73 }
74 if (array_key_exists($shortName, $this->drivers)) {
75 // Return immediately without changing configuration
76 if ($this->drivers[$shortName] === $className) {
77 return TRUE;
78 } else {
79 throw new \InvalidArgumentException('Driver ' . $shortName . ' is already registered.', 1314979451);
80 }
81 }
82 $this->drivers[$shortName] = $className;
83 $this->driverConfigurations[$shortName] = array(
84 'class' => $className,
85 'shortName' => $shortName,
86 'label' => $label,
87 'flexFormDS' => $flexFormDataStructurePathAndFilename
88 );
89 return TRUE;
90 }
91
92 /**
93 * @return void
94 */
95 public function addDriversToTCA() {
96 // Add driver to TCA of sys_file_storage
97 if (TYPO3_MODE !== 'BE') {
98 return;
99 }
100 foreach ($this->driverConfigurations as $driver) {
101 $label = $driver['label'] ?: $driver['class'];
102 $driverFieldConfig = &$GLOBALS['TCA']['sys_file_storage']['columns']['driver']['config'];
103 $driverFieldConfig['items'][] = array($label, $driver['shortName']);
104 if ($driver['flexFormDS']) {
105 $configurationFieldConfig = &$GLOBALS['TCA']['sys_file_storage']['columns']['configuration']['config'];
106 $configurationFieldConfig['ds'][$driver['shortName']] = $driver['flexFormDS'];
107 }
108 }
109 }
110
111 /**
112 * Returns a class name for a given class name or short name.
113 *
114 * @param string $shortName
115 * @return string The class name
116 */
117 public function getDriverClass($shortName) {
118 if (class_exists($shortName) && in_array($shortName, $this->drivers)) {
119 return $shortName;
120 }
121 if (!array_key_exists($shortName, $this->drivers)) {
122 throw new \InvalidArgumentException('Desired storage is not in the list of available storages.', 1314085990);
123 }
124 return $this->drivers[$shortName];
125 }
126
127 /**
128 * Checks if the given driver exists
129 *
130 * @param string $shortName Name of the driver
131 * @return boolean TRUE if the driver exists, FALSE otherwise
132 */
133 public function driverExists($shortName) {
134 return array_key_exists($shortName, $this->drivers);
135 }
136 }
137
138
139 ?>