1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* part-db version 0.1 |
4
|
|
|
* Copyright (C) 2005 Christoph Lechner |
5
|
|
|
* http://www.cl-projects.de/. |
6
|
|
|
* |
7
|
|
|
* part-db version 0.2+ |
8
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
9
|
|
|
* http://code.google.com/p/part-db/ |
10
|
|
|
* |
11
|
|
|
* Part-DB Version 0.4+ |
12
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
13
|
|
|
* https://github.com/jbtronics |
14
|
|
|
* |
15
|
|
|
* This program is free software; you can redistribute it and/or |
16
|
|
|
* modify it under the terms of the GNU General Public License |
17
|
|
|
* as published by the Free Software Foundation; either version 2 |
18
|
|
|
* of the License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU General Public License |
26
|
|
|
* along with this program; if not, write to the Free Software |
27
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace App\Twig; |
31
|
|
|
|
32
|
|
|
use App\Entity\Attachment; |
33
|
|
|
use App\Entity\DBElement; |
34
|
|
|
use App\Services\EntityURLGenerator; |
35
|
|
|
use App\Services\MoneyFormatter; |
36
|
|
|
use App\Services\TreeBuilder; |
37
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
38
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
39
|
|
|
use Twig\Extension\AbstractExtension; |
40
|
|
|
use Twig\TwigFilter; |
41
|
|
|
use s9e\TextFormatter\Bundles\Forum as TextFormatter; |
42
|
|
|
use Twig\TwigFunction; |
43
|
|
|
use Twig\TwigTest; |
44
|
|
|
|
45
|
|
|
class AppExtension extends AbstractExtension |
46
|
|
|
{ |
47
|
|
|
protected $entityURLGenerator; |
48
|
|
|
protected $cache; |
49
|
|
|
protected $serializer; |
50
|
|
|
protected $treeBuilder; |
51
|
|
|
protected $moneyFormatter; |
52
|
|
|
|
53
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache, |
54
|
|
|
SerializerInterface $serializer, TreeBuilder $treeBuilder, |
55
|
|
|
MoneyFormatter $moneyFormatter) |
56
|
|
|
{ |
57
|
|
|
$this->entityURLGenerator = $entityURLGenerator; |
58
|
|
|
$this->cache = $cache; |
59
|
|
|
$this->serializer = $serializer; |
60
|
|
|
$this->treeBuilder = $treeBuilder; |
61
|
|
|
$this->moneyFormatter = $moneyFormatter; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getFilters() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
new TwigFilter('entityURL', [$this, 'generateEntityURL']), |
68
|
|
|
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]), |
69
|
|
|
new TwigFilter('moneyFormat', [$this, 'formatCurrency']) |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getTests() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
new TwigTest('instanceof', function ($var, $instance) { |
77
|
|
|
return $var instanceof $instance; |
78
|
|
|
} ) |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getFunctions() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
new TwigFunction('generateTreeData', [$this, 'treeData']) |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function treeData(DBElement $element, string $type = 'newEdit') : string |
90
|
|
|
{ |
91
|
|
|
$tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element); |
92
|
|
|
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function generateEntityURL(DBElement $entity, string $method = 'info'): string |
96
|
|
|
{ |
97
|
|
|
return $this->entityURLGenerator->getURL($entity, $method); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function parseBBCode(string $bbcode): string |
101
|
|
|
{ |
102
|
|
|
if ('' === $bbcode) { |
103
|
|
|
return ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$item = $this->cache->getItem('bbcode_'.md5($bbcode)); |
107
|
|
|
if (!$item->isHit()) { |
108
|
|
|
$xml = TextFormatter::parse($bbcode); |
109
|
|
|
$item->set(TextFormatter::render($xml)); |
110
|
|
|
$this->cache->save($item); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $item->get(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function formatCurrency($amount, $currency = "") |
117
|
|
|
{ |
118
|
|
|
return $this->moneyFormatter->format($amount, $currency); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|