21
21
*/
22
22
class ExceptionCaster
23
23
{
24
+ public static $ srcContext = 1 ;
24
25
public static $ traceArgs = true ;
25
26
public static $ errorTypes = array (
26
27
E_DEPRECATED => 'E_DEPRECATED ' ,
@@ -71,7 +72,7 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
71
72
'file ' => $ b [$ prefix .'file ' ],
72
73
'line ' => $ b [$ prefix .'line ' ],
73
74
));
74
- $ a [$ xPrefix .'trace ' ] = new TraceStub ($ b [$ xPrefix .'trace ' ], 1 , false , 0 , -1 - count ($ a [$ xPrefix .'trace ' ]->value ));
75
+ $ a [$ xPrefix .'trace ' ] = new TraceStub ($ b [$ xPrefix .'trace ' ], false , 0 , -1 - count ($ a [$ xPrefix .'trace ' ]->value ));
75
76
}
76
77
77
78
unset($ a [$ xPrefix .'previous ' ], $ a [$ prefix .'code ' ], $ a [$ prefix .'file ' ], $ a [$ prefix .'line ' ]);
@@ -90,15 +91,15 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
90
91
91
92
$ a = array ();
92
93
$ j = count ($ frames );
93
- if (0 > $ i = $ trace ->offset ) {
94
+ if (0 > $ i = $ trace ->sliceOffset ) {
94
95
$ i = max (0 , $ j + $ i );
95
96
}
96
97
if (!isset ($ trace ->value [$ i ])) {
97
98
return array ();
98
99
}
99
100
$ lastCall = isset ($ frames [$ i ]['function ' ]) ? ' ==> ' .(isset ($ frames [$ i ]['class ' ]) ? $ frames [0 ]['class ' ].$ frames [$ i ]['type ' ] : '' ).$ frames [$ i ]['function ' ].'() ' : '' ;
100
101
101
- for ($ j -= $ i ++; isset ($ frames [$ i ]); ++$ i , --$ j ) {
102
+ for ($ j += $ trace -> numberingOffset - $ i ++; isset ($ frames [$ i ]); ++$ i , --$ j ) {
102
103
$ call = isset ($ frames [$ i ]['function ' ]) ? (isset ($ frames [$ i ]['class ' ]) ? $ frames [$ i ]['class ' ].$ frames [$ i ]['type ' ] : '' ).$ frames [$ i ]['function ' ].'() ' : '??? ' ;
103
104
104
105
$ a [Caster::PREFIX_VIRTUAL .$ j .'. ' .$ call .$ lastCall ] = new FrameStub (
@@ -108,7 +109,6 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
108
109
'type ' => isset ($ frames [$ i ]['type ' ]) ? $ frames [$ i ]['type ' ] : null ,
109
110
'function ' => isset ($ frames [$ i ]['function ' ]) ? $ frames [$ i ]['function ' ] : null ,
110
111
) + $ frames [$ i - 1 ],
111
- $ trace ->srcContext ,
112
112
$ trace ->keepArgs ,
113
113
true
114
114
);
@@ -122,12 +122,11 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
122
122
'type ' => null ,
123
123
'function ' => '{main} ' ,
124
124
) + $ frames [$ i - 1 ],
125
- $ trace ->srcContext ,
126
125
$ trace ->keepArgs ,
127
126
true
128
127
);
129
- if (null !== $ trace ->length ) {
130
- $ a = array_slice ($ a , 0 , $ trace ->length , true );
128
+ if (null !== $ trace ->sliceLength ) {
129
+ $ a = array_slice ($ a , 0 , $ trace ->sliceLength , true );
131
130
}
132
131
133
132
return $ a ;
@@ -146,8 +145,8 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
146
145
$ f ['file ' ] = substr ($ f ['file ' ], 0 , -strlen ($ match [0 ]));
147
146
$ f ['line ' ] = (int ) $ match [1 ];
148
147
}
149
- if (file_exists ($ f ['file ' ]) && 0 <= $ frame -> srcContext ) {
150
- $ src [$ f ['file ' ].': ' .$ f ['line ' ]] = self ::extractSource (explode ("\n" , file_get_contents ($ f ['file ' ])), $ f ['line ' ], $ frame -> srcContext );
148
+ if (file_exists ($ f ['file ' ]) && 0 <= self :: $ srcContext ) {
149
+ $ src [$ f ['file ' ].': ' .$ f ['line ' ]] = self ::extractSource (explode ("\n" , file_get_contents ($ f ['file ' ])), $ f ['line ' ], self :: $ srcContext );
151
150
152
151
if (!empty ($ f ['class ' ]) && is_subclass_of ($ f ['class ' ], 'Twig_Template ' ) && method_exists ($ f ['class ' ], 'getDebugInfo ' )) {
153
152
$ template = isset ($ f ['object ' ]) ? $ f ['object ' ] : new $ f ['class ' ](new \Twig_Environment (new \Twig_Loader_Filesystem ()));
@@ -157,7 +156,7 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
157
156
$ templateSrc = explode ("\n" , method_exists ($ template , 'getSource ' ) ? $ template ->getSource () : $ template ->getEnvironment ()->getLoader ()->getSource ($ templateName ));
158
157
$ templateInfo = $ template ->getDebugInfo ();
159
158
if (isset ($ templateInfo [$ f ['line ' ]])) {
160
- $ src [$ templateName .': ' .$ templateInfo [$ f ['line ' ]]] = self ::extractSource ($ templateSrc , $ templateInfo [$ f ['line ' ]], $ frame -> srcContext );
159
+ $ src [$ templateName .': ' .$ templateInfo [$ f ['line ' ]]] = self ::extractSource ($ templateSrc , $ templateInfo [$ f ['line ' ]], self :: $ srcContext );
161
160
}
162
161
} catch (\Twig_Error_Loader $ e ) {
163
162
}
@@ -247,15 +246,29 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
247
246
248
247
private static function extractSource (array $ srcArray , $ line , $ srcContext )
249
248
{
250
- $ src = '' ;
249
+ $ src = array () ;
251
250
252
251
for ($ i = $ line - 1 - $ srcContext ; $ i <= $ line - 1 + $ srcContext ; ++$ i ) {
253
- $ src . = (isset ($ srcArray [$ i ]) ? $ srcArray [$ i ] : '' )."\n" ;
252
+ $ src[] = (isset ($ srcArray [$ i ]) ? $ srcArray [$ i ] : '' )."\n" ;
254
253
}
255
- if (!$ srcContext ) {
256
- $ src = trim ($ src );
254
+
255
+ $ ltrim = 0 ;
256
+ while (' ' === $ src [0 ][$ ltrim ] || "\t" === $ src [0 ][$ ltrim ]) {
257
+ $ i = $ srcContext << 1 ;
258
+ while ($ i > 0 && $ src [0 ][$ ltrim ] === $ src [$ i ][$ ltrim ]) {
259
+ --$ i ;
260
+ }
261
+ if ($ i ) {
262
+ break ;
263
+ }
264
+ ++$ ltrim ;
265
+ }
266
+ if ($ ltrim ) {
267
+ foreach ($ src as $ i => $ line ) {
268
+ $ src [$ i ] = substr ($ line , $ ltrim );
269
+ }
257
270
}
258
271
259
- return $ src ;
272
+ return implode ( '' , $ src) ;
260
273
}
261
274
}
0 commit comments