1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use Formularium\Exception\Exception; |
6
|
|
|
use Formularium\Factory\DatatypeFactory; |
7
|
|
|
use Formularium\Factory\FrameworkFactory; |
8
|
|
|
use Formularium\Factory\RenderableFactory; |
9
|
|
|
use Formularium\Frontend\Blade\Framework; |
10
|
|
|
use FormulariumTests\DatatypeTest; |
11
|
|
|
use Illuminate\Console\Command; |
12
|
|
|
|
13
|
|
|
class CommandRenderable extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'formularium:renderable |
21
|
|
|
{name : The datatype name} |
22
|
|
|
{--framework=* : The frameworks to use. You can use this options several times. Use "*" for all.} |
23
|
|
|
{--namespace=Formularium\\Frontend : base namespace. Defaults to Formularium\\Frontend } |
24
|
|
|
{--path= : path to save the file. Defaults to base_path("app/Frontend") } |
25
|
|
|
'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Creates renderable scaffolding'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new command instance. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function handle() |
50
|
|
|
{ |
51
|
|
|
$frameworkNames = $this->option('framework'); |
52
|
|
|
if (is_string($frameworkNames)) { |
|
|
|
|
53
|
|
|
$frameworkNames = [$frameworkNames]; |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* @var array $frameworkNames |
57
|
|
|
*/ |
58
|
|
|
if (in_array('*', $frameworkNames)) { |
59
|
|
|
$frameworks = FrameworkFactory::factoryAll(); |
60
|
|
|
} else { |
61
|
|
|
$frameworks = array_map( |
62
|
|
|
[FrameworkFactory::class, 'factory'], |
63
|
|
|
$frameworkNames |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$name = $this->argument('name'); |
68
|
|
|
if (!is_string($name)) { |
69
|
|
|
$this->error('Name must be a string'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
$datatype = DatatypeFactory::factory($name); |
73
|
|
|
$datatypeLower = mb_strtolower($datatype->getName()); |
74
|
|
|
$baseNamespace = $this->option('namespace'); |
75
|
|
|
if (!is_string($baseNamespace)) { |
|
|
|
|
76
|
|
|
$this->error('namespace must be a string'); |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$basePath = $this->option('path'); |
81
|
|
|
if (!is_string($basePath)) { |
|
|
|
|
82
|
|
|
$this->error('path must be a string'); |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$printer = new \Nette\PhpGenerator\PsrPrinter(); |
87
|
|
|
|
88
|
|
|
foreach ($frameworks as $framework) { |
89
|
|
|
/** |
90
|
|
|
* @var Framework $framework |
91
|
|
|
*/ |
92
|
|
|
$phpns = $framework->generateRenderable($datatype, $baseNamespace); |
93
|
|
|
$code = "<?php declare(strict_types=1);\n" . $printer->printNamespace($phpns); |
94
|
|
|
$basepath = $basePath . '/' . $framework->getName() . '/Renderable/'; |
95
|
|
|
if (!is_dir($basepath)) { |
96
|
|
|
\Safe\mkdir($basepath, 0777, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$filename = $basepath . 'Renderable_' . $datatypeLower . '.php'; |
100
|
|
|
if (!file_exists($filename)) { |
101
|
|
|
$this->info("Created renderable at {$filename}."); |
102
|
|
|
file_put_contents($filename, $code); |
103
|
|
|
} else { |
104
|
|
|
$this->warn("Filename $filename already exists."); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|