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\Attachments\Attachment; |
33
|
|
|
use App\Entity\Base\DBElement; |
34
|
|
|
use App\Entity\Parts\MeasurementUnit; |
35
|
|
|
use App\Entity\PriceInformations\Currency; |
36
|
|
|
use App\Services\AmountFormatter; |
37
|
|
|
use App\Services\Attachments\AttachmentURLGenerator; |
38
|
|
|
use App\Services\EntityURLGenerator; |
39
|
|
|
use App\Services\MarkdownParser; |
40
|
|
|
use App\Services\MoneyFormatter; |
41
|
|
|
use App\Services\SIFormatter; |
42
|
|
|
use App\Services\TreeBuilder; |
43
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
44
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
45
|
|
|
use Twig\Cache\CacheInterface; |
46
|
|
|
use Twig\Extension\AbstractExtension; |
47
|
|
|
use Twig\TwigFilter; |
48
|
|
|
use s9e\TextFormatter\Bundles\Forum as TextFormatter; |
49
|
|
|
use Twig\TwigFunction; |
50
|
|
|
use Twig\TwigTest; |
51
|
|
|
|
52
|
|
|
class AppExtension extends AbstractExtension |
53
|
|
|
{ |
54
|
|
|
protected $entityURLGenerator; |
55
|
|
|
protected $markdownParser; |
56
|
|
|
protected $serializer; |
57
|
|
|
protected $treeBuilder; |
58
|
|
|
protected $moneyFormatter; |
59
|
|
|
protected $siformatter; |
60
|
|
|
protected $amountFormatter; |
61
|
|
|
protected $attachmentURLGenerator; |
62
|
|
|
|
63
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser, |
64
|
|
|
SerializerInterface $serializer, TreeBuilder $treeBuilder, |
65
|
|
|
MoneyFormatter $moneyFormatter, |
66
|
|
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter, |
67
|
|
|
AttachmentURLGenerator $attachmentURLGenerator) |
68
|
|
|
{ |
69
|
|
|
$this->entityURLGenerator = $entityURLGenerator; |
70
|
|
|
$this->markdownParser = $markdownParser; |
71
|
|
|
$this->serializer = $serializer; |
72
|
|
|
$this->treeBuilder = $treeBuilder; |
73
|
|
|
$this->moneyFormatter = $moneyFormatter; |
74
|
|
|
$this->siformatter = $SIFormatter; |
75
|
|
|
$this->amountFormatter = $amountFormatter; |
76
|
|
|
$this->attachmentURLGenerator = $attachmentURLGenerator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getFilters() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
new TwigFilter('entityURL', [$this, 'generateEntityURL']), |
83
|
|
|
new TwigFilter('markdown', [$this->markdownParser, 'parse'], ['pre_escape' => 'html', 'is_safe' => ['html']]), |
84
|
|
|
new TwigFilter('moneyFormat', [$this, 'formatCurrency']), |
85
|
|
|
new TwigFilter('siFormat', [$this, 'siFormat']), |
86
|
|
|
new TwigFilter('amountFormat', [$this, 'amountFormat']), |
87
|
|
|
new TwigFilter('loginPath', [$this, 'loginPath']) |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getTests() |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
new TwigTest('instanceof', function ($var, $instance) { |
95
|
|
|
return $var instanceof $instance; |
96
|
|
|
} ) |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getFunctions() |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
new TwigFunction('generateTreeData', [$this, 'treeData']), |
104
|
|
|
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']) |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function treeData(DBElement $element, string $type = 'newEdit') : string |
109
|
|
|
{ |
110
|
|
|
$tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element); |
111
|
|
|
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* This function/filter generates an path |
116
|
|
|
* @param string $path |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function loginPath(string $path) : string |
120
|
|
|
{ |
121
|
|
|
$parts = explode("/" ,$path); |
122
|
|
|
//Remove the part with |
123
|
|
|
unset($parts[1]); |
124
|
|
|
return implode("/", $parts); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function generateEntityURL(DBElement $entity, string $method = 'info'): string |
128
|
|
|
{ |
129
|
|
|
return $this->entityURLGenerator->getURL($entity, $method); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function formatCurrency($amount, Currency $currency = null, int $decimals = 5) |
133
|
|
|
{ |
134
|
|
|
return $this->moneyFormatter->format($amount, $currency, $decimals); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function siFormat($value, $unit, $decimals = 2, bool $show_all_digits = false) |
138
|
|
|
{ |
139
|
|
|
return $this->siformatter->format($value, $unit, $decimals, $show_all_digits); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = []) |
143
|
|
|
{ |
144
|
|
|
return $this->amountFormatter->format($value, $unit, $options); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.