2
2
3
3
namespace Spatie \TypeScriptTransformer \Writers ;
4
4
5
+ use Spatie \TypeScriptTransformer \Compactors \Compactor ;
6
+ use Spatie \TypeScriptTransformer \Compactors \ConfigCompactor ;
5
7
use Spatie \TypeScriptTransformer \Structures \NamespacedType ;
6
8
use Spatie \TypeScriptTransformer \Structures \TransformedType ;
7
9
use Spatie \TypeScriptTransformer \Structures \TypesCollection ;
10
+ use Spatie \TypeScriptTransformer \TypeScriptTransformerConfig ;
8
11
9
12
class ModuleWriter implements Writer
10
13
{
14
+
15
+ protected Compactor $ compactor ;
16
+
17
+ public function __construct (TypeScriptTransformerConfig $ config ) {
18
+ $ this ->compactor = new ConfigCompactor ($ config );
19
+ }
20
+
11
21
public function format (TypesCollection $ collection ): string {
12
22
$ output = '' ;
13
23
@@ -18,16 +28,23 @@ public function format(TypesCollection $collection): string {
18
28
return strcmp ($ a ->name , $ b ->name );
19
29
});
20
30
21
- $ currentModuleNamespace = null ;
31
+ $ currentModuleTsNamespace = null ;
22
32
/** @var NamespacedType[] $typesByNamespace */
23
33
$ typesByNamespace = [];
24
34
foreach ($ iterator as $ type ) {
25
35
/** @var TransformedType $type */
26
36
if ($ type ->isInline ) {
27
37
continue ;
28
38
}
29
- if ($ currentModuleNamespace === null ) {
30
- $ currentModuleNamespace = trim (NamespacedType::namespace ($ type ->reflection ->name ), '\\' );
39
+
40
+ if ($ currentModuleTsNamespace === null ) {
41
+ $ currentModuleTsNamespace =
42
+ $ this ->compactor ->removePrefix (
43
+ trim (
44
+ NamespacedType::namespace ($ type ->reflection ->name ),
45
+ '\\'
46
+ )
47
+ );
31
48
}
32
49
33
50
$ output .= "export {$ type ->toString ()}" . PHP_EOL ;
@@ -42,8 +59,11 @@ public function format(TypesCollection $collection): string {
42
59
43
60
$ import = '' ;
44
61
foreach ($ typesByNamespace as $ namespace => $ types ) {
45
- $ namespace = trim ($ namespace , '\\' );
46
- if ($ namespace === $ currentModuleNamespace ) {
62
+ $ tsNamespace = $ this ->compactor ->removePrefix (
63
+ trim ($ namespace , '\\' )
64
+ );
65
+
66
+ if ($ tsNamespace === $ currentModuleTsNamespace ) {
47
67
continue ;
48
68
}
49
69
$ import .= 'import { ' ;
@@ -54,9 +74,9 @@ public function format(TypesCollection $collection): string {
54
74
$ types
55
75
)
56
76
);
57
- $ commonPrefix = NamespacedType::commonPrefix ($ namespace , $ currentModuleNamespace );
58
- $ thatRest = ltrim (substr ($ namespace , strlen ($ commonPrefix )), '\\' );
59
- $ currentRest = ltrim (substr ($ currentModuleNamespace , strlen ($ commonPrefix )), '\\' );
77
+ $ commonPrefix = NamespacedType::commonPrefix ($ tsNamespace , $ currentModuleTsNamespace );
78
+ $ thatRest = ltrim (substr ($ tsNamespace , strlen ($ commonPrefix )), '\\' );
79
+ $ currentRest = ltrim (substr ($ currentModuleTsNamespace , strlen ($ commonPrefix )), '\\' );
60
80
$ sourceModulePath =
61
81
join (
62
82
'/ ' ,
0 commit comments