File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed
Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,10 @@ class Helpers
2222 ];
2323
2424
25+ /** @var Http\IClient */
26+ private static $ client ;
27+
28+
2529 /**
2630 * @param mixed
2731 * @return string
@@ -80,13 +84,18 @@ public static function jsonDecode($json)
8084
8185
8286 /**
87+ * @param bool
8388 * @return Http\IClient
8489 */
85- public static function createDefaultClient ()
90+ public static function createDefaultClient ($ newInstance = FALSE )
8691 {
87- return extension_loaded ('curl ' )
88- ? new Http \CurlClient
89- : new Http \StreamClient ;
92+ if (self ::$ client === NULL || $ newInstance ) {
93+ self ::$ client = extension_loaded ('curl ' )
94+ ? new Http \CurlClient
95+ : new Http \StreamClient ;
96+ }
97+
98+ return self ::$ client ;
9099 }
91100
92101}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * @author Miloslav Hůla
5+ */
6+
7+ require __DIR__ . '/../bootstrap.php ' ;
8+
9+
10+ $ client1 = Milo \Github \Helpers::createDefaultClient ();
11+ Assert::type ('Milo\Github\Http\IClient ' , $ client1 );
12+
13+ $ client2 = Milo \Github \Helpers::createDefaultClient ();
14+ Assert::type ('Milo\Github\Http\IClient ' , $ client2 );
15+
16+ Assert::same ($ client1 , $ client2 );
17+
18+
19+ $ client3 = Milo \Github \Helpers::createDefaultClient (TRUE );
20+ Assert::type ('Milo\Github\Http\IClient ' , $ client3 );
21+
22+ Assert::notSame ($ client1 , $ client3 );
23+ Assert::notSame ($ client2 , $ client3 );
You can’t perform that action at this time.
0 commit comments