Skip to content

Introduce Compactor to shorten output namespaces and type names #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Collectors/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
namespace Spatie\TypeScriptTransformer\Collectors;

use ReflectionClass;
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

abstract class Collector
{
protected TypeScriptTransformerConfig $config;

protected ConfigCompactor $compactor;

public function __construct(TypeScriptTransformerConfig $config)
{
$this->config = $config;
$this->compactor = new ConfigCompactor($config);
}

abstract public function getTransformedType(ReflectionClass $class): ?TransformedType;
Expand Down
1 change: 1 addition & 0 deletions src/Collectors/DefaultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected function resolveAlreadyTransformedType(ClassTypeReflector $reflector):
$reflector->getReflectionClass(),
$reflector->getName(),
$transpiler->execute($reflector->getType()),
$this->compactor,
$missingSymbols
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Compactors/Compactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\TypeScriptTransformer\Compactors;

/**
* Shortens namespace of a typescript identifier
*/
interface Compactor
{
public function removeSuffix(string $typeName): string;

public function removePrefix(string $namespace): string;
}
74 changes: 74 additions & 0 deletions src/Compactors/ConfigCompactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Spatie\TypeScriptTransformer\Compactors;

use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

class ConfigCompactor implements Compactor
{

protected ?array $prefixes = null;

protected ?array $suffixes = null;

protected TypeScriptTransformerConfig $config;

public function __construct(TypeScriptTransformerConfig $config) {
$this->config = $config;
}

/**
* @return string[]
*/
protected function getPrefixes(): array {
if ($this->prefixes === null) {
$this->prefixes = array_map(
function (string $prefix): string {
if (str_ends_with($prefix, "\\")) {
$prefix = rtrim($prefix, "\\");
}
return $prefix;
},
$this->config->getCompactorPrefixes()
);
}
return $this->prefixes;
}

/**
* @return string[]
*/
protected function getSuffixes(): array {
if ($this->suffixes === null) {
$this->suffixes = $this->config->getCompactorSuffixes();
}
return $this->suffixes;
}

public function removeSuffix(string $typeName): string {
$matchingSuffix = '';
foreach ($this->getSuffixes() as $suffix) {
if (str_ends_with($typeName, $suffix)) {
$matchingSuffix = $suffix;
break;
}
}
if ($matchingSuffix !== '') {
$typeName = substr($typeName, 0, -strlen($matchingSuffix));
}
return $typeName;
}

public function removePrefix(string $namespace): string {
$matchingPrefix = '';
foreach ($this->getPrefixes() as $prefix) {
if (str_starts_with($namespace, $prefix)) {
$matchingPrefix = $prefix;
break;
}
}
$substr = substr($namespace, strlen($matchingPrefix));
return ltrim($substr, '\\');
}

}
15 changes: 15 additions & 0 deletions src/Compactors/IdentityCompactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\TypeScriptTransformer\Compactors;

class IdentityCompactor implements Compactor
{

public function removeSuffix(string $typeName): string {
return $typeName;
}

public function removePrefix(string $namespace): string {
return $namespace;
}
}
17 changes: 15 additions & 2 deletions src/Structures/TransformedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\TypeScriptTransformer\Structures;

use ReflectionClass;
use Spatie\TypeScriptTransformer\Compactors\Compactor;

class TransformedType
{
Expand All @@ -14,6 +15,8 @@ class TransformedType

public MissingSymbolsCollection $missingSymbols;

public Compactor $compactor;

public bool $isInline;

public string $keyword;
Expand All @@ -24,26 +27,29 @@ public static function create(
ReflectionClass $class,
string $name,
string $transformed,
Compactor $compactor,
?MissingSymbolsCollection $missingSymbols = null,
bool $inline = false,
string $keyword = 'type',
bool $trailingSemicolon = true,
): self {
return new self($class, $name, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
return new self($class, $compactor->removeSuffix($name), $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
}

public static function createInline(
ReflectionClass $class,
string $transformed,
Compactor $compactor,
?MissingSymbolsCollection $missingSymbols = null
): self {
return new self($class, null, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), true);
return new self($class, null, $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), true);
}

public function __construct(
ReflectionClass $class,
?string $name,
string $transformed,
Compactor $compactor,
MissingSymbolsCollection $missingSymbols,
bool $isInline,
string $keyword = 'type',
Expand All @@ -53,6 +59,7 @@ public function __construct(
$this->name = $name;
$this->transformed = $transformed;
$this->missingSymbols = $missingSymbols;
$this->compactor = $compactor;
$this->isInline = $isInline;
$this->keyword = $keyword;
$this->trailingSemicolon = $trailingSemicolon;
Expand All @@ -70,6 +77,12 @@ public function getNamespaceSegments(): array
return [];
}

$namespace = $this->compactor->removePrefix($namespace);

if ($namespace === '') {
return [];
}

return explode('\\', $namespace);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Transformers/DtoTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ReflectionProperty;
use Spatie\TypeScriptTransformer\Attributes\Hidden;
use Spatie\TypeScriptTransformer\Attributes\Optional;
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeProcessors\DtoCollectionTypeProcessor;
Expand All @@ -18,9 +19,12 @@ class DtoTransformer implements Transformer

protected TypeScriptTransformerConfig $config;

protected ConfigCompactor $compactor;

public function __construct(TypeScriptTransformerConfig $config)
{
$this->config = $config;
$this->compactor = new ConfigCompactor($config);
}

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

$missingSymbols = new MissingSymbolsCollection();

$type = join([
$this->transformProperties($class, $missingSymbols),
$this->transformMethods($class, $missingSymbols),
Expand All @@ -41,6 +44,7 @@ public function transform(ReflectionClass $class, string $name): ?TransformedTyp
$class,
$name,
"{" . PHP_EOL . $type . "}",
$this->compactor,
$missingSymbols
);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Transformers/EnumTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use ReflectionClass;
use ReflectionEnum;
use ReflectionEnumBackedCase;
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

class EnumTransformer implements Transformer
{
protected ConfigCompactor $compactor;

public function __construct(protected TypeScriptTransformerConfig $config)
{
$this->compactor = new ConfigCompactor($config);
}

public function transform(ReflectionClass $class, string $name): ?TransformedType
Expand Down Expand Up @@ -46,6 +50,7 @@ protected function toEnum(ReflectionEnum $enum, string $name): TransformedType
$enum,
$name,
implode(', ', $options),
$this->compactor,
keyword: 'enum'
);
}
Expand All @@ -60,7 +65,8 @@ protected function toType(ReflectionEnum $enum, string $name): TransformedType
return TransformedType::create(
$enum,
$name,
implode(' | ', $options)
implode(' | ', $options),
$this->compactor
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/Transformers/MyclabsEnumTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use MyCLabs\Enum\Enum;
use ReflectionClass;
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

class MyclabsEnumTransformer implements Transformer
{
protected ConfigCompactor $compactor;

public function __construct(protected TypeScriptTransformerConfig $config)
{
$this->compactor = new ConfigCompactor($config);
}

public function transform(ReflectionClass $class, string $name): ?TransformedType
Expand Down Expand Up @@ -39,6 +43,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
$class,
$name,
implode(', ', $options),
$this->compactor,
keyword: 'enum'
);
}
Expand All @@ -56,7 +61,8 @@ protected function toType(ReflectionClass $class, string $name): TransformedType
return TransformedType::create(
$class,
$name,
implode(' | ', $options)
implode(' | ', $options),
$this->compactor
);
}
}
8 changes: 7 additions & 1 deletion src/Transformers/SpatieEnumTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use ReflectionClass;
use Spatie\Enum\Enum;
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

class SpatieEnumTransformer implements Transformer
{
protected ConfigCompactor $compactor;

public function __construct(protected TypeScriptTransformerConfig $config)
{
$this->compactor = new ConfigCompactor($config);
}

public function transform(ReflectionClass $class, string $name): ?TransformedType
Expand Down Expand Up @@ -39,6 +43,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
$class,
$name,
implode(', ', $options),
$this->compactor,
keyword: 'enum'
);
}
Expand All @@ -56,7 +61,8 @@ private function toType(ReflectionClass $class, string $name): TransformedType
return TransformedType::create(
$class,
$name,
implode(' | ', $options)
implode(' | ', $options),
$this->compactor
);
}
}
Loading