4
4
5
5
namespace Enqueue \Fs ;
6
6
7
+ use Enqueue \Dsn \Dsn ;
7
8
use Interop \Queue \ConnectionFactory ;
8
9
use Interop \Queue \Context ;
9
10
@@ -27,23 +28,31 @@ class FsConnectionFactory implements ConnectionFactory
27
28
* or
28
29
*
29
30
* file: - create queue files in tmp dir.
30
- * file://home/foo/enqueue
31
- * file://home/foo/enqueue?pre_fetch_count=20&chmod=0777
31
+ * file:/// home/foo/enqueue
32
+ * file:/// home/foo/enqueue?pre_fetch_count=20&chmod=0777
32
33
*
33
34
* @param array|string|null $config
34
35
*/
35
36
public function __construct ($ config = 'file: ' )
36
37
{
37
38
if (empty ($ config ) || 'file: ' === $ config ) {
38
- $ config = [ ' path ' => sys_get_temp_dir ().'/enqueue ' ] ;
39
+ $ config = $ this -> parseDsn ( ' file:// ' . sys_get_temp_dir ().'/enqueue ' ) ;
39
40
} elseif (is_string ($ config )) {
40
- $ config = $ this ->parseDsn ($ config );
41
+ if ('/ ' === $ config [0 ]) {
42
+ $ config = $ this ->parseDsn ('file:// ' .$ config );
43
+ } else {
44
+ $ config = $ this ->parseDsn ($ config );
45
+ }
41
46
} elseif (is_array ($ config )) {
42
47
} else {
43
48
throw new \LogicException ('The config must be either an array of options, a DSN string or null ' );
44
49
}
45
50
46
51
$ this ->config = array_replace ($ this ->defaultConfig (), $ config );
52
+
53
+ if (empty ($ this ->config ['path ' ])) {
54
+ throw new \LogicException ('The path option must be set. ' );
55
+ }
47
56
}
48
57
49
58
/**
@@ -61,39 +70,23 @@ public function createContext(): Context
61
70
62
71
private function parseDsn (string $ dsn ): array
63
72
{
64
- if ($ dsn && '/ ' === $ dsn [0 ]) {
65
- return ['path ' => $ dsn ];
66
- }
67
-
68
- if (false === strpos ($ dsn , 'file: ' )) {
69
- throw new \LogicException (sprintf ('The given DSN "%s" is not supported. Must start with "file:". ' , $ dsn ));
70
- }
71
-
72
- $ dsn = substr ($ dsn , 7 );
73
-
74
- $ path = parse_url ($ dsn , PHP_URL_PATH );
75
- $ query = parse_url ($ dsn , PHP_URL_QUERY );
76
-
77
- if ('/ ' != $ path [0 ]) {
78
- throw new \LogicException (sprintf ('Failed to parse DSN path "%s". The path must start with "/" ' , $ path ));
73
+ $ dsn = new Dsn ($ dsn );
74
+
75
+ $ supportedSchemes = ['file ' ];
76
+ if (false == in_array ($ dsn ->getSchemeProtocol (), $ supportedSchemes , true )) {
77
+ throw new \LogicException (sprintf (
78
+ 'The given scheme protocol "%s" is not supported. It must be one of "%s" ' ,
79
+ $ dsn ->getSchemeProtocol (),
80
+ implode ('", " ' , $ supportedSchemes )
81
+ ));
79
82
}
80
83
81
- if ($ query ) {
82
- $ config = [];
83
- parse_str ($ query , $ config );
84
- }
85
-
86
- if (isset ($ config ['pre_fetch_count ' ])) {
87
- $ config ['pre_fetch_count ' ] = (int ) $ config ['pre_fetch_count ' ];
88
- }
89
-
90
- if (isset ($ config ['chmod ' ])) {
91
- $ config ['chmod ' ] = intval ($ config ['chmod ' ], 8 );
92
- }
93
-
94
- $ config ['path ' ] = $ path ;
95
-
96
- return $ config ;
84
+ return array_filter (array_replace ($ dsn ->getQuery (), [
85
+ 'path ' => $ dsn ->getPath (),
86
+ 'pre_fetch_count ' => $ dsn ->getInt ('pre_fetch_count ' ),
87
+ 'chmod ' => $ dsn ->getOctal ('chmod ' ),
88
+ 'polling_interval ' => $ dsn ->getInt ('polling_interval ' ),
89
+ ]), function ($ value ) { return null !== $ value ; });
97
90
}
98
91
99
92
private function defaultConfig (): array
0 commit comments