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
09a61047
Commit
09a61047
authored
Nov 04, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved the converter and string to response listeners to proper classes
parent
f51a1f24
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
45 deletions
+110
-45
src/Silex/Application.php
src/Silex/Application.php
+5
-45
src/Silex/EventListener/ConverterListener.php
src/Silex/EventListener/ConverterListener.php
+60
-0
src/Silex/EventListener/StringToResponseListener.php
src/Silex/EventListener/StringToResponseListener.php
+45
-0
No files found.
src/Silex/Application.php
View file @
09a61047
...
@@ -14,9 +14,7 @@ namespace Silex;
...
@@ -14,9 +14,7 @@ namespace Silex;
use
Symfony\Component\HttpKernel\HttpKernel
;
use
Symfony\Component\HttpKernel\HttpKernel
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
use
Symfony\Component\HttpKernel\TerminableInterface
;
use
Symfony\Component\HttpKernel\TerminableInterface
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
;
use
Symfony\Component\HttpKernel\Event\FilterControllerEvent
;
use
Symfony\Component\HttpKernel\Event\FilterResponseEvent
;
use
Symfony\Component\HttpKernel\Event\FilterResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
...
@@ -31,20 +29,21 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
...
@@ -31,20 +29,21 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RequestContext
;
use
Symfony\Component\Routing\RequestContext
;
use
Silex\RedirectableUrlMatcher
;
use
Silex\RedirectableUrlMatcher
;
use
Silex\ControllerResolver
;
use
Silex\ControllerResolver
;
use
Silex\EventListener\LocaleListener
;
use
Silex\EventListener\LocaleListener
;
use
Silex\EventListener\MiddlewareListener
;
use
Silex\EventListener\MiddlewareListener
;
use
Silex\EventListener\ConverterListener
;
use
Silex\EventListener\StringToResponseListener
;
/**
/**
* The Silex framework class.
* The Silex framework class.
*
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
*/
class
Application
extends
\Pimple
implements
HttpKernelInterface
,
EventSubscriberInterface
,
TerminableInterface
class
Application
extends
\Pimple
implements
HttpKernelInterface
,
TerminableInterface
{
{
const
VERSION
=
'1.0-DEV'
;
const
VERSION
=
'1.0-DEV'
;
...
@@ -91,7 +90,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -91,7 +90,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this
[
'dispatcher_class'
]
=
'Symfony\\Component\\EventDispatcher\\EventDispatcher'
;
$this
[
'dispatcher_class'
]
=
'Symfony\\Component\\EventDispatcher\\EventDispatcher'
;
$this
[
'dispatcher'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$this
[
'dispatcher'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$dispatcher
=
new
$app
[
'dispatcher_class'
]();
$dispatcher
=
new
$app
[
'dispatcher_class'
]();
$dispatcher
->
addSubscriber
(
$app
);
$urlMatcher
=
new
LazyUrlMatcher
(
function
()
use
(
$app
)
{
$urlMatcher
=
new
LazyUrlMatcher
(
function
()
use
(
$app
)
{
return
$app
[
'url_matcher'
];
return
$app
[
'url_matcher'
];
...
@@ -103,6 +101,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -103,6 +101,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
}
}
$dispatcher
->
addSubscriber
(
new
ResponseListener
(
$app
[
'charset'
]));
$dispatcher
->
addSubscriber
(
new
ResponseListener
(
$app
[
'charset'
]));
$dispatcher
->
addSubscriber
(
new
MiddlewareListener
(
$app
));
$dispatcher
->
addSubscriber
(
new
MiddlewareListener
(
$app
));
$dispatcher
->
addSubscriber
(
new
ConverterListener
(
$app
[
'routes'
]));
$dispatcher
->
addSubscriber
(
new
StringToResponseListener
());
return
$dispatcher
;
return
$dispatcher
;
});
});
...
@@ -497,44 +497,4 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -497,44 +497,4 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{
{
$this
[
'kernel'
]
->
terminate
(
$request
,
$response
);
$this
[
'kernel'
]
->
terminate
(
$request
,
$response
);
}
}
/**
* Handles converters.
*
* @param FilterControllerEvent $event The event to handle
*/
public
function
onKernelController
(
FilterControllerEvent
$event
)
{
$request
=
$event
->
getRequest
();
$route
=
$this
[
'routes'
]
->
get
(
$request
->
attributes
->
get
(
'_route'
));
if
(
$route
&&
$converters
=
$route
->
getOption
(
'_converters'
))
{
foreach
(
$converters
as
$name
=>
$callback
)
{
$request
->
attributes
->
set
(
$name
,
call_user_func
(
$callback
,
$request
->
attributes
->
get
(
$name
,
null
),
$request
));
}
}
}
/**
* Handles string responses.
*
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public
function
onKernelView
(
GetResponseForControllerResultEvent
$event
)
{
$response
=
$event
->
getControllerResult
();
$response
=
$response
instanceof
Response
?
$response
:
new
Response
((
string
)
$response
);
$event
->
setResponse
(
$response
);
}
/**
* {@inheritdoc}
*/
public
static
function
getSubscribedEvents
()
{
return
array
(
KernelEvents
::
CONTROLLER
=>
array
(
'onKernelController'
,
0
),
KernelEvents
::
VIEW
=>
array
(
'onKernelView'
,
-
10
),
);
}
}
}
src/Silex/EventListener/ConverterListener.php
0 → 100644
View file @
09a61047
<?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\EventListener
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\Event\FilterControllerEvent
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
use
Symfony\Component\Routing\RouteCollection
;
/**
* Handles converters.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
ConverterListener
implements
EventSubscriberInterface
{
protected
$routes
;
/**
* Constructor.
*
* @param RouteCollection $routes A RouteCollection instance
*/
public
function
__construct
(
RouteCollection
$routes
)
{
$this
->
routes
=
$routes
;
}
/**
* Handles converters.
*
* @param FilterControllerEvent $event The event to handle
*/
public
function
onKernelController
(
FilterControllerEvent
$event
)
{
$request
=
$event
->
getRequest
();
$route
=
$this
->
routes
->
get
(
$request
->
attributes
->
get
(
'_route'
));
if
(
$route
&&
$converters
=
$route
->
getOption
(
'_converters'
))
{
foreach
(
$converters
as
$name
=>
$callback
)
{
$request
->
attributes
->
set
(
$name
,
call_user_func
(
$callback
,
$request
->
attributes
->
get
(
$name
,
null
),
$request
));
}
}
}
public
static
function
getSubscribedEvents
()
{
return
array
(
KernelEvents
::
CONTROLLER
=>
'onKernelController'
,
);
}
}
src/Silex/EventListener/StringToResponseListener.php
0 → 100644
View file @
09a61047
<?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\EventListener
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
use
Symfony\Component\HttpFoundation\Response
;
/**
* Handles converters.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
StringToResponseListener
implements
EventSubscriberInterface
{
/**
* Handles string responses.
*
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public
function
onKernelView
(
GetResponseForControllerResultEvent
$event
)
{
$response
=
$event
->
getControllerResult
();
$response
=
$response
instanceof
Response
?
$response
:
new
Response
((
string
)
$response
);
$event
->
setResponse
(
$response
);
}
public
static
function
getSubscribedEvents
()
{
return
array
(
KernelEvents
::
VIEW
=>
array
(
'onKernelView'
,
-
10
),
);
}
}
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