[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Collection / FileCollectionRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Collection;
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 use TYPO3\CMS\Core\SingletonInterface;
17 use TYPO3\CMS\Core\Utility\ArrayUtility;
18
19 /**
20 * Registry for FileCollection classes
21 */
22 class FileCollectionRegistry implements SingletonInterface
23 {
24 /**
25 * Registered FileCollection types
26 *
27 * @var array
28 */
29 protected $types = [];
30
31 /**
32 * Constructor
33 */
34 public function __construct()
35 {
36 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] as $type => $class) {
37 $this->registerFileCollectionClass($class, $type);
38 }
39 }
40
41 /**
42 * Register a (new) FileCollection type
43 *
44 * @param string $className
45 * @param string $type FileCollection type max length 30 chars (db field restriction)
46 * @param bool $override existing FileCollection type
47 * @return bool TRUE if registration succeeded
48 * @throws \InvalidArgumentException
49 */
50 public function registerFileCollectionClass($className, $type, $override = false)
51 {
52 if (strlen($type) > 30) {
53 throw new \InvalidArgumentException('FileCollection type can have a max string length of 30 bytes', 1391295611);
54 }
55
56 if (!class_exists($className)) {
57 throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1391295613);
58 }
59
60 if (!in_array(AbstractFileCollection::class, class_parents($className), true)) {
61 throw new \InvalidArgumentException('FileCollection ' . $className . ' needs to extend the AbstractFileCollection.', 1391295633);
62 }
63
64 if (isset($this->types[$type])) {
65 // Return immediately without changing configuration
66 if ($this->types[$type] === $className) {
67 return true;
68 }
69 if (!$override) {
70 throw new \InvalidArgumentException('FileCollections ' . $type . ' is already registered.', 1391295643);
71 }
72 }
73
74 $this->types[$type] = $className;
75 return true;
76 }
77
78 /**
79 * Add the type to the TCA of sys_file_collection
80 *
81 * @param string $type
82 * @param string $label
83 * @param string $availableFields comma separated list of fields to show
84 * @param array $additionalColumns Additional columns configuration
85 * @return array adjusted TCA for sys_file_collection
86 */
87 public function addTypeToTCA($type, $label, $availableFields, array $additionalColumns = [])
88 {
89 $GLOBALS['TCA']['sys_file_collection']['types'][$type] = [
90 'showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, ' . $availableFields
91 ];
92
93 // search for existing type when found override label
94 $typeFound = false;
95 foreach ($GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'] as $key => $item) {
96 if ($item[1] === $type) {
97 $typeFound = true;
98 $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$key][0] = $label;
99 }
100 }
101 if (!$typeFound) {
102 $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = [
103 0 => $label,
104 1 => $type
105 ];
106 }
107 if ($additionalColumns !== []) {
108 ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TCA']['sys_file_collection']['columns'], $additionalColumns);
109 }
110 return $GLOBALS['TCA']['sys_file_collection'];
111 }
112
113 /**
114 * Returns a class name for a given type
115 *
116 * @param string $type
117 * @return string The class name
118 * @throws \InvalidArgumentException
119 */
120 public function getFileCollectionClass($type)
121 {
122 if (!isset($this->types[$type])) {
123 throw new \InvalidArgumentException('Desired FileCollection type "' . $type . '" is not in the list of available FileCollections.', 1391295644);
124 }
125 return $this->types[$type];
126 }
127
128 /**
129 * Checks if the given FileCollection type exists
130 *
131 * @param string $type Type of the FileCollection
132 * @return bool TRUE if the FileCollection exists, FALSE otherwise
133 */
134 public function fileCollectionTypeExists($type)
135 {
136 return isset($this->types[$type]);
137 }
138 }