[CLEANUP] Alwas put null at the last position
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / BackendLayout / DataProviderCollection.php
1 <?php
2 namespace TYPO3\CMS\Backend\View\BackendLayout;
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
17 /**
18 * Collection of backend layout data providers.
19 */
20 class DataProviderCollection implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * @var array|DataProviderInterface[]
24 */
25 protected $dataProviders = [];
26
27 /**
28 * @var array
29 */
30 protected $results = [];
31
32 /**
33 * Adds a data provider to this collection.
34 *
35 * @param string $identifier
36 * @param string|object $classNameOrObject
37 * @throws \UnexpectedValueException
38 * @throws \LogicException
39 */
40 public function add($identifier, $classNameOrObject)
41 {
42 if (strpos($identifier, '__') !== false) {
43 throw new \UnexpectedValueException(
44 'Identifier "' . $identifier . '" must not contain "__"',
45 1381597629
46 );
47 }
48
49 if (is_object($classNameOrObject)) {
50 $className = get_class($classNameOrObject);
51 $dataProvider = $classNameOrObject;
52 } else {
53 $className = $classNameOrObject;
54 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($classNameOrObject);
55 }
56
57 if (!$dataProvider instanceof DataProviderInterface) {
58 throw new \LogicException(
59 $className . ' must implement interface ' . \TYPO3\CMS\Backend\View\BackendLayout\DataProviderInterface::class,
60 1381269811
61 );
62 }
63
64 $this->dataProviders[$identifier] = $dataProvider;
65 }
66
67 /**
68 * Gets all backend layout collections and thus, all
69 * backend layouts. Each data provider returns its own
70 * backend layout collection.
71 *
72 * @param DataProviderContext $dataProviderContext
73 * @return array|BackendLayoutCollection[]
74 */
75 public function getBackendLayoutCollections(DataProviderContext $dataProviderContext)
76 {
77 $result = [];
78
79 foreach ($this->dataProviders as $identifier => $dataProvider) {
80 $backendLayoutCollection = $this->createBackendLayoutCollection($identifier);
81 $dataProvider->addBackendLayouts($dataProviderContext, $backendLayoutCollection);
82 $result[$identifier] = $backendLayoutCollection;
83 }
84
85 return $result;
86 }
87
88 /**
89 * Gets a backend layout by a combined identifier, which is
90 * e.g. "myextension_regular" and "myextension" is the identifier
91 * of the accordant data provider and "regular" the identifier of
92 * the accordant backend layout.
93 *
94 * @param string $combinedIdentifier
95 * @param int $pageId
96 * @return BackendLayout|null
97 */
98 public function getBackendLayout($combinedIdentifier, $pageId)
99 {
100 $backendLayout = null;
101
102 if (strpos($combinedIdentifier, '__') === false) {
103 $dataProviderIdentifier = 'default';
104 $backendLayoutIdentifier = $combinedIdentifier;
105 } else {
106 list($dataProviderIdentifier, $backendLayoutIdentifier) = explode('__', $combinedIdentifier, 2);
107 }
108
109 if (isset($this->dataProviders[$dataProviderIdentifier])) {
110 $backendLayout = $this->dataProviders[$dataProviderIdentifier]->getBackendLayout($backendLayoutIdentifier, $pageId);
111 }
112
113 return $backendLayout;
114 }
115
116 /**
117 * Creates a new backend layout collection.
118 *
119 * @param string $identifier
120 * @return BackendLayoutCollection
121 */
122 protected function createBackendLayoutCollection($identifier)
123 {
124 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
125 BackendLayoutCollection::class,
126 $identifier
127 );
128 }
129 }