forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFsConnectionFactory.php
More file actions
112 lines (95 loc) · 2.98 KB
/
FsConnectionFactory.php
File metadata and controls
112 lines (95 loc) · 2.98 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
<?php
namespace Enqueue\Fs;
use Interop\Queue\PsrConnectionFactory;
class FsConnectionFactory implements PsrConnectionFactory
{
/**
* @var string
*/
private $config;
/**
* The config could be an array, string DSN or null. In case of null it will attempt to store files in /tmp/enqueue folder.
*
* [
* 'path' => 'the directory where all queue\topic files remain. For example /home/foo/enqueue',
* 'pre_fetch_count' => 'Integer. Defines how many messages to fetch from the file.',
* 'chmod' => 'Defines a mode the files are created with',
* 'polling_interval' => 'How often query for new messages, default 100 (milliseconds)',
* ]
*
* or
*
* file: - create queue files in tmp dir.
* file://home/foo/enqueue
* file://home/foo/enqueue?pre_fetch_count=20&chmod=0777
*
* @param array|string|null $config
*/
public function __construct($config = 'file:')
{
if (empty($config) || 'file:' === $config) {
$config = ['path' => sys_get_temp_dir().'/enqueue'];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$this->config = array_replace($this->defaultConfig(), $config);
}
/**
* {@inheritdoc}
*
* @return FsContext
*/
public function createContext()
{
return new FsContext(
$this->config['path'],
$this->config['pre_fetch_count'],
$this->config['chmod'],
$this->config['polling_interval']
);
}
/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
{
if ($dsn && '/' === $dsn[0]) {
return ['path' => $dsn];
}
if (false === strpos($dsn, 'file:')) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "file:".', $dsn));
}
$dsn = substr($dsn, 7);
$path = parse_url($dsn, PHP_URL_PATH);
$query = parse_url($dsn, PHP_URL_QUERY);
if ('/' != $path[0]) {
throw new \LogicException(sprintf('Failed to parse DSN path "%s". The path must start with "/"', $path));
}
if ($query) {
$config = [];
parse_str($query, $config);
}
if (isset($config['pre_fetch_count'])) {
$config['pre_fetch_count'] = (int) $config['pre_fetch_count'];
}
if (isset($config['chmod'])) {
$config['chmod'] = intval($config['chmod'], 8);
}
$config['path'] = $path;
return $config;
}
private function defaultConfig()
{
return [
'path' => null,
'pre_fetch_count' => 1,
'chmod' => 0600,
'polling_interval' => 100,
];
}
}