1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2024 ChannelWeb Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
namespace BEdita\DevTools\Command; |
16
|
|
|
|
17
|
|
|
use Cake\Console\Arguments; |
18
|
|
|
use Cake\Console\ConsoleIo; |
19
|
|
|
use Cake\Console\ConsoleOptionParser; |
20
|
|
|
use Migrations\Command\BakeSimpleMigrationCommand; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
* |
25
|
|
|
* Command class for generating resources migrations files. |
26
|
|
|
*/ |
27
|
|
|
class ResourcesMigrationCommand extends BakeSimpleMigrationCommand |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
|
|
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
33
|
|
|
{ |
34
|
|
|
$parser->addArgument('name', [ |
35
|
|
|
'help' => 'Name of the migration', |
36
|
|
|
'required' => true, |
37
|
|
|
]) |
38
|
|
|
->addOption('force', [ |
39
|
|
|
'short' => 'f', |
40
|
|
|
'boolean' => true, |
41
|
|
|
'default' => 'false', |
42
|
|
|
'help' => 'Force overwriting existing files without prompting.', |
43
|
|
|
]) |
44
|
|
|
->addOption('connection', [ |
45
|
|
|
'short' => 'c', |
46
|
|
|
'default' => 'default', |
47
|
|
|
'help' => 'The datasource connection to get data from.', |
48
|
|
|
]) |
49
|
|
|
->addOption('source', [ |
50
|
|
|
'short' => 's', |
51
|
|
|
'default' => 'Migrations', |
52
|
|
|
'help' => 'The migrations folder.', |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
return $parser; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Main migration file name. |
60
|
|
|
* |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
protected ?string $migrationFile = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function name(): string |
69
|
|
|
{ |
70
|
|
|
return 'resources_migration'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function fileName($name): string |
77
|
|
|
{ |
78
|
|
|
if (isset($this->migrationFile)) { |
79
|
|
|
return $this->migrationFile; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->migrationFile = parent::fileName($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function template(): string |
89
|
|
|
{ |
90
|
|
|
return 'BEdita/DevTools.resources'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function bake(string $name, Arguments $args, ConsoleIo $io): void |
97
|
|
|
{ |
98
|
|
|
// create .php file first, then .yml |
99
|
|
|
parent::bake($name, $args, $io); |
100
|
|
|
|
101
|
|
|
$contents = $this->createTemplateRenderer() |
102
|
|
|
->set('name', $name) |
103
|
|
|
->set($this->templateData($args)) |
104
|
|
|
->generate('BEdita/DevTools.yaml'); |
105
|
|
|
|
106
|
|
|
$filename = $this->getPath($args) . str_replace('.php', '.yml', $this->fileName($name)); |
107
|
|
|
$io->createFile($filename, $contents, $this->force); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|