Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 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\Yaml\Exception;
13
 
14
/**
15
 * Exception class thrown when an error occurs during parsing.
16
 *
17
 * @author Fabien Potencier <fabien@symfony.com>
18
 */
19
class ParseException extends RuntimeException
20
{
21
    private ?string $parsedFile;
22
    private int $parsedLine;
23
    private ?string $snippet;
24
    private string $rawMessage;
25
 
26
    /**
27
     * @param string      $message    The error message
28
     * @param int         $parsedLine The line where the error occurred
29
     * @param string|null $snippet    The snippet of code near the problem
30
     * @param string|null $parsedFile The file name where the error occurred
31
     */
32
    public function __construct(string $message, int $parsedLine = -1, string $snippet = null, string $parsedFile = null, \Throwable $previous = null)
33
    {
34
        $this->parsedFile = $parsedFile;
35
        $this->parsedLine = $parsedLine;
36
        $this->snippet = $snippet;
37
        $this->rawMessage = $message;
38
 
39
        $this->updateRepr();
40
 
41
        parent::__construct($this->message, 0, $previous);
42
    }
43
 
44
    /**
45
     * Gets the snippet of code near the error.
46
     */
47
    public function getSnippet(): string
48
    {
49
        return $this->snippet;
50
    }
51
 
52
    /**
53
     * Sets the snippet of code near the error.
54
     */
55
    public function setSnippet(string $snippet)
56
    {
57
        $this->snippet = $snippet;
58
 
59
        $this->updateRepr();
60
    }
61
 
62
    /**
63
     * Gets the filename where the error occurred.
64
     *
65
     * This method returns null if a string is parsed.
66
     */
67
    public function getParsedFile(): string
68
    {
69
        return $this->parsedFile;
70
    }
71
 
72
    /**
73
     * Sets the filename where the error occurred.
74
     */
75
    public function setParsedFile(string $parsedFile)
76
    {
77
        $this->parsedFile = $parsedFile;
78
 
79
        $this->updateRepr();
80
    }
81
 
82
    /**
83
     * Gets the line where the error occurred.
84
     */
85
    public function getParsedLine(): int
86
    {
87
        return $this->parsedLine;
88
    }
89
 
90
    /**
91
     * Sets the line where the error occurred.
92
     */
93
    public function setParsedLine(int $parsedLine)
94
    {
95
        $this->parsedLine = $parsedLine;
96
 
97
        $this->updateRepr();
98
    }
99
 
100
    private function updateRepr()
101
    {
102
        $this->message = $this->rawMessage;
103
 
104
        $dot = false;
105
        if (str_ends_with($this->message, '.')) {
106
            $this->message = substr($this->message, 0, -1);
107
            $dot = true;
108
        }
109
 
110
        if (null !== $this->parsedFile) {
111
            $this->message .= sprintf(' in %s', json_encode($this->parsedFile, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
112
        }
113
 
114
        if ($this->parsedLine >= 0) {
115
            $this->message .= sprintf(' at line %d', $this->parsedLine);
116
        }
117
 
118
        if ($this->snippet) {
119
            $this->message .= sprintf(' (near "%s")', $this->snippet);
120
        }
121
 
122
        if ($dot) {
123
            $this->message .= '.';
124
        }
125
    }
126
}