|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2016 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\Core\Core; |
|
18
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
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
|
|
|
/** |
|
28
|
|
|
* @var ConditionParserFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $instance; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ConditionTree[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $trees = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return ConditionParserFactory |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function get() |
|
41
|
|
|
{ |
|
42
|
|
|
if (null === self::$instance) { |
|
43
|
|
|
self::$instance = GeneralUtility::makeInstance(self::class); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return self::$instance; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Will parse a condition expression, to build a tree containing one or |
|
51
|
|
|
* several nodes which represent the condition. See the class |
|
52
|
|
|
* `ConditionTree` for more information. |
|
53
|
|
|
* |
|
54
|
|
|
* @param ActivationInterface $condition The condition instance. |
|
55
|
|
|
* @return ConditionTree |
|
56
|
|
|
*/ |
|
57
|
|
|
public function parse(ActivationInterface $condition) |
|
58
|
|
|
{ |
|
59
|
|
|
$hash = 'condition-tree-' . |
|
60
|
|
|
sha1(serialize([ |
|
61
|
|
|
$condition->getCondition(), |
|
62
|
|
|
$condition->getItems() |
|
63
|
|
|
])); |
|
64
|
|
|
|
|
65
|
|
|
if (false === array_key_exists($hash, $this->trees)) { |
|
66
|
|
|
$this->trees[$hash] = $this->getConditionTree($hash, $condition); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->trees[$hash]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $cacheIdentifier |
|
74
|
|
|
* @param ActivationInterface $condition |
|
75
|
|
|
* @return ConditionTree |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getConditionTree($cacheIdentifier, ActivationInterface $condition) |
|
78
|
|
|
{ |
|
79
|
|
|
$cacheInstance = Core::get()->getCacheInstance(); |
|
80
|
|
|
|
|
81
|
|
|
/** @var ConditionTree $instance */ |
|
82
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
|
83
|
|
|
$instance = $cacheInstance->get($cacheIdentifier); |
|
84
|
|
|
} else { |
|
85
|
|
|
$instance = ConditionParser::get()->parse($condition); |
|
86
|
|
|
$cacheInstance->set($cacheIdentifier, $instance); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $instance; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|