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
86c02016
Commit
86c02016
authored
Nov 08, 2013
by
Beau Simensen
Committed by
Dave Marshall
May 05, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add view.
parent
fde7db80
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
+89
-0
src/Silex/Application.php
src/Silex/Application.php
+23
-0
tests/Silex/Tests/ApplicationTest.php
tests/Silex/Tests/ApplicationTest.php
+66
-0
No files found.
src/Silex/Application.php
View file @
86c02016
...
...
@@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
use
Symfony\Component\HttpKernel\TerminableInterface
;
use
Symfony\Component\HttpKernel\Event\FilterResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
use
Symfony\Component\HttpKernel\EventListener\ResponseListener
;
use
Symfony\Component\HttpKernel\EventListener\RouterListener
;
...
...
@@ -404,6 +405,28 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$this
->
on
(
KernelEvents
::
EXCEPTION
,
new
ExceptionListenerWrapper
(
$this
,
$callback
),
$priority
);
}
/**
* Registers a view handler.
*
* View handlers are simple callables which take a single
* GetResponseForControllerResultEvent as an argument. They care called
* when a controller returns a value that is not an instance of Response.
*
* Call setResponse() to set the response that will be returned for the
* current request. The propagation of this event is stopped as soon as a
* response is set.
*
* @param mixed $callback View handelr callback
* @param integer $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
*/
public
function
view
(
$callback
,
$priority
=
0
)
{
$this
->
on
(
KernelEvents
::
VIEW
,
function
(
GetResponseForControllerResultEvent
$event
)
use
(
$callback
)
{
call_user_func
(
$callback
,
$event
);
},
$priority
);
}
/**
* Flushes the controller collection.
*
...
...
tests/Silex/Tests/ApplicationTest.php
View file @
86c02016
...
...
@@ -559,6 +559,72 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
301
,
$response
->
getStatusCode
());
}
public
function
testBeforeFilterOnMountedControllerGroupIsolatedToGroup
()
{
$app
=
new
Application
();
$app
->
match
(
'/'
,
function
()
{
return
new
Response
(
'ok'
);
});
$mounted
=
$app
[
'controllers_factory'
];
$mounted
->
before
(
function
()
{
return
new
Response
(
'not ok'
);
});
$app
->
mount
(
'/group'
,
$mounted
);
$response
=
$app
->
handle
(
Request
::
create
(
'/'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
public
function
testViewListener
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
array
(
'ok'
);
});
$app
->
view
(
function
(
$event
)
{
$view
=
$event
->
getControllerResult
();
$event
->
setResponse
(
new
Response
(
$view
[
0
]));
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
public
function
testDefaultPriorityStringResponseTriggerViewListener
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
'not ok'
;
});
$app
->
view
(
function
(
$event
)
{
$event
->
setResponse
(
new
Response
(
'ok'
));
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
public
function
testLowPriorityStringResponseDoesNotTriggerViewListener
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
'ok'
;
});
$app
->
view
(
function
(
$event
)
{
$event
->
setResponse
(
new
Response
(
'not ok'
));
},
-
100
);
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
public
function
testResponseInstanceResponseDoesNotTriggerViewListener
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
new
Response
(
'ok'
);
});
$app
->
view
(
function
(
$event
)
{
$event
->
setResponse
(
new
Response
(
'not ok'
));
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
}
class
FooController
...
...
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