Skip to content

Commit ee44437

Browse files
committed
Draft compactor integration
1 parent 4e2a4d4 commit ee44437

16 files changed

+268
-10
lines changed

src/Collectors/Collector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
namespace Spatie\TypeScriptTransformer\Collectors;
44

55
use ReflectionClass;
6+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
67
use Spatie\TypeScriptTransformer\Structures\TransformedType;
78
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
89

910
abstract class Collector
1011
{
1112
protected TypeScriptTransformerConfig $config;
1213

14+
protected ConfigCompactor $compactor;
15+
1316
public function __construct(TypeScriptTransformerConfig $config)
1417
{
1518
$this->config = $config;
19+
$this->compactor = new ConfigCompactor($config);
1620
}
1721

1822
abstract public function getTransformedType(ReflectionClass $class): ?TransformedType;

src/Collectors/DefaultCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function resolveAlreadyTransformedType(ClassTypeReflector $reflector):
4949
$reflector->getReflectionClass(),
5050
$reflector->getName(),
5151
$transpiler->execute($reflector->getType()),
52+
$this->compactor,
5253
$missingSymbols
5354
);
5455
}

src/Compactors/Compactor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
/**
6+
* Shortens namespace of a typescript identifier
7+
*/
8+
interface Compactor
9+
{
10+
public function compact(string $typescriptIdentifier): string;
11+
}

src/Compactors/ConfigCompactor.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
6+
7+
class ConfigCompactor implements Compactor
8+
{
9+
10+
protected ?array $prefixes = null;
11+
12+
protected ?array $suffixes = null;
13+
14+
protected TypeScriptTransformerConfig $config;
15+
16+
public function __construct(TypeScriptTransformerConfig $config) {
17+
$this->config = $config;
18+
}
19+
20+
/**
21+
* @return string[]
22+
*/
23+
protected function getPrefixes(): array {
24+
if ($this->prefixes === null) {
25+
$this->prefixes = array_map(
26+
function(string $prefix): string {
27+
$prefix = str_replace("\\", ".", $prefix);
28+
if (!str_ends_with($prefix, ".")) {
29+
$prefix .= ".";
30+
}
31+
return $prefix;
32+
},
33+
$this->config->getCompactorPrefixes()
34+
);
35+
}
36+
return $this->prefixes;
37+
}
38+
39+
/**
40+
* @return string[]
41+
*/
42+
protected function getSuffixes(): array {
43+
if ($this->suffixes === null) {
44+
$this->suffixes = $this->config->getCompactorSuffixes();
45+
}
46+
return $this->suffixes;
47+
}
48+
49+
public function compact(
50+
string $typescriptIdentifier
51+
): string {
52+
$matchingPrefix = '';
53+
$matchingSuffix = '';
54+
foreach ($this->getPrefixes() as $prefix) {
55+
if (str_starts_with($typescriptIdentifier, $prefix)) {
56+
$matchingPrefix = $prefix;
57+
break;
58+
}
59+
}
60+
foreach ($this->getSuffixes() as $suffix) {
61+
if (str_ends_with($typescriptIdentifier, $suffix)) {
62+
$matchingSuffix = $suffix;
63+
break;
64+
}
65+
}
66+
if ($matchingSuffix !== '') {
67+
$typescriptIdentifier = substr($typescriptIdentifier, 0, -strlen($matchingSuffix));
68+
}
69+
$substr = substr($typescriptIdentifier, strlen($matchingPrefix));
70+
return $substr;
71+
}
72+
73+
}

src/Compactors/IdentityCompactor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
class IdentityCompactor implements Compactor
6+
{
7+
8+
public function compact(string $typescriptIdentifier): string {
9+
return $typescriptIdentifier;
10+
}
11+
12+
}

src/Structures/TransformedType.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\TypeScriptTransformer\Structures;
44

55
use ReflectionClass;
6+
use Spatie\TypeScriptTransformer\Compactors\Compactor;
67

