@@ -32,9 +32,9 @@ class AppFactory
32
32
/**
33
33
* Create an application.
34
34
*/
35
- public static function create (string $ host = '0.0.0.0 ' , int $ port = 9501 ): App
35
+ public static function create (string $ host = '0.0.0.0 ' , int $ port = 9501 , array $ dependencies = [] ): App
36
36
{
37
- $ app = self ::createApp ();
37
+ $ app = self ::createApp ($ dependencies );
38
38
$ app ->config ([
39
39
'server ' => Preset::default (),
40
40
'server.servers.0.host ' => $ host ,
@@ -47,9 +47,9 @@ public static function create(string $host = '0.0.0.0', int $port = 9501): App
47
47
/**
48
48
* Create a single worker application in base mode, with max_requests = 0.
49
49
*/
50
- public static function createBase (string $ host = '0.0.0.0 ' , int $ port = 9501 ): App
50
+ public static function createBase (string $ host = '0.0.0.0 ' , int $ port = 9501 , array $ dependencies = [] ): App
51
51
{
52
- $ app = self ::createApp ();
52
+ $ app = self ::createApp ($ dependencies );
53
53
$ app ->config ([
54
54
'server ' => Preset::base (),
55
55
'server.servers.0.host ' => $ host ,
@@ -59,7 +59,21 @@ public static function createBase(string $host = '0.0.0.0', int $port = 9501): A
59
59
return $ app ;
60
60
}
61
61
62
- protected static function prepareContainer (): ContainerInterface
62
+ /**
63
+ * Create an application with a chosen preset.
64
+ */
65
+ public static function createApp (array $ dependencies = []): App
66
+ {
67
+ // Setting ini and flags
68
+ self ::prepareFlags ();
69
+
70
+ // Prepare container
71
+ $ container = self ::prepareContainer ($ dependencies );
72
+
73
+ return new App ($ container );
74
+ }
75
+
76
+ protected static function prepareContainer (array $ dependencies = []): ContainerInterface
63
77
{
64
78
$ config = new Config (ProviderConfig::load ());
65
79
$ config ->set (StdoutLoggerInterface::class, [
@@ -74,7 +88,8 @@ protected static function prepareContainer(): ContainerInterface
74
88
LogLevel::WARNING ,
75
89
],
76
90
]);
77
- $ container = new Container (new DefinitionSource ($ config ->get ('dependencies ' )));
91
+ $ dependencies = array_merge ($ config ->get ('dependencies ' , []), $ dependencies );
92
+ $ container = new Container (new DefinitionSource ($ dependencies ));
78
93
$ container ->set (ConfigInterface::class, $ config );
79
94
$ container ->define (DispatcherFactory::class, DispatcherFactory::class);
80
95
$ container ->define (BoundInterface::class, ContainerProxy::class);
@@ -96,18 +111,4 @@ protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL)
96
111
! defined ('BASE_PATH ' ) && define ('BASE_PATH ' , $ projectRootPath );
97
112
! defined ('SWOOLE_HOOK_FLAGS ' ) && define ('SWOOLE_HOOK_FLAGS ' , $ hookFlags );
98
113
}
99
-
100
- /**
101
- * Create an application with a chosen preset.
102
- */
103
- protected static function createApp (): App
104
- {
105
- // Setting ini and flags
106
- self ::prepareFlags ();
107
-
108
- // Prepare container
109
- $ container = self ::prepareContainer ();
110
-
111
- return new App ($ container );
112
- }
113
114
}
0 commit comments