-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractFactory.php
More file actions
119 lines (103 loc) · 3.35 KB
/
Copy pathAbstractFactory.php
File metadata and controls
119 lines (103 loc) · 3.35 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Bolt\Session\Handler\Factory;
use Bolt\Common\Deprecated;
use Bolt\Session\OptionsBag;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
/**
* Abstract Factory with common functions to help create connections to services.
*
* @author Carson Full <carsonfull@gmail.com>
*/
abstract class AbstractFactory
{
/**
* Parse the session options to connection parameters.
*
* @param OptionsBag $sessionOptions
*
* @return OptionsBag[]
*/
public function parse(OptionsBag $sessionOptions)
{
if ($sessionOptions['save_path']) {
return $this->parseConnectionsFromSavePath($sessionOptions['save_path']);
}
return $this->parseConnectionsFromOptions($sessionOptions);
}
/**
* Parses session save_path into a list of connection parameters.
*
* @param string $savePath
*
* @return OptionsBag[]
*/
protected function parseConnectionsFromSavePath($savePath)
{
$connections = (array) explode(',', $savePath);
$connections = array_map('trim', $connections);
return array_map([$this, 'parseConnectionItemFromSavePath'], $connections);
}
/**
* @param string $path
*
* @return OptionsBag
*/
abstract protected function parseConnectionItemFromSavePath($path);
/**
* Parses session options with array configuration into a list of connection parameters.
*
* @param OptionsBag $options
*
* @return OptionsBag[]
*/
protected function parseConnectionsFromOptions(OptionsBag $options)
{
if ($options->has('connections')) {
$connections = $options->get('connections');
} elseif ($options->has('connection')) {
$connections = [$options->get('connection')];
} elseif ($options->has('host') || $options->has('port')) {
Deprecated::warn('Specifying "host" and other options directly in session config', 1.0, 'Move them under the "connection" key.');
$connections = [$options->all()];
} else {
$connections = [[]];
}
return array_map([$this, 'parseConnectionItemFromOptions'], $connections);
}
/**
* @param array|string $item
*
* @return OptionsBag
*/
abstract protected function parseConnectionItemFromOptions($item);
/**
* @param string $uri
*
* @return UriInterface
*/
protected function parseUri($uri)
{
if ($uri && stripos($uri, 'unix://') === 0) {
// parse_url() doesn't like "unix:///foo", but parses "unix:/foo" fine.
$uri = str_ireplace('unix://', 'unix:', $uri);
} elseif ($uri && $uri[0] !== '/' && !preg_match('#^\w+://?.+#', $uri)) {
// If no scheme given and path exists and isn't absolute, assume tcp scheme is wanted.
// This allows both "127.0.0.1" and "/foo.sock" to work without specifying scheme.
// If non absolute path is wanted, specify the scheme: "unix://foo.sock"
$uri = 'tcp://' . $uri;
}
$parsed = new Uri($uri);
return $parsed;
}
/**
* @param UriInterface $uri
*
* @return OptionsBag
*/
protected function parseQuery(UriInterface $uri)
{
$query = \GuzzleHttp\Psr7\parse_query($uri->getQuery());
return new OptionsBag($query);
}
}