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
88e44ea4
Commit
88e44ea4
authored
Nov 03, 2013
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored session code to make it more reusable
parent
9a32c0a9
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
52 deletions
+94
-52
doc/changelog.rst
doc/changelog.rst
+1
-1
src/Silex/EventListener/SessionListener.php
src/Silex/EventListener/SessionListener.php
+39
-0
src/Silex/EventListener/TestSessionListener.php
src/Silex/EventListener/TestSessionListener.php
+39
-0
src/Silex/Provider/SessionServiceProvider.php
src/Silex/Provider/SessionServiceProvider.php
+12
-50
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
+3
-1
No files found.
doc/changelog.rst
View file @
88e44ea4
...
@@ -4,7 +4,7 @@ Changelog
...
@@ -4,7 +4,7 @@ Changelog
2.0.0 (2013-XX-XX)
2.0.0 (2013-XX-XX)
------------------
------------------
*
n/a
*
Updated session listeners to extends HttpKernel ones
1.2.0 (2014-03-29)
1.2.0 (2014-03-29)
...
...
src/Silex/EventListener/SessionListener.php
0 → 100644
View file @
88e44ea4
<?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
Silex\Application
;
use
Symfony\Component\HttpKernel\EventListener\SessionListener
as
BaseSessionListener
;
/**
* Sets the session in the request.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
SessionListener
extends
BaseSessionListener
{
private
$app
;
public
function
__construct
(
Application
$app
)
{
$this
->
app
=
$app
;
}
public
function
getSession
()
{
if
(
!
isset
(
$this
->
app
[
'session'
]))
{
return
null
;
}
return
$this
->
app
[
'session'
];
}
}
src/Silex/EventListener/TestSessionListener.php
0 → 100644
View file @
88e44ea4
<?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
Silex\Application
;
use
Symfony\Component\HttpKernel\EventListener\TestSessionListener
as
BaseTestSessionListener
;
/**
* Simulates sessions for testing purpose.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
TestSessionListener
extends
BaseTestSessionListener
{
private
$app
;
public
function
__construct
(
Application
$app
)
{
$this
->
app
=
$app
;
}
public
function
getSession
()
{
if
(
!
isset
(
$this
->
app
[
'session'
]))
{
return
null
;
}
return
$this
->
app
[
'session'
];
}
}
src/Silex/Provider/SessionServiceProvider.php
View file @
88e44ea4
...
@@ -13,15 +13,12 @@ namespace Silex\Provider;
...
@@ -13,15 +13,12 @@ namespace Silex\Provider;
use
Silex\Application
;
use
Silex\Application
;
use
Silex\ServiceProviderInterface
;
use
Silex\ServiceProviderInterface
;
use
Silex\EventListener\SessionListener
;
use
Silex\EventListener\TestSessionListener
;
use
Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler
;
use
Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler
;
use
Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage
;
use
Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage
;
use
Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage
;
use
Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage
;
use
Symfony\Component\HttpFoundation\Session\Session
;
use
Symfony\Component\HttpFoundation\Session\Session
;
use
Symfony\Component\HttpFoundation\Cookie
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\Event\FilterResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
/**
/**
* Symfony HttpFoundation component Provider for sessions.
* Symfony HttpFoundation component Provider for sessions.
...
@@ -61,64 +58,29 @@ class SessionServiceProvider implements ServiceProviderInterface
...
@@ -61,64 +58,29 @@ class SessionServiceProvider implements ServiceProviderInterface
);
);
});
});
$app
[
'session.listener'
]
=
$app
->
share
(
function
(
$app
)
{
return
new
SessionListener
(
$app
);
});
$app
[
'session.storage.test'
]
=
$app
->
share
(
function
()
{
$app
[
'session.storage.test'
]
=
$app
->
share
(
function
()
{
return
new
MockFileSessionStorage
();
return
new
MockFileSessionStorage
();
});
});
$app
[
'session.listener.test'
]
=
$app
->
share
(
function
(
$app
)
{
return
new
TestSessionListener
(
$app
);
});
$app
[
'session.storage.options'
]
=
array
();
$app
[
'session.storage.options'
]
=
array
();
$app
[
'session.default_locale'
]
=
'en'
;
$app
[
'session.default_locale'
]
=
'en'
;
$app
[
'session.storage.save_path'
]
=
null
;
$app
[
'session.storage.save_path'
]
=
null
;
}
}
public
function
onEarlyKernelRequest
(
GetResponseEvent
$event
)
{
$event
->
getRequest
()
->
setSession
(
$this
->
app
[
'session'
]);
}
public
function
onKernelRequest
(
GetResponseEvent
$event
)
{
if
(
HttpKernelInterface
::
MASTER_REQUEST
!==
$event
->
getRequestType
())
{
return
;
}
// bootstrap the session
if
(
!
isset
(
$this
->
app
[
'session'
]))
{
return
;
}
$session
=
$this
->
app
[
'session'
];
$cookies
=
$event
->
getRequest
()
->
cookies
;
if
(
$cookies
->
has
(
$session
->
getName
()))
{
$session
->
setId
(
$cookies
->
get
(
$session
->
getName
()));
}
else
{
$session
->
migrate
(
false
);
}
}
public
function
onKernelResponse
(
FilterResponseEvent
$event
)
{
if
(
HttpKernelInterface
::
MASTER_REQUEST
!==
$event
->
getRequestType
())
{
return
;
}
$session
=
$event
->
getRequest
()
->
getSession
();
if
(
$session
&&
$session
->
isStarted
())
{
$session
->
save
();
$params
=
session_get_cookie_params
();
$event
->
getResponse
()
->
headers
->
setCookie
(
new
Cookie
(
$session
->
getName
(),
$session
->
getId
(),
0
===
$params
[
'lifetime'
]
?
0
:
time
()
+
$params
[
'lifetime'
],
$params
[
'path'
],
$params
[
'domain'
],
$params
[
'secure'
],
$params
[
'httponly'
]));
}
}
public
function
boot
(
Application
$app
)
public
function
boot
(
Application
$app
)
{
{
$app
[
'dispatcher'
]
->
add
Listener
(
KernelEvents
::
REQUEST
,
array
(
$this
,
'onEarlyKernelRequest'
),
128
);
$app
[
'dispatcher'
]
->
add
Subscriber
(
$app
[
'session.listener'
]
);
if
(
$app
[
'session.test'
])
{
if
(
$app
[
'session.test'
])
{
$app
[
'dispatcher'
]
->
addListener
(
KernelEvents
::
REQUEST
,
array
(
$this
,
'onKernelRequest'
),
192
);
$app
[
'dispatcher'
]
->
addSubscriber
(
$app
[
'session.listener.test'
]);
$app
[
'dispatcher'
]
->
addListener
(
KernelEvents
::
RESPONSE
,
array
(
$this
,
'onKernelResponse'
),
-
128
);
}
}
}
}
}
}
tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
View file @
88e44ea4
...
@@ -200,7 +200,9 @@ class SecurityServiceProviderTest extends WebTestCase
...
@@ -200,7 +200,9 @@ class SecurityServiceProviderTest extends WebTestCase
'default'
=>
array
(
'default'
=>
array
(
'pattern'
=>
'^.*$'
,
'pattern'
=>
'^.*$'
,
'anonymous'
=>
true
,
'anonymous'
=>
true
,
'form'
=>
true
,
'form'
=>
array
(
'require_previous_session'
=>
false
,
),
'logout'
=>
true
,
'logout'
=>
true
,
'users'
=>
array
(
'users'
=>
array
(
// password is foo
// password is foo
...
...
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