generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add partial support for `spatie/laravel-settings` package
- Loading branch information
1 parent
37b9b4f
commit b5332be
Showing
16 changed files
with
313 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Kiwilan\Typescriptable\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Kiwilan\Typescriptable\Typescriptable; | ||
|
||
class TypescriptableSettingsCommand extends Command | ||
{ | ||
public $signature = 'typescriptable:settings | ||
{--settings-path : Path to settings directory} | ||
{--output-path : Path to output} | ||
{--extends : Extends class to parse}'; | ||
|
||
public $description = 'Generate Spatie Settings types.'; | ||
|
||
public function __construct( | ||
public ?string $settingsPath = null, | ||
public ?string $outputPath = null, | ||
public ?string $extends = null, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function handle(): int | ||
{ | ||
$this->settingsPath = (string) $this->option('settings-path'); | ||
$this->outputPath = (string) $this->option('output-path'); | ||
$this->extends = (string) $this->option('extends'); | ||
|
||
$service = Typescriptable::settings($this->settingsPath, $this->outputPath, $this->extends); | ||
// $namespaces = []; | ||
|
||
// foreach ($service->items as $item) { | ||
// $namespaces[] = [$item->namespace]; | ||
// } | ||
// $this->table(['Models'], $namespaces); | ||
|
||
$this->info('Generated settings types.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Kiwilan\Typescriptable\Typed\Setting; | ||
|
||
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentItem; | ||
use Kiwilan\Typescriptable\Typed\Utils\ClassItem; | ||
use ReflectionNamedType; | ||
use ReflectionProperty; | ||
|
||
class SettingItem | ||
{ | ||
/** | ||
* @property SettingItemProperty[] $properties | ||
*/ | ||
protected function __construct( | ||
public ClassItem $class, | ||
public string $name, | ||
public array $properties = [], | ||
) { | ||
} | ||
|
||
public static function make(ClassItem $class): self | ||
{ | ||
$properties = []; | ||
foreach ($class->reflect->getProperties() as $property) { | ||
if ($class->namespace === $property->class) { | ||
$item = SettingItemProperty::make($property); | ||
$properties[$item->name] = $item; | ||
} | ||
} | ||
|
||
$self = new self( | ||
class: $class, | ||
name: $class->name, | ||
properties: $properties, | ||
); | ||
|
||
return $self; | ||
} | ||
} | ||
|
||
class SettingItemProperty | ||
{ | ||
protected function __construct( | ||
public string $name, | ||
public string $type = 'mixed', | ||
public bool $isNullable = false, | ||
public bool $isBuiltin = false, | ||
public string $typeTs = 'any', | ||
) { | ||
} | ||
|
||
public static function make(ReflectionProperty $property): self | ||
{ | ||
$type = $property->getType(); | ||
$self = new self( | ||
name: $property->getName(), | ||
type: $type instanceof ReflectionNamedType ? $type->getName() : 'mixed', | ||
isNullable: $type->allowsNull(), | ||
isBuiltin: $type instanceof ReflectionNamedType ? $type->isBuiltin() : false, | ||
); | ||
|
||
$self->typeTs = EloquentItem::phpToTs($self->type); | ||
|
||
return $self; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Kiwilan\Typescriptable\Typed\Setting; | ||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
class SettingTypescript | ||
{ | ||
protected function __construct( | ||
public string $path, | ||
public string $content = '', | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string,SettingItem> $items | ||
*/ | ||
public static function make(array $items, string $path): self | ||
{ | ||
$self = new self($path); | ||
|
||
/** @var string[] */ | ||
$content = []; | ||
|
||
$content[] = '// This file is auto generated by TypescriptableLaravel.'; | ||
$content[] = 'declare namespace App {'; | ||
$content[] = ' declare namespace Settings {'; | ||
|
||
foreach ($items as $model => $item) { | ||
$content[] = " export type {$model} = {"; | ||
foreach ($item->properties as $field => $property) { | ||
$field = $property->isNullable ? "{$field}?" : $field; | ||
$content[] = " {$field}: {$property->typeTs}"; | ||
} | ||
$content[] = ' }'; | ||
} | ||
$content[] = ' }'; | ||
$content[] = '}'; | ||
|
||
$self->content = implode(PHP_EOL, $content); | ||
|
||
return $self; | ||
} | ||
|
||
public function print(): void | ||
{ | ||
File::put($this->path, $this->content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.