Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Application modulaire fonctionnelle !

Emmanuel ROY authored on 12/08/2019 15:10:25
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,163 +0,0 @@
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
-        $disableEntities = libxml_disable_entity_loader(false);
65
-
66
-        $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion));
67
-        if (!$isValid) {
68
-            libxml_disable_entity_loader($disableEntities);
69
-
70
-            return self::getXmlErrors($internalErrors);
71
-        }
72
-
73
-        libxml_disable_entity_loader($disableEntities);
74
-
75
-        $dom->normalizeDocument();
76
-
77
-        libxml_clear_errors();
78
-        libxml_use_internal_errors($internalErrors);
79
-
80
-        return [];
81
-    }
82
-
83
-    public static function getErrorsAsString(array $xmlErrors): string
84
-    {
85
-        $errorsAsString = '';
86
-
87
-        foreach ($xmlErrors as $error) {
88
-            $errorsAsString .= sprintf("[%s %s] %s (in %s - line %d, column %d)\n",
89
-                LIBXML_ERR_WARNING === $error['level'] ? 'WARNING' : 'ERROR',
90
-                $error['code'],
91
-                $error['message'],
92
-                $error['file'],
93
-                $error['line'],
94
-                $error['column']
95
-            );
96
-        }
97
-
98
-        return $errorsAsString;
99
-    }
100
-
101
-    private static function getSchema(string $xliffVersion): string
102
-    {
103
-        if ('1.2' === $xliffVersion) {
104
-            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-1.2-strict.xsd');
105
-            $xmlUri = 'http://www.w3.org/2001/xml.xsd';
106
-        } elseif ('2.0' === $xliffVersion) {
107
-            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-2.0.xsd');
108
-            $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
109
-        } else {
110
-            throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
111
-        }
112
-
113
-        return self::fixXmlLocation($schemaSource, $xmlUri);
114
-    }
115
-
116
-    /**
117
-     * Internally changes the URI of a dependent xsd to be loaded locally.
118
-     */
119
-    private static function fixXmlLocation(string $schemaSource, string $xmlUri): string
120
-    {
121
-        $newPath = str_replace('\\', '/', __DIR__).'/../Resources/schemas/xml.xsd';
122
-        $parts = explode('/', $newPath);
123
-        $locationstart = 'file:///';
124
-        if (0 === stripos($newPath, 'phar://')) {
125
-            $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
126
-            if ($tmpfile) {
127
-                copy($newPath, $tmpfile);
128
-                $parts = explode('/', str_replace('\\', '/', $tmpfile));
129
-            } else {
130
-                array_shift($parts);
131
-                $locationstart = 'phar:///';
132
-            }
133
-        }
134
-
135
-        $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
136
-        $newPath = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
137
-
138
-        return str_replace($xmlUri, $newPath, $schemaSource);
139
-    }
140
-
141
-    /**
142
-     * Returns the XML errors of the internal XML parser.
143
-     */
144
-    private static function getXmlErrors(bool $internalErrors): array
145
-    {
146
-        $errors = [];
147
-        foreach (libxml_get_errors() as $error) {
148
-            $errors[] = [
149
-                'level' => LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
150
-                'code' => $error->code,
151
-                'message' => trim($error->message),
152
-                'file' => $error->file ?: 'n/a',
153
-                'line' => $error->line,
154
-                'column' => $error->column,
155
-            ];
156
-        }
157
-
158
-        libxml_clear_errors();
159
-        libxml_use_internal_errors($internalErrors);
160
-
161
-        return $errors;
162
-    }
163
-}
Browse code

initial commit

Emmanuel ROY authored on 09/08/2019 08:39:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,163 @@
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
+        $disableEntities = libxml_disable_entity_loader(false);
65
+
66
+        $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion));
67
+        if (!$isValid) {
68
+            libxml_disable_entity_loader($disableEntities);
69
+
70
+            return self::getXmlErrors($internalErrors);
71
+        }
72
+
73
+        libxml_disable_entity_loader($disableEntities);
74
+
75
+        $dom->normalizeDocument();
76
+
77
+        libxml_clear_errors();
78
+        libxml_use_internal_errors($internalErrors);
79
+
80
+        return [];
81
+    }
82
+
83
+    public static function getErrorsAsString(array $xmlErrors): string
84
+    {
85
+        $errorsAsString = '';
86
+
87
+        foreach ($xmlErrors as $error) {
88
+            $errorsAsString .= sprintf("[%s %s] %s (in %s - line %d, column %d)\n",
89
+                LIBXML_ERR_WARNING === $error['level'] ? 'WARNING' : 'ERROR',
90
+                $error['code'],
91
+                $error['message'],
92
+                $error['file'],
93
+                $error['line'],
94
+                $error['column']
95
+            );
96
+        }
97
+
98
+        return $errorsAsString;
99
+    }
100
+
101
+    private static function getSchema(string $xliffVersion): string
102
+    {
103
+        if ('1.2' === $xliffVersion) {
104
+            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-1.2-strict.xsd');
105
+            $xmlUri = 'http://www.w3.org/2001/xml.xsd';
106
+        } elseif ('2.0' === $xliffVersion) {
107
+            $schemaSource = file_get_contents(__DIR__.'/../Resources/schemas/xliff-core-2.0.xsd');
108
+            $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
109
+        } else {
110
+            throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
111
+        }
112
+
113
+        return self::fixXmlLocation($schemaSource, $xmlUri);
114
+    }
115
+
116
+    /**
117
+     * Internally changes the URI of a dependent xsd to be loaded locally.
118
+     */
119
+    private static function fixXmlLocation(string $schemaSource, string $xmlUri): string
120
+    {
121
+        $newPath = str_replace('\\', '/', __DIR__).'/../Resources/schemas/xml.xsd';
122
+        $parts = explode('/', $newPath);
123
+        $locationstart = 'file:///';
124
+        if (0 === stripos($newPath, 'phar://')) {
125
+            $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
126
+            if ($tmpfile) {
127
+                copy($newPath, $tmpfile);
128
+                $parts = explode('/', str_replace('\\', '/', $tmpfile));
129
+            } else {
130
+                array_shift($parts);
131
+                $locationstart = 'phar:///';
132
+            }
133
+        }
134
+
135
+        $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
136
+        $newPath = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
137
+
138
+        return str_replace($xmlUri, $newPath, $schemaSource);
139
+    }
140
+
141
+    /**
142
+     * Returns the XML errors of the internal XML parser.
143
+     */
144
+    private static function getXmlErrors(bool $internalErrors): array
145
+    {
146
+        $errors = [];
147
+        foreach (libxml_get_errors() as $error) {
148
+            $errors[] = [
149
+                'level' => LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
150
+                'code' => $error->code,
151
+                'message' => trim($error->message),
152
+                'file' => $error->file ?: 'n/a',
153
+                'line' => $error->line,
154
+                'column' => $error->column,
155
+            ];
156
+        }
157
+
158
+        libxml_clear_errors();
159
+        libxml_use_internal_errors($internalErrors);
160
+
161
+        return $errors;
162
+    }
163
+}