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\Condition\Parser; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Condition\Activation\ActivationInterface; |
17
|
|
|
use Romm\Formz\Service\CacheService; |
18
|
|
|
use Romm\Formz\Service\Traits\FacadeInstanceTrait; |
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Factory class allowing to parse a condition to get an instance of |
23
|
|
|
* `ConditionTree`. |
24
|
|
|
*/ |
25
|
|
|
class ConditionParserFactory implements SingletonInterface |
26
|
|
|
{ |
27
|
|
|
use FacadeInstanceTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ConditionTree[] |
31
|
|
|
*/ |
32
|
|
|
private $trees = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Will parse a condition expression, to build a tree containing one or |
36
|
|
|
* several nodes which represent the condition. See the class |
37
|
|
|
* `ConditionTree` for more information. |
38
|
|
|
* |
39
|
|
|
* @param ActivationInterface $activation |
40
|
|
|
* @return ConditionTree |
41
|
|
|
*/ |
42
|
|
|
public function parse(ActivationInterface $activation) |
43
|
|
|
{ |
44
|
|
|
$hash = 'condition-tree-' . |
45
|
|
|
sha1(serialize([ |
46
|
|
|
$activation->getExpression(), |
47
|
|
|
$activation->getConditions() |
48
|
|
|
])); |
49
|
|
|
|
50
|
|
|
if (false === array_key_exists($hash, $this->trees)) { |
51
|
|
|
$this->trees[$hash] = $this->getConditionTree($hash, $activation); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->trees[$hash]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $cacheIdentifier |
59
|
|
|
* @param ActivationInterface $activation |
60
|
|
|
* @return ConditionTree |
61
|
|
|
*/ |
62
|
|
|
protected function getConditionTree($cacheIdentifier, ActivationInterface $activation) |
63
|
|
|
{ |
64
|
|
|
$cacheInstance = CacheService::get()->getCacheInstance(); |
65
|
|
|
|
66
|
|
|
/** @var ConditionTree $instance */ |
67
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
68
|
|
|
$instance = $cacheInstance->get($cacheIdentifier); |
69
|
|
|
} else { |
70
|
|
|
$instance = $this->buildConditionTree($activation); |
71
|
|
|
$cacheInstance->set($cacheIdentifier, $instance); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $instance; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ActivationInterface $activation |
79
|
|
|
* @return ConditionTree |
80
|
|
|
*/ |
81
|
|
|
protected function buildConditionTree(ActivationInterface $activation) |
82
|
|
|
{ |
83
|
|
|
return ConditionParser::get()->parse($activation); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|