-
-
Notifications
You must be signed in to change notification settings - Fork 934
Add maker command to generate data providers/persisters #3850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
56eb8ce
Add maker command to generate data providers/persisters
antograssiot da362fb
Add new features
antograssiot 73eee07
Add tests for the new Maker commands
antograssiot 85934bb
Fix tests for PHP 7.1
antograssiot ef4eed9
Try to fix test on windows
antograssiot a01bef2
Try to fix code coverage generation
antograssiot 83e36f7
Improve coverage and fix review
antograssiot ad1fb34
Fix indentation
antograssiot 52c0b80
fix: minor changes
alanpoulain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="api_platform.maker.command.data_provider" class="ApiPlatform\Core\Bridge\Symfony\Maker\MakeDataProvider"> | ||
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" /> | ||
<tag name="maker.command" /> | ||
</service> | ||
|
||
<service id="api_platform.maker.command.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Maker\MakeDataPersister"> | ||
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" /> | ||
<tag name="maker.command" /> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\Core\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/</>', | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; | ||
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\Input\InputOption; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
class MakeDataProvider extends AbstractMaker | ||
{ | ||
private $resourceNameCollection; | ||
|
||
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollection) | ||
{ | ||
$this->resourceNameCollection = $resourceNameCollection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getCommandName(): string | ||
{ | ||
return 'make:data-provider'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getCommandDescription(): string | ||
{ | ||
return 'Creates an API Platform data provider'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureCommand(Command $command, InputConfiguration $inputConfig) | ||
{ | ||
$command | ||
->addArgument('name', InputArgument::OPTIONAL, 'Choose a class name for your data provider (e.g. <fg=yellow>AwesomeDataProvider</>)') | ||
->addArgument('resource-class', InputArgument::OPTIONAL, 'Choose a Resource class') | ||
->addOption('item-only', null, InputOption::VALUE_NONE, 'Generate only an item data provider') | ||
->addOption('collection-only', null, InputOption::VALUE_NONE, 'Generate only a collection data provider') | ||
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeDataProvider.txt')); | ||
|
||
$inputConfig->setArgumentAsNonInteractive('resource-class'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureDependencies(DependencyBuilder $dependencies) | ||
{ | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command) | ||
{ | ||
if ($input->getOption('item-only') && $input->getOption('collection-only')) { | ||
throw new RuntimeCommandException('You should at least generate an item or a collection data provider'); | ||
} | ||
|
||
if (null === $input->getArgument('resource-class')) { | ||
$argument = $command->getDefinition()->getArgument('resource-class'); | ||
|
||
$question = new Question($argument->getDescription()); | ||
$question->setAutocompleterValues($this->resourceNameCollection->create()); | ||
|
||
$input->setArgument('resource-class', $io->askQuestion($question)); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
{ | ||
$dataProviderClassNameDetails = $generator->createClassNameDetails( | ||
$input->getArgument('name'), | ||
'DataProvider\\' | ||
); | ||
$resourceClass = $input->getArgument('resource-class'); | ||
|
||
$generator->generateClass( | ||
$dataProviderClassNameDetails->getFullName(), | ||
__DIR__.'/Resources/skeleton/DataProvider.tpl.php', | ||
[ | ||
'resource_class' => null !== $resourceClass ? Str::getShortClassName($resourceClass) : null, | ||
'resource_full_class_name' => $resourceClass, | ||
'generate_collection' => !$input->getOption('item-only'), | ||
'generate_item' => !$input->getOption('collection-only'), | ||
] | ||
); | ||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
$io->text([ | ||
'Next: Open your new data provider class and start customizing it.', | ||
'Find the documentation at <fg=yellow>https://api-platform.com/docs/core/data-providers/</>', | ||
]); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Bridge/Symfony/Maker/Resources/help/MakeDataPersister.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The <info>%command.name%</info> command generates a new API Platform data persister class. | ||
|
||
<info>php %command.full_name% AwesomeDataPersister</info> | ||
|
||
If the argument is missing, the command will ask for the class name interactively. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The <info>%command.name%</info> command generates a new API Platform data provider class. | ||
|
||
<info>php %command.full_name% AwesomeDataProvider</info> | ||
|
||
If the argument is missing, the command will ask for the class name interactively. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add the
getCommandDescription
static method instead of a call tosetDescription
(seems new to make it lazy).