1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Formz project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\AssetHandler; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Exceptions\ClassNotFoundException; |
17
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentTypeException; |
18
|
|
|
use Romm\Formz\Form\FormObject; |
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
20
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A factory used to get instances of asset handlers. This is useful because it |
24
|
|
|
* will automatically manage cached instances of asset handlers for a given |
25
|
|
|
* form. |
26
|
|
|
* |
27
|
|
|
* The factory is here to store all information which can be useful to the asset |
28
|
|
|
* handlers. |
29
|
|
|
*/ |
30
|
|
|
class AssetHandlerFactory |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Contains the instances of factory for every form/controller context. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected static $factoryInstances = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var AbstractAssetHandler[] |
42
|
|
|
*/ |
43
|
|
|
protected $instances = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var FormObject |
47
|
|
|
*/ |
48
|
|
|
protected $formObject = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ControllerContext |
52
|
|
|
*/ |
53
|
|
|
protected $controllerContext; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param FormObject $formObject Name of the form class. |
57
|
|
|
* @param ControllerContext $controllerContext |
58
|
|
|
*/ |
59
|
|
|
protected function __construct(FormObject $formObject, ControllerContext $controllerContext) |
60
|
|
|
{ |
61
|
|
|
$this->formObject = $formObject; |
62
|
|
|
$this->controllerContext = $controllerContext; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a factory instance. The same instance will be returned for the |
67
|
|
|
* same `$formObject` and `$controllerContext` parameters. |
68
|
|
|
* |
69
|
|
|
* @param FormObject $formObject Configuration of the form. |
70
|
|
|
* @param ControllerContext $controllerContext |
71
|
|
|
* @return AssetHandlerFactory |
72
|
|
|
*/ |
73
|
|
|
public static function get(FormObject $formObject, ControllerContext $controllerContext) |
74
|
|
|
{ |
75
|
|
|
$hash = md5(spl_object_hash($formObject) . spl_object_hash($controllerContext)); |
76
|
|
|
|
77
|
|
|
if (false === array_key_exists($hash, self::$factoryInstances)) { |
78
|
|
|
self::$factoryInstances[$hash] = new self($formObject, $controllerContext); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return self::$factoryInstances[$hash]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return an instance of the wanted asset handler. Local storage is handled. |
86
|
|
|
* |
87
|
|
|
* @param string $className |
88
|
|
|
* @return AbstractAssetHandler |
89
|
|
|
* @throws ClassNotFoundException |
90
|
|
|
* @throws InvalidArgumentTypeException |
91
|
|
|
*/ |
92
|
|
|
public function getAssetHandler($className) |
93
|
|
|
{ |
94
|
|
|
if (false === array_key_exists($className, $this->instances)) { |
95
|
|
|
if (false === class_exists($className)) { |
96
|
|
|
throw ClassNotFoundException::wrongAssetHandlerClassName($className); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$instance = GeneralUtility::makeInstance($className, $this); |
100
|
|
|
|
101
|
|
|
if (false === $instance instanceof AbstractAssetHandler) { |
102
|
|
|
throw InvalidArgumentTypeException::wrongAssetHandlerType(get_class($instance)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->instances[$className] = $instance; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->instances[$className]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return FormObject |
113
|
|
|
*/ |
114
|
|
|
public function getFormObject() |
115
|
|
|
{ |
116
|
|
|
return $this->formObject; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return ControllerContext |
121
|
|
|
*/ |
122
|
|
|
public function getControllerContext() |
123
|
|
|
{ |
124
|
|
|
return $this->controllerContext; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|