Skip to content

Implement splitting types per file per namespace #105

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 10 commits into
base: main
Choose a base branch
from
Open
8 changes: 5 additions & 3 deletions src/Actions/FormatTypeScriptAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class FormatTypeScriptAction
{
protected TypeScriptTransformerConfig $config;

public function __construct(TypeScriptTransformerConfig $config)
{
public function __construct(
TypeScriptTransformerConfig $config,
protected string $outputFile
) {
$this->config = $config;
}

Expand All @@ -21,6 +23,6 @@ public function execute(): void
return;
}

$formatter->format($this->config->getOutputFile());
$formatter->format($this->outputFile);
}
}
20 changes: 12 additions & 8 deletions src/Actions/PersistTypesCollectionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@ class PersistTypesCollectionAction
{
protected TypeScriptTransformerConfig $config;

public function __construct(TypeScriptTransformerConfig $config)
public function __construct(TypeScriptTransformerConfig $config, protected string $outputFile)
{
$this->config = $config;
}

public function execute(TypesCollection $collection): void
public function execute(TypesCollection $moduleCollection, ?TypesCollection $totalCollection = null): void
{
if ($totalCollection === null) {
$totalCollection = $moduleCollection;
}
$this->ensureOutputFileExists();

$writer = $this->config->getWriter();

(new ReplaceSymbolsInCollectionAction())->execute(
$collection,
(new ReplaceIntermoduleSymbolsInCollectionAction())->execute(
$moduleCollection,
$totalCollection,
$writer->replacesSymbolsWithFullyQualifiedIdentifiers()
);

file_put_contents(
$this->config->getOutputFile(),
$writer->format($collection)
$this->outputFile,
$writer->format($moduleCollection)
);
}

protected function ensureOutputFileExists(): void
{
if (! file_exists(pathinfo($this->config->getOutputFile(), PATHINFO_DIRNAME))) {
mkdir(pathinfo($this->config->getOutputFile(), PATHINFO_DIRNAME), 0755, true);
if (! file_exists(pathinfo($this->outputFile, PATHINFO_DIRNAME))) {
mkdir(pathinfo($this->outputFile, PATHINFO_DIRNAME), 0755, true);
}
}
}
24 changes: 24 additions & 0 deletions src/Actions/ReplaceIntermoduleSymbolsInCollectionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\TypeScriptTransformer\Actions;

use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
use Spatie\TypeScriptTransformer\Structures\TypesCollection;

class ReplaceIntermoduleSymbolsInCollectionAction
{
public function execute(
TypesCollection $moduleCollection,
TypesCollection $totalCollection,
$withFullyQualifiedNames = true
): TypesCollection
{
$replaceSymbolsInTypeAction = new ReplaceSymbolsInTypeAction($totalCollection, $withFullyQualifiedNames);

foreach ($moduleCollection as $type) {
$type->transformed = $replaceSymbolsInTypeAction->execute($type);
}

return $moduleCollection;
}
}
3 changes: 2 additions & 1 deletion src/Actions/ReplaceSymbolsInTypeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\TypeScriptTransformer\Exceptions\CircularDependencyChain;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
use Spatie\TypeScriptTransformer\Structures\TypesCollection;

class ReplaceSymbolsInTypeAction
Expand All @@ -18,7 +19,7 @@ public function __construct(TypesCollection $collection, $withFullyQualifiedName
$this->withFullyQualifiedNames = $withFullyQualifiedNames;
}

public function execute(TransformedType $type, array $chain = []): string
public function execute(TransformedType $type, array $chain = []): TranspilationResult
{
if (in_array($type->getTypeScriptName(), $chain)) {
$chain = array_merge($chain, [$type->getTypeScriptName()]);
Expand Down
93 changes: 93 additions & 0 deletions src/Actions/ResolveSplitTypesCollectionsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Spatie\TypeScriptTransformer\Actions;

use Exception;
use Generator;
use ReflectionClass;
use Spatie\TypeScriptTransformer\Exceptions\NoAutoDiscoverTypesPathsDefined;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\Structures\TypesCollection;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
use Symfony\Component\Finder\Finder;

class ResolveSplitTypesCollectionsAction
{
protected Finder $finder;

/** @var \Spatie\TypeScriptTransformer\Collectors\Collector[] */
protected array $collectors;

protected TypeScriptTransformerConfig $config;

public function __construct(Finder $finder, TypeScriptTransformerConfig $config)
{
$this->finder = $finder;

$this->config = $config;

$this->collectors = $config->getCollectors();
}

/**
* @return TypesCollection[]
* @throws NoAutoDiscoverTypesPathsDefined
*/
public function execute(): array
{
$collections = [];

$paths = $this->config->getAutoDiscoverTypesPaths();

if (empty($paths)) {
throw NoAutoDiscoverTypesPathsDefined::create();
}

foreach ($this->resolveIterator($paths) as $class) {
$transformedType = $this->resolveTransformedType($class);

if ($transformedType === null) {
continue;
}
$namespace = implode('/', $transformedType->getNamespaceSegments());
if (!isset($collections[$namespace])) {
$collections[$namespace] = new TypesCollection();
}
$collections[$namespace][] = $transformedType;
}

return $collections;
}

protected function resolveIterator(array $paths): Generator
{
$paths = array_map(
fn (string $path) => is_dir($path) ? $path : dirname($path),
$paths
);

foreach ($this->finder->in($paths) as $fileInfo) {
try {
$classes = (new ResolveClassesInPhpFileAction())->execute($fileInfo);

foreach ($classes as $name) {
yield $name => new ReflectionClass($name);
}
} catch (Exception $exception) {
}
}
}

protected function resolveTransformedType(ReflectionClass $class): ?TransformedType
{
foreach ($this->collectors as $collector) {
$transformedType = $collector->getTransformedType($class);

if ($transformedType !== null) {
return $transformedType;
}
}

return null;
}
}
Loading