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
60243281
Commit
60243281
authored
Feb 23, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make full use of event dispatcher, change error behaviour to notifyUntil
parent
38b34164
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
18 deletions
+25
-18
src/Silex/Framework.php
src/Silex/Framework.php
+19
-16
tests/Silex/Tests/ErrorHandlerTest.php
tests/Silex/Tests/ErrorHandlerTest.php
+6
-2
No files found.
src/Silex/Framework.php
View file @
60243281
...
...
@@ -6,8 +6,9 @@ use Symfony\Component\HttpKernel\HttpKernel;
use
Symfony\Component\HttpKernel\Controller\ControllerResolver
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\EventDispatcher\EventDispatcher
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\EventDispatcher\EventDispatcher
;
use
Symfony\Component\EventDispatcher\EventInterface
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\Route
;
use
Symfony\Component\Routing\Matcher\UrlMatcher
;
...
...
@@ -29,7 +30,6 @@ use Symfony\Component\Routing\Matcher\UrlMatcher;
class
Framework
extends
HttpKernel
{
protected
$routes
;
protected
$errorHandlers
=
array
();
protected
$request
;
/**
...
...
@@ -224,7 +224,15 @@ class Framework extends HttpKernel
*/
public
function
error
(
$callback
)
{
$this
->
errorHandlers
[]
=
$callback
;
$this
->
dispatcher
->
connect
(
'silex.error'
,
function
(
EventInterface
$event
)
use
(
$callback
)
{
$exception
=
$event
->
get
(
'exception'
);
$result
=
$callback
(
$exception
);
if
(
null
!==
$result
)
{
$event
->
setProcessed
();
return
$result
;
}
});
return
$this
;
}
...
...
@@ -332,25 +340,20 @@ class Framework extends HttpKernel
/**
* Handler for core.exception
*
* Executes
all registered error handlers and sets the first response
*
to be sen
t to the client.
* Executes
registered error handlers until a response is returned,
*
in which case it returns i
t to the client.
*
* @see error()
*/
public
function
handleException
(
Event
$event
)
{
$exception
=
$event
->
get
(
'exception'
);
$errorEvent
=
new
Event
(
null
,
'silex.error'
,
$event
->
all
());
$result
=
$this
->
dispatcher
->
notifyUntil
(
$errorEvent
);
$response
=
$prevResult
=
null
;
foreach
(
$this
->
errorHandlers
as
$callback
)
{
$result
=
$callback
(
$exception
);
if
(
null
!==
$result
&&
!
$prevResult
)
{
if
(
$errorEvent
->
isProcessed
())
{
$event
->
setProcessed
();
$response
=
$this
->
parseStringResponse
(
$event
,
$result
);
$event
->
setProcessed
(
true
);
$prevResult
=
$result
;
}
}
return
$response
;
}
}
}
tests/Silex/Tests/ErrorHandlerTest.php
View file @
60243281
...
...
@@ -70,20 +70,24 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
$errors
++
;
});
$framework
->
error
(
function
(
$e
)
use
(
&
$errors
)
{
$errors
++
;
});
$framework
->
error
(
function
(
$e
)
use
(
&
$errors
)
{
$errors
++
;
return
new
Response
(
'foo exception handler'
);
});
$framework
->
error
(
function
(
$e
)
use
(
&
$errors
)
{
// should not execute
$errors
++
;
return
new
Response
(
'foo exception handler 2'
);
});
$request
=
Request
::
create
(
'/foo'
);
$this
->
checkRouteResponse
(
$framework
,
'/foo'
,
'foo exception handler'
,
'should return the first response returned by an exception handler'
);
$this
->
assertEquals
(
3
,
$errors
,
'should execute
all error handlers
'
);
$this
->
assertEquals
(
3
,
$errors
,
'should execute
error handlers until a response is returned
'
);
}
public
function
testNoResponseErrorHandler
()
...
...
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