Completed
Push — master ( 0826f5...8a4d66 )
by Jan
03:23
created

AppExtension::getTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\TreeBuilder;
36
use Symfony\Component\Cache\Adapter\AdapterInterface;
37
use Symfony\Component\Serializer\SerializerInterface;
38
use Twig\Extension\AbstractExtension;
39
use Twig\TwigFilter;
40
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
41
use Twig\TwigFunction;
42
use Twig\TwigTest;
43
44
class AppExtension extends AbstractExtension
45
{
46
    protected $entityURLGenerator;
47
    protected $cache;
48
    protected $serializer;
49
    protected $treeBuilder;
50
51
    public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
52
                                SerializerInterface $serializer, TreeBuilder $treeBuilder)
53
    {
54
        $this->entityURLGenerator = $entityURLGenerator;
55
        $this->cache = $cache;
56
        $this->serializer = $serializer;
57
        $this->treeBuilder = $treeBuilder;
58
    }
59
60
    public function getFilters()
61
    {
62
        return [
63
           new TwigFilter('entityURL', [$this, 'generateEntityURL']),
64
           new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
65
       ];
66
    }
67
68
    public function getTests()
69
    {
70
        return [
71
            new TwigTest('instanceof', function ($var, $instance) {
72
                return $var instanceof $instance;
73
            } )
74
        ];
75
    }
76
77
    public function getFunctions()
78
    {
79
        return [
80
            new TwigFunction('generateTreeData', [$this, 'treeData'])
81
        ];
82
    }
83
84
    public function treeData(DBElement $element, string $type = 'newEdit') : string
85
    {
86
        $tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element);
87
        return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
88
    }
89
90
    public function generateEntityURL(DBElement $entity, string $method = 'info'): string
91
    {
92
        return $this->entityURLGenerator->getURL($entity, $method);
93
    }
94
95
    public function parseBBCode(string $bbcode): string
96
    {
97
        if ('' === $bbcode) {
98
            return '';
99
        }
100
101
        $item = $this->cache->getItem('bbcode_'.md5($bbcode));
102
        if (!$item->isHit()) {
103
            $xml = TextFormatter::parse($bbcode);
104
            $item->set(TextFormatter::render($xml));
105
            $this->cache->save($item);
106
        }
107
108
        return $item->get();
109
    }
110
}
111