-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
SessionConverter.php
81 lines (72 loc) · 2.19 KB
/
SessionConverter.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Neos\Flow\Property\TypeConverter;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Property\PropertyMappingConfigurationInterface;
use Neos\Flow\Session\Session;
use Neos\Flow\Session\SessionManagerInterface;
/**
* This converter transforms a session identifier into a real session object.
*
* Given a session ID this will return an instance of Neos\Flow\Session\Session.
*
* @api
* @Flow\Scope("singleton")
*/
class SessionConverter extends AbstractTypeConverter
{
/**
* @var string
*/
const PATTERN_MATCH_SESSIONIDENTIFIER = '/([a-zA-Z0-9]){32}/';
/**
* @var array
*/
protected $sourceTypes = ['string'];
/**
* @var string
*/
protected $targetType = Session::class;
/**
* @var integer
*/
protected $priority = 1;
/**
* @Flow\Inject
* @var SessionManagerInterface
*/
protected $sessionManager;
/**
* This implementation always returns true for this method.
*
* @param mixed $source the source data
* @param string $targetType the type to convert to.
* @return boolean true if this TypeConverter can convert from $source to $targetType, false otherwise.
* @api
*/
public function canConvertFrom($source, $targetType)
{
return (preg_match(self::PATTERN_MATCH_SESSIONIDENTIFIER, $source) === 1) && ($targetType === $this->targetType);
}
/**
* Convert a session identifier from $source to a Session object
*
* @param string $source
* @param string $targetType
* @param array $convertedChildProperties
* @param PropertyMappingConfigurationInterface|null $configuration
* @return object the target type
*/
public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
{
return $this->sessionManager->getSession($source);
}
}