forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakeDataPersister.php
116 lines (97 loc) · 3.65 KB
/
MakeDataPersister.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Bridge\Symfony\Maker;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
final class MakeDataPersister extends AbstractMaker
{
private $resourceNameCollection;
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollection)
{
$this->resourceNameCollection = $resourceNameCollection;
}
/**
* {@inheritdoc}
*/
public static function getCommandName(): string
{
return 'make:data-persister';
}
/**
* {@inheritdoc}
*/
public static function getCommandDescription(): string
{
return 'Creates an API Platform data persister';
}
/**
* {@inheritdoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig)
{
$command
->addArgument('name', InputArgument::OPTIONAL, 'Choose a class name for your data persister (e.g. <fg=yellow>AwesomeDataPersister</>)')
->addArgument('resource-class', InputArgument::OPTIONAL, 'Choose a Resource class')
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeDataPersister.txt'));
$inputConfig->setArgumentAsNonInteractive('resource-class');
}
/**
* {@inheritdoc}
*/
public function configureDependencies(DependencyBuilder $dependencies)
{
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
{
if (null === $input->getArgument('resource-class')) {
$argument = $command->getDefinition()->getArgument('resource-class');
$resourceClasses = $this->resourceNameCollection->create();
$question = new Question($argument->getDescription());
$question->setAutocompleterValues($resourceClasses);
$value = $io->askQuestion($question);
$input->setArgument('resource-class', $value);
}
}
/**
* {@inheritdoc}
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$dataPersisterClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'DataPersister\\'
);
$resourceClass = $input->getArgument('resource-class');
$generator->generateClass(
$dataPersisterClassNameDetails->getFullName(),
__DIR__.'/Resources/skeleton/DataPersister.tpl.php',
[
'resource_class' => null !== $resourceClass ? Str::getShortClassName($resourceClass) : null,
'resource_full_class_name' => $resourceClass,
]
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next: Open your new data persister class and start customizing it.',
'Find the documentation at <fg=yellow>https://api-platform.com/docs/core/data-persisters/</>',
]);
}
}