Skip to content

Commit 3a88934

Browse files
author
Prosper Otemuyiwa
authored
Merge pull request #22 from vferdiansyah/session-storage
Add session storage
2 parents 0d7e01e + 57e506c commit 3a88934

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

config/parse.php

+12
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@
6767

6868
'mount_path' => '1',
6969

70+
/*
71+
|--------------------------------------------------------------------------
72+
| Parse Session Handler
73+
|--------------------------------------------------------------------------
74+
|
75+
| Here you may specify your parse session handler.
76+
| Use 'laravel' if you want to use Laravel's session or blank if you don't.
77+
|
78+
*/
79+
80+
'session' => 'laravel',
81+
7082
];

src/ParseServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ protected function setupParse()
7171

7272
ParseClient::setServerURL($serverURL, $mountPath);
7373
}
74+
75+
if (isset($config['session']) && $config['session'] === 'laravel') {
76+
ParseClient::setStorage(new ParseSessionStorage($this->app->session));
77+
}
7478
}
7579

7680
/**

src/ParseSessionStorage.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace GrahamCampbell\Parse;
4+
5+
use Illuminate\Session\SessionManager;
6+
use Parse\ParseStorageInterface;
7+
8+
class ParseSessionStorage implements ParseStorageInterface
9+
{
10+
/**
11+
* @var \Illuminate\Session\SessionManager
12+
*/
13+
private $session;
14+
15+
/**
16+
* @param \Illuminate\Session\SessionManager $session
17+
*/
18+
public function __construct(SessionManager $session)
19+
{
20+
$this->session = $session;
21+
}
22+
23+
/**
24+
* Sets a key-value pair in storage.
25+
*
26+
* @param string $key The key to set
27+
* @param mixed $value The value to set
28+
*
29+
* @return null
30+
*/
31+
public function set($key, $value)
32+
{
33+
$this->session->put($key, $value);
34+
}
35+
36+
/**
37+
* Remove a key from storage.
38+
*
39+
* @param string $key The key to remove.
40+
*
41+
* @return null
42+
*/
43+
public function remove($key)
44+
{
45+
$this->session->forget($key);
46+
}
47+
48+
/**
49+
* Gets the value for a key from storage.
50+
*
51+
* @param string $key The key to get the value for
52+
*
53+
* @return mixed
54+
*/
55+
public function get($key)
56+
{
57+
if ($this->session->has($key)) {
58+
return $this->session->get($key);
59+
}
60+
}
61+
62+
/**
63+
* Clear all the values in storage.
64+
*
65+
* @return null
66+
*/
67+
public function clear()
68+
{
69+
$this->session->forget();
70+
}
71+
72+
/**
73+
* Save the data, if necessary. This would be a no-op when using the
74+
* $_SESSION implementation, but could be used for saving to file or
75+
* database as an action instead of on every set.
76+
*
77+
* @return null
78+
*/
79+
public function save()
80+
{
81+
//
82+
}
83+
84+
/**
85+
* Get all keys in storage.
86+
*
87+
* @return array
88+
*/
89+
public function getKeys()
90+
{
91+
return array_keys($this->session->get());
92+
}
93+
94+
/**
95+
* Get all key-value pairs from storage.
96+
*
97+
* @return array
98+
*/
99+
public function getAll()
100+
{
101+
return $this->session->get();
102+
}
103+
}

0 commit comments

Comments
 (0)