Completed
Push — master ( 928b57...650b38 )
by Jan
03:05
created

AppExtension::treeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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
43
class AppExtension extends AbstractExtension
44
{
45
    protected $entityURLGenerator;
46
    protected $cache;
47
    protected $serializer;
48
    protected $treeBuilder;
49
50
    public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
51
                                SerializerInterface $serializer, TreeBuilder $treeBuilder)
52
    {
53
        $this->entityURLGenerator = $entityURLGenerator;
54
        $this->cache = $cache;
55
        $this->serializer = $serializer;
56
        $this->treeBuilder = $treeBuilder;
57
    }
58
59
    public function getFilters()
60
    {
61
        return [
62
           new TwigFilter('entityURL', [$this, 'generateEntityURL']),
63
           new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
64
       ];
65
    }
66
67
    public function getFunctions()
68
    {
69
        return [
70
            new TwigFunction('generateTreeData', [$this, 'treeData'])
71
        ];
72
    }
73
74
    public function treeData(DBElement $element, string $type = 'newEdit') : string
75
    {
76
        $tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element);
77
        return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
78
    }
79
80
    public function generateEntityURL(DBElement $entity, string $method = 'info'): string
81
    {
82
        return $this->entityURLGenerator->getURL($entity, $method);
83
    }
84
85
    public function parseBBCode(string $bbcode): string
86
    {
87
        if ('' === $bbcode) {
88
            return '';
89
        }
90
91
        $item = $this->cache->getItem('bbcode_'.md5($bbcode));
92
        if (!$item->isHit()) {
93
            $xml = TextFormatter::parse($bbcode);
94
            $item->set(TextFormatter::render($xml));
95
            $this->cache->save($item);
96
        }
97
98
        return $item->get();
99
    }
100
}
101