Skip to content

Commit 3ec1257

Browse files
committed
add datagrid
1 parent a3f4fd9 commit 3ec1257

File tree

4 files changed

+88
-48
lines changed

4 files changed

+88
-48
lines changed

src/Generator/Cli/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function __construct()
2222
new GenerateControlCommand(),
2323
new GeneratePresenterCommand(),
2424
new GenerateFromTemplateCommand(),
25+
new GenerateDataGridCommand(),
2526
new SaveTemplateCommand(),
2627
new LoadTemplateCommand(),
2728
new RemoveTemplateCommand(),

src/Generator/Cli/GenerateCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6969
] : ['name' => $name];
7070
switch(strtolower($type)) {
7171
case 'database':
72-
case 'd':
72+
case 'db':
7373
$output->writeln("Generating entity <options=bold>{$name}</>...");
7474
$output->writeln("Generating facade <options=bold>{$name}Facade</>...");
7575
$output->writeln("Generating repository <options=bold>{$name}Repository</>...");
@@ -128,6 +128,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
128128
...$uiArgs,
129129
], $output);
130130
break;
131+
case 'datagrid':
132+
case 'grid':
133+
case 'dg':
134+
$this->runCommand('gen:dg', [
135+
'command' => 'gen:dg',
136+
...$uiArgs,
137+
], $output);
138+
break;
131139
default:
132140
$io->error('Invalid type.');
133141
$io->block('<comment>The type you entered is not recognized.</comment> Recognized types are: <options=bold>[database, entity, repository, facade, presenter, ui, control, form]</>', null, null, ' ', false, false);
@@ -164,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
164172
$io->newLine();
165173
$io->block('Entity named ' . $chosenName . ' will be generated.', null, null, ' ', true, false);
166174

