Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
e54627f6
Commit
e54627f6
authored
Nov 04, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved exception wrapper to its own class
parent
5fefc7dd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
38 deletions
+78
-38
src/Silex/Application.php
src/Silex/Application.php
+1
-38
src/Silex/ExceptionListenerWrapper.php
src/Silex/ExceptionListenerWrapper.php
+77
-0
No files found.
src/Silex/Application.php
View file @
e54627f6
...
...
@@ -14,8 +14,6 @@ namespace Silex;
use
Symfony\Component\HttpKernel\HttpKernel
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
use
Symfony\Component\HttpKernel\TerminableInterface
;
use
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Symfony\Component\HttpKernel\Event\FilterResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
...
...
@@ -332,42 +330,7 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
*/
public
function
error
(
$callback
,
$priority
=
-
8
)
{
$app
=
$this
;
$this
[
'dispatcher'
]
->
addListener
(
KernelEvents
::
EXCEPTION
,
function
(
GetResponseForExceptionEvent
$event
)
use
(
$callback
,
$app
)
{
$exception
=
$event
->
getException
();
if
(
is_array
(
$callback
))
{
$callbackReflection
=
new
\ReflectionMethod
(
$callback
[
0
],
$callback
[
1
]);
}
elseif
(
is_object
(
$callback
)
&&
!
$callback
instanceof
\Closure
)
{
$callbackReflection
=
new
\ReflectionObject
(
$callback
);
$callbackReflection
=
$callbackReflection
->
getMethod
(
'__invoke'
);
}
else
{
$callbackReflection
=
new
\ReflectionFunction
(
$callback
);
}
if
(
$callbackReflection
->
getNumberOfParameters
()
>
0
)
{
$parameters
=
$callbackReflection
->
getParameters
();
$expectedException
=
$parameters
[
0
];
if
(
$expectedException
->
getClass
()
&&
!
$expectedException
->
getClass
()
->
isInstance
(
$exception
))
{
return
;
}
}
$code
=
$exception
instanceof
HttpExceptionInterface
?
$exception
->
getStatusCode
()
:
500
;
$response
=
call_user_func
(
$callback
,
$exception
,
$code
);
if
(
$response
instanceof
Response
)
{
$event
->
setResponse
(
$response
);
}
else
{
$viewEvent
=
new
GetResponseForControllerResultEvent
(
$app
[
'kernel'
],
$event
->
getRequest
(),
$event
->
getRequestType
(),
$response
);
$app
[
'dispatcher'
]
->
dispatch
(
KernelEvents
::
VIEW
,
$viewEvent
);
if
(
$viewEvent
->
hasResponse
())
{
$event
->
setResponse
(
$viewEvent
->
getResponse
());
}
}
},
$priority
);
$this
[
'dispatcher'
]
->
addListener
(
KernelEvents
::
EXCEPTION
,
new
ExceptionListenerWrapper
(
$this
,
$callback
),
$priority
);
}
/**
...
...
src/Silex/ExceptionListenerWrapper.php
0 → 100644
View file @
e54627f6
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Silex\Application
;
/**
* Wraps exception listeners.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
ExceptionListenerWrapper
{
protected
$app
;
protected
$callback
;
/**
* Constructor.
*
* @param Application $app An Application instance
*/
public
function
__construct
(
Application
$app
,
$callback
)
{
$this
->
app
=
$app
;
$this
->
callback
=
$callback
;
}
public
function
__invoke
(
GetResponseForExceptionEvent
$event
)
{
$exception
=
$event
->
getException
();
if
(
is_array
(
$this
->
callback
))
{
$callbackReflection
=
new
\ReflectionMethod
(
$this
->
callback
[
0
],
$this
->
callback
[
1
]);
}
elseif
(
is_object
(
$this
->
callback
)
&&
!
$this
->
callback
instanceof
\Closure
)
{
$callbackReflection
=
new
\ReflectionObject
(
$this
->
callback
);
$callbackReflection
=
$callbackReflection
->
getMethod
(
'__invoke'
);
}
else
{
$callbackReflection
=
new
\ReflectionFunction
(
$this
->
callback
);
}
if
(
$callbackReflection
->
getNumberOfParameters
()
>
0
)
{
$parameters
=
$callbackReflection
->
getParameters
();
$expectedException
=
$parameters
[
0
];
if
(
$expectedException
->
getClass
()
&&
!
$expectedException
->
getClass
()
->
isInstance
(
$exception
))
{
return
;
}
}
$code
=
$exception
instanceof
HttpExceptionInterface
?
$exception
->
getStatusCode
()
:
500
;
$response
=
call_user_func
(
$this
->
callback
,
$exception
,
$code
);
if
(
$response
instanceof
Response
)
{
$event
->
setResponse
(
$response
);
}
else
{
$viewEvent
=
new
GetResponseForControllerResultEvent
(
$this
->
app
[
'kernel'
],
$event
->
getRequest
(),
$event
->
getRequestType
(),
$response
);
$this
->
app
[
'dispatcher'
]
->
dispatch
(
KernelEvents
::
VIEW
,
$viewEvent
);
if
(
$viewEvent
->
hasResponse
())
{
$event
->
setResponse
(
$viewEvent
->
getResponse
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment