|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of monofony. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mobizel |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace App\Maker; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Bundle\MakerBundle\ConsoleStyle; |
|
17
|
|
|
use Symfony\Bundle\MakerBundle\DependencyBuilder; |
|
18
|
|
|
use Symfony\Bundle\MakerBundle\Generator; |
|
19
|
|
|
use Symfony\Bundle\MakerBundle\InputConfiguration; |
|
20
|
|
|
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
|
21
|
|
|
use Symfony\Component\Console\Command\Command; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
25
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
26
|
|
|
use Webmozart\Assert\Assert; |
|
27
|
|
|
|
|
28
|
|
|
final class MakeGrid extends AbstractMaker |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $projectDir; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
private $syliusResources; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Filesystem */ |
|
37
|
|
|
private $fileSystem; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $projectDir, array $syliusResources) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->projectDir = $projectDir; |
|
42
|
|
|
$this->syliusResources = $syliusResources; |
|
43
|
|
|
|
|
44
|
|
|
$this->fileSystem = new Filesystem(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function getCommandName(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return 'make:grid'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function configureCommand(Command $command, InputConfiguration $inputConfig) |
|
53
|
|
|
{ |
|
54
|
|
|
$command |
|
55
|
|
|
->setDescription('Creates a new grid') |
|
56
|
|
|
->addArgument('resource', InputArgument::REQUIRED, 'Resource alias of the grid') |
|
57
|
|
|
->addArgument('section', InputArgument::REQUIRED, 'Section of the grid (backend or frontend)') |
|
58
|
|
|
; |
|
59
|
|
|
|
|
60
|
|
|
$inputConfig->setArgumentAsNonInteractive('resource'); |
|
61
|
|
|
$inputConfig->setArgumentAsNonInteractive('section'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function interact(InputInterface $input, ConsoleStyle $io, Command $command) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$input->getArgument('resource')) { |
|
67
|
|
|
$question = new ChoiceQuestion( |
|
68
|
|
|
'Please select a resource for your grid', |
|
69
|
|
|
$this->getResourcesAliases(), |
|
70
|
|
|
0 |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$resource = $io->askQuestion($question); |
|
74
|
|
|
|
|
75
|
|
|
$input->setArgument('resource', $resource); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($resource = $input->getArgument('resource')) { |
|
79
|
|
|
Assert::true(in_array($resource, $this->getResourcesAliases()), sprintf('Resource with alias %s not found', $resource)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!$input->getArgument('section')) { |
|
83
|
|
|
$question = new ChoiceQuestion( |
|
84
|
|
|
'Please select a section for your grid', |
|
85
|
|
|
['backend', 'frontend'], |
|
86
|
|
|
0 |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$section = $io->askQuestion($question); |
|
90
|
|
|
|
|
91
|
|
|
$input->setArgument('section', $section); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function configureDependencies(DependencyBuilder $dependencies) |
|
96
|
|
|
{ |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->generateGridConfigFile($input, $io, $generator); |
|
102
|
|
|
|
|
103
|
|
|
// $this->fileSystem->dumpFile($this->projectDir.'/config/packages/grids/backend/'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function generateGridConfigFile(InputInterface $input, ConsoleStyle $io, Generator $generator) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$resource = $input->getArgument('resource'); |
|
109
|
|
|
[$appName, $resourceName] = explode('.', $resource); |
|
110
|
|
|
$section = $input->getArgument('section'); |
|
111
|
|
|
$gridId = sprintf('%s_%s_%s', $section, $appName, $resourceName); |
|
112
|
|
|
|
|
113
|
|
|
$gridConfigDir = $this->getGridConfigDir($resource, $section); |
|
|
|
|
|
|
114
|
|
|
$modelClass = sprintf('%s.model.%s.class', $appName, $resourceName); |
|
115
|
|
|
|
|
116
|
|
|
$this->fileSystem->dumpFile($gridConfigDir, <<<EOM |
|
117
|
|
|
sylius_grid: |
|
118
|
|
|
grids: |
|
119
|
|
|
$gridId: |
|
120
|
|
|
driver: |
|
121
|
|
|
name: doctrine/orm |
|
122
|
|
|
options: |
|
123
|
|
|
class: "%$modelClass%" |
|
124
|
|
|
EOM |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function getResourcesAliases(): array |
|
129
|
|
|
{ |
|
130
|
|
|
return array_keys($this->syliusResources); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function getGridConfigDir(string $resource, string $section) |
|
134
|
|
|
{ |
|
135
|
|
|
$filename = explode('.', $resource)[1].'.yaml'; |
|
136
|
|
|
|
|
137
|
|
|
return sprintf('%s/%s/%s', $this->projectDir.'/config/packages/grids', $section, $filename); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.