Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Translation\Util;
13
 
14
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15
use Symfony\Component\Translation\Exception\InvalidResourceException;
16
 
17
/**
18
 * Provides some utility methods for XLIFF translation files, such as validating
19
 * their contents according to the XSD schema.
20
 *
21
 * @author Fabien Potencier <fabien@symfony.com>
22
 */
23
class XliffUtils
24
{
25
    /**
26
     * Gets xliff file version based on the root "version" attribute.
27
     *
28
     * Defaults to 1.2 for backwards compatibility.
29
     *
30
     * @throws InvalidArgumentException
31
     */
32
    public static function getVersionNumber(\DOMDocument $dom): string
33
    {
34
        /** @var \DOMNode $xliff */
35
        foreach ($dom->getElementsByTagName('xliff') as $xliff) {
36
            $version = $xliff->attributes->getNamedItem('version');
37
            if ($version) {
38
                return $version->nodeValue;
39
            }
40
 
41
            $namespace = $xliff->attributes->getNamedItem('xmlns');
42
            if ($namespace) {
43
                if (0 !== substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) {
44
                    throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s".', $namespace));
45
                }
46
 
47
                return substr($namespace, 34);
48
            }
49
        }
50
 
51
        // Falls back to v1.2
52
        return '1.2';
53
    }
54
 
55
    /**
56
     * Validates and parses the given file into a DOMDocument.
57
     *
58
     * @throws InvalidResourceException
59
     */
60
    public static function validateSchema(\DOMDocument $dom): array
61
    {
62
        $xliffVersion = static::getVersionNumber($dom);
63
        $internalErrors = libxml_use_internal_errors(true);
64
        if ($shouldEnable = self::shouldEnableEntityLoader()) {
65
            $disableEntities = libxml_disable_entity_loader(false);
66
        }
67
        try {
68
            $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion));
69
            if (!$isValid) {
70
                return self::getXmlErrors($internalErrors);
71
            }
72
        } finally {
73
            if ($shouldEnable) {
74
                libxml_disable_entity_loader($disableEntities);
75
            }
76
        }
77
 
78
        $dom->normalizeDocument();
79
 
80
        libxml_clear_errors();
81
        libxml_use_internal_errors($internalErrors);
82
 
83
        return [];
84
    }
85
 
86
    private static function shouldEnableEntityLoader(): bool
87
    {
88
        static $dom, $schema;
89
        if (null === $dom) {
90
            $dom = new \DOMDocument();
91
            $dom->loadXML('<?xml version="1.0"?><test/>');
92
 
93
            $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
94
            register_shutdown_function(static function () use ($tmpfile) {
95
                @unlink($tmpfile);
96
            });
97
            $schema = '<?xml version="1.0" encoding="utf-8"?>
98
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
99
  <xsd:include schemaLocation="file:///'.str_replace('\\', '/', $tmpfile).'" />
100
</xsd:schema>';
101
            file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?>
102
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
103
  <xsd:element name="test" type="testType" />
104
  <xsd:complexType name="testType"/>
105
</xsd:schema>');
106
        }
107
 
108
        return !@$dom->schemaValidateSource($schema);
109
    }
110
 
111
    public static function getErrorsAsString(array $xmlErrors): string
112
    {
113
        $errorsAsString = '';
114
 
115
        foreach ($xmlErrors as $error) {
116
            $errorsAsString .= sprintf("[%s %s] %s (in %s - line %d, column %d)\n",
117
                \LIBXML_ERR_WARNING === $error['level'] ? 'WARNING' : 'ERROR',
118
                $error['code'],
119
                $error['message'],
120
                $error['file'],
121
                $error['line'],
122
                $error['column']
123
            );
124
        }
125
 
126
        return $errorsAsString;
127
    }
128
 
129
    private static function getSchema(string $xliffVersion): string
130
    {
131
        if ('1.2' === $xliffVersion) {
132
            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-1.2-transitional.xsd');
133
            $xmlUri = 'http://www.w3.org/2001/xml.xsd';
134
        } elseif ('2.0' === $xliffVersion) {
135
            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-2.0.xsd');
136
            $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
137
        } else {
138
            throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
139
        }
140
 
141
        return self::fixXmlLocation($schemaSource, $xmlUri);
142
    }
143
 
144
    /**
145
     * Internally changes the URI of a dependent xsd to be loaded locally.
146
     */
147
    private static function fixXmlLocation(string $schemaSource, string $xmlUri): string
148
    {
149
        $newPath = str_replace('\\', '/', __DIR__).'/../Resources/schemas/xml.xsd';
150
        $parts = explode('/', $newPath);
151
        $locationstart = 'file:///';
152
        if (0 === stripos($newPath, 'phar://')) {
153
            $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
154
            if ($tmpfile) {
155
                copy($newPath, $tmpfile);
156
                $parts = explode('/', str_replace('\\', '/', $tmpfile));
157
            } else {
158
                array_shift($parts);
159
                $locationstart = 'phar:///';
160
            }
161
        }
162
 
163
        $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
164
        $newPath = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
165
 
166
        return str_replace($xmlUri, $newPath, $schemaSource);
167
    }
168
 
169
    /**
170
     * Returns the XML errors of the internal XML parser.
171
     */
172
    private static function getXmlErrors(bool $internalErrors): array
173
    {
174
        $errors = [];
175
        foreach (libxml_get_errors() as $error) {
176
            $errors[] = [
177
                'level' => \LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
178
                'code' => $error->code,
179
                'message' => trim($error->message),
180
                'file' => $error->file ?: 'n/a',
181
                'line' => $error->line,
182
                'column' => $error->column,
183
            ];
184
        }
185
 
186
        libxml_clear_errors();
187
        libxml_use_internal_errors($internalErrors);
188
 
189
        return $errors;
190
    }
191
}