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\Middleware; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
17
|
|
|
use Romm\Formz\Exceptions\ClassNotFoundException; |
18
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentTypeException; |
19
|
|
|
use Romm\Formz\Middleware\Option\AbstractOptionDefinition; |
20
|
|
|
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait; |
21
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
22
|
|
|
use TYPO3\CMS\Extbase\Reflection\ReflectionService; |
23
|
|
|
|
24
|
|
|
class MiddlewareFactory implements SingletonInterface |
25
|
|
|
{ |
26
|
|
|
use ExtendedSelfInstantiateTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ReflectionService |
30
|
|
|
*/ |
31
|
|
|
protected $reflectionService; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ReflectionService $reflectionService |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ReflectionService $reflectionService) |
37
|
|
|
{ |
38
|
|
|
$this->reflectionService = $reflectionService; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $className |
43
|
|
|
* @param callable $optionsCallback |
44
|
|
|
* @return MiddlewareInterface |
45
|
|
|
* @throws ClassNotFoundException |
46
|
|
|
* @throws InvalidArgumentTypeException |
47
|
|
|
*/ |
48
|
|
|
public function create($className, callable $optionsCallback = null) |
49
|
|
|
{ |
50
|
|
|
if (false === class_exists($className)) { |
51
|
|
|
throw ClassNotFoundException::middlewareClassNameNotFound($className); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (false === in_array(MiddlewareInterface::class, class_implements($className))) { |
55
|
|
|
throw InvalidArgumentTypeException::middlewareWrongClassName($className); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$optionsType = $this->getOptionsType($className); |
59
|
|
|
$options = Core::instantiate($optionsType); |
60
|
|
|
|
61
|
|
|
if (is_callable($optionsCallback)) { |
62
|
|
|
call_user_func($optionsCallback, $options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @var MiddlewareInterface $middleware */ |
66
|
|
|
$middleware = Core::instantiate($className, $options); |
67
|
|
|
|
68
|
|
|
return $middleware; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $className |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getOptionsType($className) |
76
|
|
|
{ |
77
|
|
|
$property = $this->reflectionService->getClassSchema($className)->getProperty('options'); |
78
|
|
|
$optionsType = $property['type']; |
79
|
|
|
|
80
|
|
|
if (false === class_exists($optionsType)) { |
81
|
|
|
throw new \Exception('todo'); // todo exception |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (false === in_array(AbstractOptionDefinition::class, class_parents($optionsType))) { |
85
|
|
|
throw new \Exception('todo'); // todo exception |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $optionsType; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|