44
55namespace Enqueue \Fs ;
66
7+ use Enqueue \Dsn \Dsn ;
78use Interop \Queue \ConnectionFactory ;
89use Interop \Queue \Context ;
910
@@ -27,23 +28,31 @@ class FsConnectionFactory implements ConnectionFactory
2728 * or
2829 *
2930 * 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
3233 *
3334 * @param array|string|null $config
3435 */
3536 public function __construct ($ config = 'file: ' )
3637 {
3738 if (empty ($ config ) || 'file: ' === $ config ) {
38- $ config = [ ' path ' => sys_get_temp_dir ().'/enqueue ' ] ;
39+ $ config = $ this -> parseDsn ( ' file:// ' . sys_get_temp_dir ().'/enqueue ' ) ;
3940 } 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+ }
4146 } elseif (is_array ($ config )) {
4247 } else {
4348 throw new \LogicException ('The config must be either an array of options, a DSN string or null ' );
4449 }
4550
4651 $ 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+ }
4756 }
4857
4958 /**
@@ -61,39 +70,23 @@ public function createContext(): Context
6170
6271 private function parseDsn (string $ dsn ): array
6372 {
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+ ));
7982 }
8083
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 ; });
9790 }
9891
9992 private function defaultConfig (): array
0 commit comments