-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionManager.php
More file actions
60 lines (49 loc) · 1.43 KB
/
SessionManager.php
File metadata and controls
60 lines (49 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Perform\PageEditorBundle;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Starts, stops, and detects a page editing session.
*
* @author Glynn Forrest <me@glynnforrest.com>
**/
class SessionManager
{
const SESSION_KEY = 'perform_page_editor.session';
protected $excludedUrlRegexes = [];
public function __construct(array $excludedUrlRegexes = [])
{
$this->excludedUrlRegexes = $excludedUrlRegexes;
}
public function start(SessionInterface $session)
{
// dispatch an event
// set the time the session started
$session->set(self::SESSION_KEY, true);
}
public function stop(SessionInterface $session)
{
// dispatch an event
$session->remove(self::SESSION_KEY);
}
public function isEditing(SessionInterface $session = null)
{
return $session && $session->get(self::SESSION_KEY) === true;
}
public function requestIsEditing(Request $request)
{
if ($request->isXmlHttpRequest()) {
return false;
}
if (!$this->isEditing($request->getSession())) {
return false;
}
$url = $request->getPathinfo();
foreach ($this->excludedUrlRegexes as $regex) {
if (preg_match('`'.$regex.'`', $url)) {
return false;
}
}
return true;
}
}