|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Core23\FacebookBundle\Session; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\FacebookBundle\Session\Session as FacebookSession; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
final class SessionManager implements SessionManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private const SESSION_FB_EXPIRES = '_CORE23_FACEBOOK_EXPIRES'; |
|
20
|
|
|
|
|
21
|
|
|
private const SESSION_FB_NAME = '_CORE23_FACEBOOK_NAME'; |
|
22
|
|
|
|
|
23
|
|
|
private const SESSION_FB_ID = '_CORE23_FACEBOOK_ID'; |
|
24
|
|
|
|
|
25
|
|
|
private const SESSION_FB_TOKEN = '_CORE23_FACEBOOK_TOKEN'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Session |
|
29
|
|
|
*/ |
|
30
|
|
|
private $session; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(Session $session) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->session = $session; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function isAuthenticated(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return (bool) $this->session->get(static::SESSION_FB_TOKEN); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getUsername(): ?string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->session->get(static::SESSION_FB_NAME); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function store(SessionInterface $session): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->session->set(static::SESSION_FB_ID, $session->getFacebookId()); |
|
50
|
|
|
$this->session->set(static::SESSION_FB_NAME, $session->getName()); |
|
51
|
|
|
$this->session->set(static::SESSION_FB_TOKEN, $session->getToken()); |
|
52
|
|
|
$this->session->set(static::SESSION_FB_EXPIRES, $session->getExpireDate()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function clear(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->session->remove(static::SESSION_FB_ID); |
|
58
|
|
|
$this->session->remove(static::SESSION_FB_NAME); |
|
59
|
|
|
$this->session->remove(static::SESSION_FB_TOKEN); |
|
60
|
|
|
$this->session->remove(static::SESSION_FB_EXPIRES); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getSession(): ?SessionInterface |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->isAuthenticated()) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new FacebookSession( |
|
70
|
|
|
$this->session->get(static::SESSION_FB_ID), |
|
71
|
|
|
$this->session->get(static::SESSION_FB_NAME), |
|
72
|
|
|
$this->session->get(static::SESSION_FB_TOKEN), |
|
73
|
|
|
$this->session->get(static::SESSION_FB_EXPIRES) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: