[TASK] Cleanup EXT:version
[Packages/TYPO3.CMS.git] / typo3 / sysext / version / Classes / Dependency / DependencyEntityFactory.php
1 <?php
2 namespace TYPO3\CMS\Version\Dependency;
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 * Object to create and keep track of element or reference entities.
19 */
20 class DependencyEntityFactory {
21
22 /**
23 * @var array
24 */
25 protected $elements = array();
26
27 /**
28 * @var array
29 */
30 protected $references = array();
31
32 /**
33 * Gets and registers a new element.
34 *
35 * @param string $table
36 * @param integer $id
37 * @param array $data (optional)
38 * @param \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency
39 * @return \TYPO3\CMS\Version\Dependency\ElementEntity
40 */
41 public function getElement($table, $id, array $data = array(), \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency) {
42 /** @var $element ElementEntity */
43 $element = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Version\\Dependency\\ElementEntity', $table, $id, $data, $dependency);
44 $elementName = $element->__toString();
45 if (!isset($this->elements[$elementName])) {
46 $this->elements[$elementName] = $element;
47 }
48 return $this->elements[$elementName];
49 }
50
51 /**
52 * Gets and registers a new reference.
53 *
54 * @param \TYPO3\CMS\Version\Dependency\ElementEntity $element
55 * @param string $field
56 * @return \TYPO3\CMS\Version\Dependency\ReferenceEntity
57 */
58 public function getReference(\TYPO3\CMS\Version\Dependency\ElementEntity $element, $field) {
59 $referenceName = $element->__toString() . '.' . $field;
60 if (!isset($this->references[$referenceName][$field])) {
61 $this->references[$referenceName][$field] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Version\\Dependency\\ReferenceEntity', $element, $field);
62 }
63 return $this->references[$referenceName][$field];
64 }
65
66 /**
67 * Gets and registers a new reference.
68 *
69 * @param string $table
70 * @param integer $id
71 * @param string $field
72 * @param array $data (optional)
73 * @param \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency
74 * @return \TYPO3\CMS\Version\Dependency\ReferenceEntity
75 * @see getElement
76 * @see getReference
77 */
78 public function getReferencedElement($table, $id, $field, array $data = array(), \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency) {
79 return $this->getReference($this->getElement($table, $id, $data, $dependency), $field);
80 }
81
82 }