Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Exception / ExceptionPresenter.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Exception;
12
13 use Behat\Testwork\Exception\Stringer\ExceptionStringer;
14 use Behat\Testwork\Output\Printer\OutputPrinter;
15 use Exception;
16
17 /**
18  * Presents exceptions as strings using registered stringers.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class ExceptionPresenter
23 {
24     /**
25      * @var string
26      */
27     private $basePath;
28
29     /**
30      * @var ExceptionStringer[]
31      */
32     private $stringers = array();
33     /**
34      * @var integer
35      */
36     private $defaultVerbosity = OutputPrinter::VERBOSITY_NORMAL;
37
38     /**
39      * Initializes presenter.
40      *
41      * @param string  $basePath
42      * @param integer $defaultVerbosity
43      */
44     public function __construct($basePath = null, $defaultVerbosity = OutputPrinter::VERBOSITY_NORMAL)
45     {
46         if (null !== $basePath) {
47             $realBasePath = realpath($basePath);
48
49             if ($realBasePath) {
50                 $basePath = $realBasePath;
51             }
52         }
53
54         $this->basePath = $basePath;
55         $this->defaultVerbosity = $defaultVerbosity;
56     }
57
58     /**
59      * Registers exception stringer.
60      *
61      * @param ExceptionStringer $stringer
62      */
63     public function registerExceptionStringer(ExceptionStringer $stringer)
64     {
65         $this->stringers[] = $stringer;
66     }
67
68     /**
69      * Sets default verbosity to a specified level.
70      *
71      * @param integer $defaultVerbosity
72      */
73     public function setDefaultVerbosity($defaultVerbosity)
74     {
75         $this->defaultVerbosity = $defaultVerbosity;
76     }
77
78     /**
79      * Presents exception as a string.
80      *
81      * @param Exception $exception
82      * @param integer   $verbosity
83      *
84      * @return string
85      */
86     public function presentException(Exception $exception, $verbosity = null)
87     {
88         $verbosity = $verbosity ?: $this->defaultVerbosity;
89
90         foreach ($this->stringers as $stringer) {
91             if ($stringer->supportsException($exception)) {
92                 return $this->relativizePaths($stringer->stringException($exception, $verbosity));
93             }
94         }
95
96         if (OutputPrinter::VERBOSITY_VERY_VERBOSE <= $verbosity) {
97             return $this->relativizePaths(trim($exception));
98         }
99
100         return trim($this->relativizePaths($exception->getMessage()) . ' (' . get_class($exception) . ')');
101     }
102
103     /**
104      * Relativizes absolute paths in the text.
105      *
106      * @param string $text
107      *
108      * @return string
109      */
110     private function relativizePaths($text)
111     {
112         if (!$this->basePath) {
113             return $text;
114         }
115
116         return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $text);
117     }
118 }