1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Frontend; |
4
|
|
|
|
5
|
|
|
use Formularium\Model; |
6
|
|
|
use Formularium\FrameworkComposer; |
7
|
|
|
use Formularium\Frontend\Blade\Framework as FrameworkBlade; |
8
|
|
|
use Formularium\Frontend\Vue\Framework as FrameworkVue; |
9
|
|
|
use Modelarium\GeneratedCollection; |
10
|
|
|
use Modelarium\GeneratedItem; |
11
|
|
|
use Modelarium\GeneratorInterface; |
12
|
|
|
|
13
|
|
|
use function Safe\file_get_contents; |
14
|
|
|
|
15
|
|
|
class FrontendGenerator implements GeneratorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FrameworkComposer |
19
|
|
|
*/ |
20
|
|
|
protected $composer = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Model |
24
|
|
|
*/ |
25
|
|
|
protected $model = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var GeneratedCollection |
29
|
|
|
*/ |
30
|
|
|
protected $collection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $stubDir = __DIR__ . '/stubs'; |
37
|
|
|
|
38
|
|
|
public function __construct(FrameworkComposer $composer, Model $model) |
39
|
|
|
{ |
40
|
|
|
$this->composer = $composer; |
41
|
|
|
$this->model = $model; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function generate(): GeneratedCollection |
45
|
|
|
{ |
46
|
|
|
$this->collection = new GeneratedCollection(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FrameworkVue $vue |
50
|
|
|
*/ |
51
|
|
|
$vue = $this->composer->getByName('Vue'); |
52
|
|
|
// $blade = FrameworkComposer::getByName('Blade'); |
53
|
|
|
|
54
|
|
|
if ($vue !== null) { |
55
|
|
|
$this->makeVue($vue, 'Base', 'viewable'); |
56
|
|
|
$this->makeVue($vue, 'Card', 'viewable'); |
57
|
|
|
$this->makeVue($vue, 'List', 'viewable'); |
58
|
|
|
$this->makeVue($vue, 'View', 'viewable'); |
59
|
|
|
$this->makeVue($vue, 'Form', 'editable'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->collection; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function makeVue(FrameworkVue $vue, string $component, string $mode): void |
66
|
|
|
{ |
67
|
|
|
$path = 'resources/js/components/' . $this->model->getName() . '/' . $component . '.vue'; |
68
|
|
|
$stub = file_get_contents($this->stubDir . "/Vue{$component}.stub.vue"); |
69
|
|
|
if ($mode == 'editable') { |
70
|
|
|
$vue->setEditableTemplate($stub); |
71
|
|
|
$this->collection->push( |
72
|
|
|
new GeneratedItem( |
73
|
|
|
GeneratedItem::TYPE_FRONTEND, |
74
|
|
|
$this->model->editable($this->composer), |
|
|
|
|
75
|
|
|
$path |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
$vue->setViewableTemplate($stub); |
80
|
|
|
$this->collection->push( |
81
|
|
|
new GeneratedItem( |
82
|
|
|
GeneratedItem::TYPE_FRONTEND, |
83
|
|
|
$this->model->viewable($this->composer, []), |
|
|
|
|
84
|
|
|
$path |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|