• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 6809777185

09 Nov 2023 09:22AM UTC coverage: 22.835%. Remained the same
6809777185

push

github

koertho
deprecate twig filters and tests

2 of 32 new or added lines in 8 files covered. (6.25%)

4 existing lines in 4 files now uncovered.

1255 of 5496 relevant lines covered (22.83%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Twig/FileExtension.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Twig;
10

11
use Contao\File;
12
use Contao\StringUtil;
13
use Contao\System;
14
use Contao\Validator;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFilter;
19

20
class FileExtension extends AbstractExtension implements ContainerAwareInterface
21
{
22
    use ContainerAwareTrait;
23

24
    const FILE_OBJECT_PROPERTIES = [
25
        'size',
26
        'readableFilesize',
27
        'filesize',
28
        'name',
29
        'basename',
30
        'dirname',
31
        'filename',
32
        'extension',
33
        'origext',
34
        'tmpname',
35
        'path',
36
        'value',
37
        'mime',
38
        'ctime',
39
        'mtime',
40
        'atime',
41
        'icon',
42
        'imageSize',
43
        'width',
44
        'height',
45
        'imageViewSize',
46
        'viewWidth',
47
        'viewHeight',
48
        'isImage',
49
        'isGdImage',
50
        'isSvgImage',
51
        'channels',
52
        'bits',
53
        'isRgbImage',
54
        'isCmykImage',
55
    ];
56

57
    /**
58
     * Get list of twig filters.
59
     *
60
     * @return array|\Twig_SimpleFilter[]
61
     */
62
    public function getFilters()
63
    {
64
        return [
×
NEW
65
            new TwigFilter('file_data', [$this, 'getFileData'], ['deprecated' => true]),
×
NEW
66
            new TwigFilter('file_path', [$this, 'getFilePath'], ['deprecated' => true]),
×
NEW
67
            new TwigFilter('file_content', [$this, 'getFileContent'], ['deprecated' => true]),
×
NEW
68
            new TwigFilter('bin2uuid', [$this, 'convertBinaryToUuid'], ['deprecated' => true]),
×
UNCOV
69
        ];
×
70
    }
71

72
    public function convertBinaryToUuid($binary)
73
    {
74
        if (Validator::isBinaryUuid($binary)) {
×
75
            return StringUtil::binToUuid($binary);
×
76
        }
77

78
        return $binary;
×
79
    }
80

81
    /**
82
     * Get file data based on given uuid.
83
     *
84
     * @param mixed $file                 File uuid
85
     * @param array $data                 Add file data here
86
     * @param array $jsonSerializeOptions Options for the object to array transformation
87
     *
88
     * @return array File data
89
     */
90

91
    /**
92
     * Get file data based on given uuid.
93
     *
94
     * @param mixed $file                 File uuid
95
     * @param array $data                 Add file data here
96
     * @param array $jsonSerializeOptions Options for the object to array transformation
97
     *
98
     * @throws \ReflectionException
99
     *
100
     * @return array File data
101
     */
102
    public function getFileData($file, array $data = [], array $jsonSerializeOptions = []): array
103
    {
104
        if (null === ($fileObj = $this->container->get('huh.utils.file')->getFileFromUuid($file))) {
×
105
            return [];
×
106
        }
107

108
        $fileData = $this->container->get('huh.utils.class')->jsonSerialize($fileObj, $data, array_merge_recursive($jsonSerializeOptions, ['ignoreMethods' => true]));
×
109

110
        foreach (static::FILE_OBJECT_PROPERTIES as $property) {
×
111
            try {
112
                $fileData[$property] = $fileObj->{$property} ?? null;
×
113
            } catch (\Exception $e) {
×
114
                $fileData[$property] = null;
×
115
            }
116
            $fileData['readableFilesize'] = System::getReadableSize($fileObj->filesize, 1);
×
117
        }
118

119
        $fileData['exists'] = $fileObj->exists();
×
120

121
        return array_merge($fileData, $data);
×
122
    }
123

124
    /**
125
     * Get file path based on given uuid.
126
     *
127
     * @param mixed $file File uuid
128
     *
129
     * @return string File path
130
     */
131
    public function getFilePath($file): string
132
    {
133
        if (null === ($fileObj = $this->container->get('huh.utils.file')->getFileFromUuid($file))) {
×
134
            return '';
×
135
        }
136

137
        return $fileObj->path;
×
138
    }
139

140
    public function getFileContent($file)
141
    {
142
        if (Validator::isUuid($file)) {
×
143
            /** @var File $fileObj */
144
            if (null === ($fileObj = $this->container->get('huh.utils.file')->getFileFromUuid($file))) {
×
145
                return '';
×
146
            }
147
        } elseif (\is_string($file)) {
×
148
            $file = str_replace($this->container->getParameter('kernel.project_dir').'/', '', $file);
×
149

150
            if (null === ($fileObj = new File($file)) || !$fileObj->exists()) {
×
151
                return '';
×
152
            }
153
        } else {
154
            return null;
×
155
        }
156

157
        return $fileObj->getContent();
×
158
    }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc