Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util\Xml;
11
 
12
use function chdir;
13
use function dirname;
14
use function error_reporting;
15
use function file_get_contents;
16
use function getcwd;
17
use function libxml_get_errors;
18
use function libxml_use_internal_errors;
19
use function sprintf;
20
use DOMDocument;
21
 
22
/**
23
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
24
 */
25
final class Loader
26
{
27
    /**
28
     * @throws Exception
29
     */
30
    public function loadFile(string $filename, bool $isHtml = false, bool $xinclude = false, bool $strict = false): DOMDocument
31
    {
32
        $reporting = error_reporting(0);
33
        $contents  = file_get_contents($filename);
34
 
35
        error_reporting($reporting);
36
 
37
        if ($contents === false) {
38
            throw new Exception(
39
                sprintf(
40
                    'Could not read "%s".',
41
                    $filename
42
                )
43
            );
44
        }
45
 
46
        return $this->load($contents, $isHtml, $filename, $xinclude, $strict);
47
    }
48
 
49
    /**
50
     * @throws Exception
51
     */
52
    public function load(string $actual, bool $isHtml = false, string $filename = '', bool $xinclude = false, bool $strict = false): DOMDocument
53
    {
54
        if ($actual === '') {
55
            throw new Exception('Could not load XML from empty string');
56
        }
57
 
58
        // Required for XInclude on Windows.
59
        if ($xinclude) {
60
            $cwd = getcwd();
61
            @chdir(dirname($filename));
62
        }
63
 
64
        $document                     = new DOMDocument;
65
        $document->preserveWhiteSpace = false;
66
 
67
        $internal  = libxml_use_internal_errors(true);
68
        $message   = '';
69
        $reporting = error_reporting(0);
70
 
71
        if ($filename !== '') {
72
            // Required for XInclude
73
            $document->documentURI = $filename;
74
        }
75
 
76
        if ($isHtml) {
77
            $loaded = $document->loadHTML($actual);
78
        } else {
79
            $loaded = $document->loadXML($actual);
80
        }
81
 
82
        if (!$isHtml && $xinclude) {
83
            $document->xinclude();
84
        }
85
 
86
        foreach (libxml_get_errors() as $error) {
87
            $message .= "\n" . $error->message;
88
        }
89
 
90
        libxml_use_internal_errors($internal);
91
        error_reporting($reporting);
92
 
93
        if (isset($cwd)) {
94
            @chdir($cwd);
95
        }
96
 
97
        if ($loaded === false || ($strict && $message !== '')) {
98
            if ($filename !== '') {
99
                throw new Exception(
100
                    sprintf(
101
                        'Could not load "%s".%s',
102
                        $filename,
103
                        $message !== '' ? "\n" . $message : ''
104
                    )
105
                );
106
            }
107
 
108
            if ($message === '') {
109
                $message = 'Could not load XML for unknown reason';
110
            }
111
 
112
            throw new Exception($message);
113
        }
114
 
115
        return $document;
116
    }
117
}