Skip to content

Commit

Permalink
Merge pull request #1 from FaSe22/json
Browse files Browse the repository at this point in the history
✨:  add json support
  • Loading branch information
FaSe22 authored Dec 22, 2024
2 parents 25830e1 + e847f3b commit 9bd0966
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/laramaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

// config for Fase22/Laramaid
return [

'generate-json' => true,
];
42 changes: 42 additions & 0 deletions src/Commands/GenerateFromJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Fase22\Laramaid\Commands;

use Fase22\Laramaid\Json\Rehydrator;
use Fase22\Laramaid\Laravel\LaravelClassGenerator;
use Illuminate\Console\Command;

class GenerateFromJson extends Command
{
public $signature = 'laramaid:json {json_file}';

public $description = 'Generate Laravel classes from Mermaid class diagram';

public function handle(
LaravelClassGenerator $generator,
Rehydrator $rehydrator
): int {
$jsonFilePath = $this->argument('json_file');

if (! file_exists($jsonFilePath)) {
$this->error('Error: Json file not found');

return self::FAILURE;
}

try {
$json = json_decode(file_get_contents($jsonFilePath), true);
$namespaceData = $rehydrator->rehydrate($json);

$generator->generate('./', $namespaceData);

$this->info('Done!');

return self::SUCCESS;
} catch (\Exception $e) {
$this->error($e->getMessage());

return self::FAILURE;
}
}
}
5 changes: 5 additions & 0 deletions src/Commands/GenerateMermaidDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function handle(

try {
$namespaces = $extractor->extractFromDirectory($targetDirectory);

if (config('laramaid.generate-json')) {
file_put_contents('laramaid_generated.json', json_encode($namespaces));
}

$mermaidContent = $generator->generate($namespaces);

if ($outputFile) {
Expand Down
4 changes: 4 additions & 0 deletions src/Commands/LaramaidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function handle(
$parser = new MermaidParser($content);
$namespaceData = $parser->parse()->getNamespaces();

if (config('laramaid.generate-json')) {
file_put_contents('laramaid.json', json_encode($namespaceData));
}

$generator->generate('./', $namespaceData);

$this->info('Done!');
Expand Down
66 changes: 66 additions & 0 deletions src/Json/Rehydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Fase22\Laramaid\Json;

use Fase22\Laramaid\Mermaid\MermaidClass;
use Fase22\Laramaid\Mermaid\MermaidMethod;
use Fase22\Laramaid\Mermaid\MermaidParameter;
use Fase22\Laramaid\Mermaid\MermaidProperty;

class Rehydrator
{
public static function rehydrate(array $data): array
{
$result = [];

foreach ($data as $category => $items) {
$result[$category] = array_map(
fn ($item) => self::rehydrateMermaidClass($item),
$items
);
}

return $result;
}

public static function rehydrateMermaidClass(array $data): MermaidClass
{
$methods = array_map(fn ($method) => self::rehydrateMermaidMethod($method), $data['methods'] ?? []);
$properties = array_map(fn ($property) => self::rehydrateMermaidProperty($property), $data['properties'] ?? []);

return new MermaidClass(
$data['name'],
$methods,
$properties
);
}

private static function rehydrateMermaidMethod(array $data): MermaidMethod
{
$parameters = array_map(fn ($param) => self::rehydrateMermaidParameter($param), $data['parameters'] ?? []);

return new MermaidMethod(
$data['name'],
$data['visibility'],
$parameters,
$data['returnType']
);
}

private static function rehydrateMermaidParameter(array $data): MermaidParameter
{
return new MermaidParameter(
$data['name'],
$data['type']
);
}

private static function rehydrateMermaidProperty(array $data): MermaidProperty
{
return new MermaidProperty(
$data['name'],
$data['visibility'],
$data['type']
);
}
}
8 changes: 7 additions & 1 deletion src/LaramaidServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fase22\Laramaid;

use Fase22\Laramaid\Commands\GenerateFromJson;
use Fase22\Laramaid\Commands\GenerateMermaidDiagram;
use Fase22\Laramaid\Commands\LaramaidCommand;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -18,7 +19,7 @@ public function configurePackage(Package $package): void
*/
$package
->name('laramaid')
->hasConfigFile()
->hasConfigFile('laramaid')
->hasViews()
->hasMigration('create_laramaid_table')
->hasCommand(LaramaidCommand::class);
Expand All @@ -30,7 +31,12 @@ public function boot(): void
$this->commands([
LaramaidCommand::class,
GenerateMermaidDiagram::class,
GenerateFromJson::class,
]);
}

$this->publishes([
__DIR__.'/../config/laramaid.php' => config_path('laramaid.php'),
]);
}
}

0 comments on commit 9bd0966

Please sign in to comment.