@@ -31,6 +31,8 @@ class NormalizerFormatter implements FormatterInterface
3131
3232 private int $ jsonEncodeOptions = Utils::DEFAULT_JSON_FLAGS ;
3333
34+ protected string $ basePath = '' ;
35+
3436 /**
3537 * @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
3638 * @throws \RuntimeException If the function json_encode does not exist
@@ -140,6 +142,21 @@ public function setJsonPrettyPrint(bool $enable): self
140142 return $ this ;
141143 }
142144
145+ /**
146+ * Setting a base path will hide the base path from exception and stack trace file names to shorten them
147+ * @return $this
148+ */
149+ public function setBasePath (string $ path = '' ): self
150+ {
151+ if ($ path !== '' ) {
152+ $ path = rtrim ($ path , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
153+ }
154+
155+ $ this ->basePath = $ path ;
156+
157+ return $ this ;
158+ }
159+
143160 /**
144161 * Provided as extension point
145162 *
@@ -247,11 +264,16 @@ protected function normalizeException(Throwable $e, int $depth = 0)
247264 return (array ) $ e ->jsonSerialize ();
248265 }
249266
267+ $ file = $ e ->getFile ();
268+ if ($ this ->basePath !== '' ) {
269+ $ file = preg_replace ('{^ ' .preg_quote ($ this ->basePath ).'} ' , '' , $ file );
270+ }
271+
250272 $ data = [
251273 'class ' => Utils::getClass ($ e ),
252274 'message ' => $ e ->getMessage (),
253275 'code ' => (int ) $ e ->getCode (),
254- 'file ' => $ e -> getFile () .': ' .$ e ->getLine (),
276+ 'file ' => $ file .': ' .$ e ->getLine (),
255277 ];
256278
257279 if ($ e instanceof \SoapFault) {
@@ -275,7 +297,11 @@ protected function normalizeException(Throwable $e, int $depth = 0)
275297 $ trace = $ e ->getTrace ();
276298 foreach ($ trace as $ frame ) {
277299 if (isset ($ frame ['file ' ], $ frame ['line ' ])) {
278- $ data ['trace ' ][] = $ frame ['file ' ].': ' .$ frame ['line ' ];
300+ $ file = $ frame ['file ' ];
301+ if ($ this ->basePath !== '' ) {
302+ $ file = preg_replace ('{^ ' .preg_quote ($ this ->basePath ).'} ' , '' , $ file );
303+ }
304+ $ data ['trace ' ][] = $ file .': ' .$ frame ['line ' ];
279305 }
280306 }
281307
0 commit comments