generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from FaSe22/json
✨: add json support
- Loading branch information
Showing
6 changed files
with
125 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
// config for Fase22/Laramaid | ||
return [ | ||
|
||
'generate-json' => true, | ||
]; |
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,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; | ||
} | ||
} | ||
} |
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,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'] | ||
); | ||
} | ||
} |
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