File tree 8 files changed +63
-20
lines changed
8 files changed +63
-20
lines changed Original file line number Diff line number Diff line change 1
1
namespace Nuxed\Asset\Context ;
2
2
3
- class Context implements IContext {
4
- public function __construct (private string $basePath , private bool $secure ) {}
3
+ final class Context implements IContext {
4
+ private function __construct (
5
+ private string $basePath ,
6
+ private bool $secure ,
7
+ ) {}
8
+
9
+ public static function create (string $base_path , bool $secure ): Context {
10
+ return new Context ($base_path , $secure );
11
+ }
5
12
6
13
/**
7
14
* {@inheritdoc}
Original file line number Diff line number Diff line change
1
+ namespace Nuxed\Asset\Context ;
2
+
3
+ final class ContextProvider implements IContextProvider {
4
+ private function __construct (private IContext $context ) {}
5
+
6
+ public static function create (
7
+ string $base_path ,
8
+ bool $secure ,
9
+ ): ContextProvider {
10
+ return new ContextProvider (Context :: create($base_path , $secure ));
11
+ }
12
+
13
+ public static function forContext (IContext $context ): ContextProvider {
14
+ return new ContextProvider ($context );
15
+ }
16
+
17
+ public function getContext (): IContext {
18
+ return $this -> context ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ namespace Nuxed\Asset\Context ;
2
+
3
+ interface IContextProvider {
4
+ /**
5
+ * Provide context.
6
+ */
7
+ public function getContext (): IContext ;
8
+ }
Original file line number Diff line number Diff line change @@ -7,13 +7,19 @@ use namespace HH\Lib\Str;
7
7
*/
8
8
<<__Sealed (UrlPackage :: class , PathPackage :: class )>>
9
9
class Package implements IPackage {
10
- private Context \IContext $context ;
10
+ private ( function (): Context\IContext) $contextProvider ;
11
11
12
12
public function __construct (
13
13
private VersionStrategy \IVersionStrategy $versionStrategy ,
14
- ?Context \IContext $context = null ,
14
+ ?Context \IContextProvider $contextProvider = null ,
15
15
) {
16
- $this -> context = $context ?? new Context \NullContext ();
16
+ $this -> contextProvider = (): Context \IContext ==> {
17
+ if ($contextProvider is nonnull ) {
18
+ return $contextProvider -> getContext();
19
+ }
20
+
21
+ return new Context \NullContext ();
22
+ };
17
23
}
18
24
19
25
/**
@@ -35,7 +41,7 @@ class Package implements IPackage {
35
41
}
36
42
37
43
protected function getContext (): Context \IContext {
38
- return $this -> context ;
44
+ return ( $this -> contextProvider )() ;
39
45
}
40
46
41
47
protected function getVersionStrategy (): VersionStrategy \IVersionStrategy {
Original file line number Diff line number Diff line change 1
1
namespace Nuxed\Asset ;
2
2
3
3
use namespace HH\Lib\Str ;
4
- use type Nuxed\Asset\Context\IContext ;
5
4
use type Nuxed\Asset\VersionStrategy\IVersionStrategy ;
6
5
7
6
/**
@@ -21,10 +20,9 @@ final class PathPackage extends Package {
21
20
public function __construct (
22
21
string $basePath ,
23
22
IVersionStrategy $versionStrategy ,
24
- ?IContext $context = null ,
23
+ ?Context \ IContextProvider $contextProvider = null ,
25
24
) {
26
- $context ??= new Context \NullContext ();
27
- parent :: __construct($versionStrategy , $context );
25
+ parent :: __construct($versionStrategy , $contextProvider );
28
26
29
27
if (' ' === $basePath ) {
30
28
$this -> basePath = ' /' ;
Original file line number Diff line number Diff line change @@ -26,10 +26,9 @@ final class UrlPackage extends Package {
26
26
public function __construct (
27
27
Container <string > $baseUrls ,
28
28
VersionStrategy \IVersionStrategy $versionStrategy ,
29
- ?Context \IContext $context = null ,
29
+ ?Context \IContextProvider $contextProvider = null ,
30
30
) {
31
- $context ??= new Context \NullContext ();
32
- parent :: __construct($versionStrategy , $context );
31
+ parent :: __construct($versionStrategy , $contextProvider );
33
32
34
33
if (C \is_empty ($baseUrls )) {
35
34
throw new Exception \LogicException (
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ class PathPackageTest extends HackTest {
51
51
$package = new Asset \PathPackage (
52
52
$basePath ,
53
53
new Asset \VersionStrategy \StaticVersionStrategy (' v1' , $format ),
54
- $this -> getContext ($basePathRequest ),
54
+ $this -> getContextProvider ($basePathRequest ),
55
55
);
56
56
expect (await $package -> getUrl($path ))-> toBeSame($expected );
57
57
}
@@ -80,14 +80,17 @@ class PathPackageTest extends HackTest {
80
80
$package = new Asset \PathPackage (
81
81
' /subdirectory' ,
82
82
$versionStrategy ,
83
- $this -> getContext (' /bar' ),
83
+ $this -> getContextProvider (' /bar' ),
84
84
);
85
85
expect (await $package -> getUrl(' main.css' ))-> toBeSame(
86
86
' https://cdn.com/bar/main.css?' ,
87
87
);
88
88
}
89
89
90
- private function getContext (string $basePath ): Asset \Context \IContext {
91
- return new Asset \Context \Context ($basePath , false );
90
+ private function getContextProvider (
91
+ string $base_path ,
92
+ bool $secure = false ,
93
+ ): Asset \Context \IContextProvider {
94
+ return Asset \Context \ContextProvider :: create($base_path , $secure );
92
95
}
93
96
}
Original file line number Diff line number Diff line change @@ -148,7 +148,7 @@ class UrlPackageTest extends HackTest {
148
148
$package = new Asset \UrlPackage (
149
149
$baseUrls ,
150
150
new Asset \VersionStrategy \StaticVersionStrategy (' v1' ),
151
- $this -> getContext ($secure ),
151
+ $this -> getContextProvider ($secure ),
152
152
);
153
153
expect (await $package -> getUrl($path ))-> toBeSame($expected );
154
154
}
@@ -250,7 +250,9 @@ class UrlPackageTest extends HackTest {
250
250
];
251
251
}
252
252
253
- private function getContext (bool $secure ): Asset \Context \IContext {
254
- return new Asset \Context \Context (' ' , $secure );
253
+ private function getContextProvider (
254
+ bool $secure ,
255
+ ): Asset \Context \IContextProvider {
256
+ return Asset \Context \ContextProvider :: create(' ' , $secure );
255
257
}
256
258
}
You can’t perform that action at this time.
0 commit comments