78
class TransformedType
89
{
@@ -14,6 +15,8 @@ class TransformedType
1415

1516
public MissingSymbolsCollection $missingSymbols;
1617

18+
public Compactor $compactor;
19+
1720
public bool $isInline;
1821

1922
public string $keyword;
@@ -24,26 +27,29 @@ public static function create(
2427
ReflectionClass $class,
2528
string $name,
2629
string $transformed,
30+
Compactor $compactor,
2731
?MissingSymbolsCollection $missingSymbols = null,
2832
bool $inline = false,
2933
string $keyword = 'type',
3034
bool $trailingSemicolon = true,
3135
): self {
32-
return new self($class, $name, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
36+
return new self($class, $compactor->compact($name), $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
3337
}
3438

3539
public static function createInline(
3640
ReflectionClass $class,
3741
string $transformed,
42+
Compactor $compactor,
3843
?MissingSymbolsCollection $missingSymbols = null
3944
): self {
40-
return new self($class, null, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), true);
45+
return new self($class, null, $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), true);
4146
}
4247

4348
public function __construct(
4449
ReflectionClass $class,
4550
?string $name,
4651
string $transformed,
52+
Compactor $compactor,
4753
MissingSymbolsCollection $missingSymbols,
4854
bool $isInline,
4955
string $keyword = 'type',
@@ -53,6 +59,7 @@ public function __construct(
5359
$this->name = $name;
5460
$this->transformed = $transformed;
5561
$this->missingSymbols = $missingSymbols;
62+
$this->compactor = $compactor;
5663
$this->isInline = $isInline;
5764
$this->keyword = $keyword;
5865
$this->trailingSemicolon = $trailingSemicolon;
@@ -84,7 +91,9 @@ public function getTypeScriptName($fullyQualified = true): string
8491
[$this->name]
8592
);
8693

87-
return implode('.', $segments);
94+
return $this->compactor->compact(
95+
implode('.', $segments)
96+
);
8897
}
8998

9099
public function replaceSymbol(string $class, string $replacement): void

src/Transformers/DtoTransformer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ReflectionProperty;
77
use Spatie\TypeScriptTransformer\Attributes\Hidden;
88
use Spatie\TypeScriptTransformer\Attributes\Optional;
9+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
910
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
1011
use Spatie\TypeScriptTransformer\Structures\TransformedType;
1112
use Spatie\TypeScriptTransformer\TypeProcessors\DtoCollectionTypeProcessor;
@@ -18,9 +19,12 @@ class DtoTransformer implements Transformer
1819

1920
protected TypeScriptTransformerConfig $config;
2021

22+
protected ConfigCompactor $compactor;
23+
2124
public function __construct(TypeScriptTransformerConfig $config)
2225
{
2326
$this->config = $config;
27+
$this->compactor = new ConfigCompactor($config);
2428
}
2529

2630
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -30,7 +34,6 @@ public function transform(ReflectionClass $class, string $name): ?TransformedTyp
3034
}
3135

3236
$missingSymbols = new MissingSymbolsCollection();
33-
3437
$type = join([
3538
$this->transformProperties($class, $missingSymbols),
3639
$this->transformMethods($class, $missingSymbols),
@@ -41,6 +44,7 @@ public function transform(ReflectionClass $class, string $name): ?TransformedTyp
4144
$class,
4245
$name,
4346
"{" . PHP_EOL . $type . "}",
47+
$this->compactor,
4448
$missingSymbols
4549
);
4650
}

src/Transformers/EnumTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
use ReflectionClass;
66
use ReflectionEnum;
77
use ReflectionEnumBackedCase;
8+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
89
use Spatie\TypeScriptTransformer\Structures\TransformedType;
910
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
1011

1112
class EnumTransformer implements Transformer
1213
{
14+
protected ConfigCompactor $compactor;
15+
1316
public function __construct(protected TypeScriptTransformerConfig $config)
1417
{
18+
$this->compactor = new ConfigCompactor($config);
1519
}
1620

1721
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -46,6 +50,7 @@ protected function toEnum(ReflectionEnum $enum, string $name): TransformedType
4650
$enum,
4751
$name,
4852
implode(', ', $options),
53+
$this->compactor,
4954
keyword: 'enum'
5055
);
5156
}
@@ -60,7 +65,8 @@ protected function toType(ReflectionEnum $enum, string $name): TransformedType
6065
return TransformedType::create(
6166
$enum,
6267
$name,
63-
implode(' | ', $options)
68+
implode(' | ', $options),
69+
$this->compactor
6470
);
6571
}
6672

src/Transformers/MyclabsEnumTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
use MyCLabs\Enum\Enum;
66
use ReflectionClass;
7+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
78
use Spatie\TypeScriptTransformer\Structures\TransformedType;
89
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
910

1011
class MyclabsEnumTransformer implements Transformer
1112
{
13+
protected ConfigCompactor $compactor;
14+
1215
public function __construct(protected TypeScriptTransformerConfig $config)
1316
{
17+
$this->compactor = new ConfigCompactor($config);
1418
}
1519

1620
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -39,6 +43,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
3943
$class,
4044
$name,
4145
implode(', ', $options),
46+
$this->compactor,
4247
keyword: 'enum'
4348
);
4449
}
@@ -56,7 +61,8 @@ protected function toType(ReflectionClass $class, string $name): TransformedType
5661
return TransformedType::create(
5762
$class,
5863
$name,
59-
implode(' | ', $options)
64+
implode(' | ', $options),
65+
$this->compactor
6066
);
6167
}
6268
}

src/Transformers/SpatieEnumTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
use ReflectionClass;
66
use Spatie\Enum\Enum;
7+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
78
use Spatie\TypeScriptTransformer\Structures\TransformedType;
89
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
910

1011
class SpatieEnumTransformer implements Transformer
1112
{
13+
protected ConfigCompactor $compactor;
14+
1215
public function __construct(protected TypeScriptTransformerConfig $config)
1316
{
17+
$this->compactor = new ConfigCompactor($config);
1418
}
1519

1620
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -39,6 +43,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
3943
$class,
4044
$name,
4145
implode(', ', $options),
46+
$this->compactor,
4247
keyword: 'enum'
4348
);
4449
}
@@ -56,7 +61,8 @@ private function toType(ReflectionClass $class, string $name): TransformedType
5661
return TransformedType::create(
5762
$class,
5863
$name,
59-
implode(' | ', $options)
64+
implode(' | ', $options),
65+
$this->compactor
6066
);
6167
}
6268
}

0 commit comments

Comments
 (0)