167-
if (in_array($chosenType, ['ui', 'form', 'control'])) {
175+
if (in_array($chosenType, ['ui', 'form', 'control', 'datagrid'])) {
168176
$entityQuestion = new Question('<comment><options=bold>Enter the Entity to which your component(s) belong</> (or leave empty):</comment> ');
169177
$entityQuestion->setValidator($validateName);
170178

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matronator\Generator\Cli;
6+
7+
use Matronator\Generator\FileGenerator;
8+
use Matronator\Generator\Generators\DataGridControl;
9+
use Matronator\Generator\Generators\FormControl;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
// #[AsCommand('generate:control', 'Generates a Control file', ['gen:control'])]
17+
class GenerateDataGridCommand extends Command
18+
{
19+
protected static $defaultName = 'generate:datagrid';
20+
protected static $defaultDescription = 'Generates a DataGrid file.';
21+
22+
public function configure(): void
23+
{
24+
$this->setAliases(['gen:grid', 'gen:dg', 'gen:datagrid']);
25+
26+
$this->addArgument('name', InputArgument::REQUIRED, 'DataGrid name without the `DataGrid` suffix which will be added automatically.')
27+
->addOption('entity', 'e', InputOption::VALUE_REQUIRED, 'Entity to which the grid belongs.');
28+
}
29+
30+
public function execute(InputInterface $input, OutputInterface $output): int
31+
{
32+
$name = $input->getArgument('name');
33+
$entity = $input->getOption('entity') ?? null;
34+
35+
if (!$entity) {
36+
$output->writeln('<fg=red>Entity is required!</>');
37+
38+
return self::FAILURE;
39+
}
40+
41+
$output->writeln("Generating <options=bold>{$name}DataGrid</> to entity {$entity}...");
42+
FileGenerator::writeFile(DataGridControl::generate($name, $entity));
43+
44+
DataGridControl::generateTemplate($name, $entity);
45+
46+
$output->writeln('<fg=green>Done!</>');
47+
48+
return self::SUCCESS;
49+
}
50+
}

src/Generator/Generators/DataGridControl.php

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,62 @@
88
use Matronator\Generator\FileObject;
99
use Nette\PhpGenerator\PhpFile;
1010

11-
class FormControl
11+
class DataGridControl
1212
{
1313
public const DIR_PATH = 'app/ui/Control/';
1414

15-
public static function generate(string $name, ?string $entity = null): FileObject
15+
public static function generate(string $name, string $entity): FileObject
1616
{
1717
$file = new PhpFile;
1818

1919
$file->setStrictTypes();
2020

21-
$namespace = $file->addNamespace('App\UI\Control' . ($entity ? "\\$entity" : ''));
22-
if ($entity) {
23-
$namespace->addUse('App\Model\Database\Entity\\'.$entity);
24-
$namespace->addUse('App\Model\Database\Repository\\'.$entity.'Repository');
25-
$namespace->addUse('App\Model\Database\Facade\\'.$entity.'Facade');
26-
}
27-
$namespace->addUse(self::getFullClass($name, $entity));
28-
$namespace->addUse(self::getFullClass($name, $entity, true));
21+
$namespace = $file->addNamespace('App\UI\Control' . "\\$entity");
22+
$namespace->addUse('App\Model\Database\Facade\\'.$entity.'Facade');
2923
$namespace->addUse('App\UI\Control\BaseControl');
3024
$namespace->addUse('Ublaboo\DataGrid\DataGrid');
3125

3226
$class = $namespace->addClass($name.'DataGridControl')
33-
->setExtends('App\UI\Control\BaseControl');
27+
->setExtends('App\UI\Control\BaseControl')
28+
->setFinal();
3429

35-
if ($entity) {
36-
$class->addProperty(lcfirst($entity.'Facade'))
37-
->setType('App\Model\Database\Facade\\'.$entity.'Facade')
38-
->addComment("@var {$entity}Facade @inject");
39-
}
30+
$class->addProperty(lcfirst($entity.'Facade'))
31+
->setType('App\Model\Database\Facade\\'.$entity.'Facade')
32+
->addComment("@var {$entity}Facade @inject");
4033

41-
$class->addMethod('createComponent'.$name.'DataGrid')
34+
$class->addMethod('createComponent'.ucfirst($name).'DataGrid')
4235
->setReturnType('Ublaboo\DataGrid\DataGrid')
43-
->addBody('$this->'.lcfirst($name).'Form = $this->'.lcfirst($name).'FormFactory->create();')
44-
->addBody('$this->'.lcfirst($name).'Form->onValidate[] = [$this, \'validate'.$name.'Form\'];')
45-
->addBody('$this->'.lcfirst($name).'Form->onSuccess[] = [$this, \'process'.$name.'Form\'];')
46-
->addBody('return $this->'.lcfirst($name).'Form;')
36+
->addBody('$dataset = $this->'.lcfirst($entity).'Facade->'.lcfirst($entity).'Repository->findAllForDataGrid();')
37+
->addBody('')
38+
->addBody('$grid = new DataGrid();')
39+
->addBody('$this->addComponent($grid, $name);')
40+
->addBody('$grid->setTranslator($this->translator);')
41+
->addBody('$grid->setDataSource($dataset);')
42+
->addBody('$grid->setStrictSessionFilterValues(false);')
43+
->addBody('')
44+
->addBody("\$grid->addColumnNumber('id', 'ID', 'id')")
45+
->addBody(' ->setSortable()')
46+
->addBody(' ->setFilterText();')
47+
->addBody('')
48+
->addBody('return $grid;')
4749
->addParameter('name')
4850
->setType('string');
4951

50-
$validateMethod = $class->addMethod('validate'.$name.'Form')
51-
->setReturnType('void');
52-
$validateMethod->addParameter('form')
53-
->setType(self::getFullClass($name, $entity));
54-
$validateMethod->addParameter('values')
55-
->setType('array');
56-
57-
$successMethod = $class->addMethod('process'.$name.'Form')
58-
->setReturnType('void')
59-
->addBody('$this->onSuccess();');
60-
$successMethod->addParameter('form')
61-
->setType(self::getFullClass($name, $entity));
62-
$successMethod->addParameter('values')
63-
->setType('array');
64-
6552
$class->addMethod('render')
6653
->setReturnType('void')
67-
->addBody('$this->template->setTranslator($this->translator);')
68-
->addBody('$this->template->setFile(dirname($this->getReflection()->getFileName()) . \'/templates/'.lcfirst($name).'FormControl.latte\');')
54+
->addBody('$this->template->setFile(dirname(self::getReflection()->getFileName()) . \'/templates/'.lcfirst($name).'DataGridControl.latte\');')
6955
->addBody('$this->template->render();');
7056

71-
return new FileObject(self::DIR_PATH . ($entity ? "$entity/" : ''), $name.'FormControl', $file, $entity);
57+
return new FileObject(self::DIR_PATH . "$entity/", $name.'DataGridControl', $file, $entity);
7258
}
7359

7460
public static function generateTemplate(string $name, ?string $entity = null): void
7561
{
76-
$dir = self::DIR_PATH.($entity ? "$entity/" : '/').'templates/';
62+
$dir = self::DIR_PATH."$entity/templates/";
7763
if (!FileGenerator::folderExist($dir)) {
7864
mkdir($dir, 0777, true);
7965
}
8066

81-
file_put_contents($dir . lcfirst($name.'FormControl.latte'), '{form ' . lcfirst($name.'Form') . '}'.PHP_EOL.'{/form}'.PHP_EOL);
82-
}
83-
84-
private static function getFullClass(string $name, string $entity = null)
85-
{
86-
return 'App\UI\Control'.($entity ? "\\$entity\\" : '\\').ucfirst($name).'DataGridControl';
67+
file_put_contents($dir . lcfirst($name.'DataGridControl.latte'), '{control ' . lcfirst($name.'DataGrid') . '}'.PHP_EOL);
8768
}
8869
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy