Skip to content

Commit 7103b5b

Browse files
committed
Working on session component.
1 parent 8d41c27 commit 7103b5b

File tree

3 files changed

+116
-50
lines changed

3 files changed

+116
-50
lines changed

src/Illuminate/Session/DatabaseSessionHandler.php

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Session;
44

55
use Carbon\Carbon;
6+
use Illuminate\Support\Arr;
67
use SessionHandlerInterface;
78
use Illuminate\Contracts\Auth\Guard;
89
use Illuminate\Database\ConnectionInterface;
@@ -85,12 +86,10 @@ public function read($sessionId)
8586
{
8687
$session = (object) $this->getQuery()->find($sessionId);
8788

88-
if (isset($session->last_activity)) {
89-
if ($session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
90-
$this->exists = true;
89+
if ($this->expired($session)) {
90+
$this->exists = true;
9191

92-
return;
93-
}
92+
return;
9493
}
9594

9695
if (isset($session->payload)) {
@@ -100,6 +99,18 @@ public function read($sessionId)
10099
}
101100
}
102101

102+
/**
103+
* Determine if the session is expired.
104+
*
105+
* @param StdClass $session
106+
* @return bool
107+
*/
108+
protected function expired($session)
109+
{
110+
return (isset($session->last_activity) &&
111+
$session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp());
112+
}
113+
103114
/**
104115
* {@inheritdoc}
105116
*/
@@ -114,14 +125,10 @@ public function write($sessionId, $data)
114125
if ($this->exists) {
115126
$this->getQuery()->where('id', $sessionId)->update($payload);
116127
} else {
117-
$payload['id'] = $sessionId;
118-
119-
$this->getQuery()->insert($payload);
128+
$this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
120129
}
121130

122-
$this->exists = true;
123-
124-
return true;
131+
return $this->exists = true;
125132
}
126133

127134
/**
@@ -132,25 +139,82 @@ public function write($sessionId, $data)
132139
*/
133140
protected function getDefaultPayload($data)
134141
{
135-
$payload = ['payload' => base64_encode($data), 'last_activity' => Carbon::now()->getTimestamp()];
142+
$payload = [
143+
'payload' => base64_encode($data),
144+
'last_activity' => Carbon::now()->getTimestamp()
145+
];
136146

137-
if (! $container = $this->container) {
147+
if (! $this->container) {
138148
return $payload;
139149
}
140150

141-
if ($container->bound(Guard::class)) {
142-
$payload['user_id'] = $container->make(Guard::class)->id();
151+
return tap($payload, function ($payload) {
152+
$this->addUserInformation($payload)
153+
->addRequestInformation($payload);
154+
});
155+
}
156+
157+
/**
158+
* Add the user information to the session payload.
159+
*
160+
* @param array $payload
161+
* @return $this
162+
*/
163+
protected function addUserInformation(&$payload)
164+
{
165+
if ($this->container->bound(Guard::class)) {
166+
$payload['user_id'] = $this->userId();
143167
}
144168

145-
if ($container->bound('request')) {
146-
$payload['ip_address'] = $container->make('request')->ip();
169+
return $this;
170+
}
147171

148-
$payload['user_agent'] = substr(
149-
(string) $container->make('request')->header('User-Agent'), 0, 500
150-
);
172+
/**
173+
* Get the currently authenticated user's ID.
174+
*
175+
* @return mixed
176+
*/
177+
protected function userId()
178+
{
179+
return $this->container->make(Guard::class)->id();
180+
}
181+
182+
/**
183+
* Add the request information to the session payload.
184+
*
185+
* @param array $payload
186+
* @return $this
187+
*/
188+
protected function addRequestInformation(&$payload)
189+
{
190+
if ($this->container->bound('request')) {
191+
$payload = array_merge($payload, [
192+
'ip_address' => $this->ipAddress(),
193+
'user_agent' => $this->userAgent(),
194+
]);
151195
}
152196

153-
return $payload;
197+
return $this;
198+
}
199+
200+
/**
201+
* Get the IP address for the current request.
202+
*
203+
* @return string
204+
*/
205+
protected function ipAddress()
206+
{
207+
return $this->container->make('request')->ip();
208+
}
209+
210+
/**
211+
* Get the user agent for the current request.
212+
*
213+
* @return string
214+
*/
215+
protected function userAgent()
216+
{
217+
return substr((string) $this->container->make('request')->header('User-Agent'), 0, 500);
154218
}
155219

156220
/**

src/Illuminate/Session/SessionManager.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ protected function createArrayDriver()
3535
*/
3636
protected function createCookieDriver()
3737
{
38-
$lifetime = $this->app['config']['session.lifetime'];
39-
40-
return $this->buildSession(new CookieSessionHandler($this->app['cookie'], $lifetime));
38+
return $this->buildSession(new CookieSessionHandler(
39+
$this->app['cookie'], $this->app['config']['session.lifetime']
40+
));
4141
}
4242

4343
/**
@@ -57,11 +57,11 @@ protected function createFileDriver()
5757
*/
5858
protected function createNativeDriver()
5959
{
60-
$path = $this->app['config']['session.files'];
61-
6260
$lifetime = $this->app['config']['session.lifetime'];
6361

64-
return $this->buildSession(new FileSessionHandler($this->app['files'], $path, $lifetime));
62+
return $this->buildSession(new FileSessionHandler(
63+
$this->app['files'], $this->app['config']['session.files'], $lifetime
64+
));
6565
}
6666

6767
/**
@@ -71,13 +71,13 @@ protected function createNativeDriver()
7171
*/
7272
protected function createDatabaseDriver()
7373
{
74-
$connection = $this->getDatabaseConnection();
75-
7674
$table = $this->app['config']['session.table'];
7775

7876
$lifetime = $this->app['config']['session.lifetime'];
7977

80-
return $this->buildSession(new DatabaseSessionHandler($connection, $table, $lifetime, $this->app));
78+
return $this->buildSession(new DatabaseSessionHandler(
79+
$this->getDatabaseConnection(), $table, $lifetime, $this->app
80+
));
8181
}
8282

8383
/**
@@ -112,16 +112,6 @@ protected function createMemcachedDriver()
112112
return $this->createCacheBased('memcached');
113113
}
114114

115-
/**
116-
* Create an instance of the Wincache session driver.
117-
*
118-
* @return \Illuminate\Session\Store
119-
*/
120-
protected function createWincacheDriver()
121-
{
122-
return $this->createCacheBased('wincache');
123-
}
124-
125115
/**
126116
* Create an instance of the Redis session driver.
127117
*
@@ -131,7 +121,9 @@ protected function createRedisDriver()
131121
{
132122
$handler = $this->createCacheHandler('redis');
133123

134-
$handler->getCache()->getStore()->setConnection($this->app['config']['session.connection']);
124+
$handler->getCache()->getStore()->setConnection(
125+
$this->app['config']['session.connection']
126+
);
135127

136128
return $this->buildSession($handler);
137129
}
@@ -157,9 +149,10 @@ protected function createCacheHandler($driver)
157149
{
158150
$store = $this->app['config']->get('session.store') ?: $driver;
159151

160-
$minutes = $this->app['config']['session.lifetime'];
161-
162-
return new CacheBasedSessionHandler(clone $this->app['cache']->store($store), $minutes);
152+
return new CacheBasedSessionHandler(
153+
clone $this->app['cache']->store($store),
154+
$this->app['config']['session.lifetime']
155+
);
163156
}
164157

165158
/**
@@ -171,14 +164,25 @@ protected function createCacheHandler($driver)
171164
protected function buildSession($handler)
172165
{
173166
if ($this->app['config']['session.encrypt']) {
174-
return new EncryptedStore(
175-
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
176-
);
167+
return $this->buildEncryptedSession($handler);
177168
} else {
178169
return new Store($this->app['config']['session.cookie'], $handler);
179170
}
180171
}
181172

173+
/**
174+
* Build the encrypted session instance.
175+
*
176+
* @param \SessionHandlerInterface $handler
177+
* @return \Illuminate\Session\EncryptedStore
178+
*/
179+
protected function buildEncryptedSession($handler)
180+
{
181+
return new EncryptedStore(
182+
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
183+
);
184+
}
185+
182186
/**
183187
* Get the session configuration.
184188
*

src/Illuminate/Session/SessionServiceProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ protected function registerSessionDriver()
4343
// First, we will create the session manager which is responsible for the
4444
// creation of the various session drivers when they are needed by the
4545
// application instance, and will resolve them on a lazy load basis.
46-
$manager = $app['session'];
47-
48-
return $manager->driver();
46+
return $app->make('session')->driver();
4947
});
5048
}
5149
}

0 commit comments

Comments
 (0)