-
-
Notifications
You must be signed in to change notification settings - Fork 436
new Maker for api-platform data persisters #697
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
Closed
zairigimad
wants to merge
7
commits into
symfony:main
from
zairigimad:feature/make-api-data-persister
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3989942
Merge pull request #1 from symfony/master
zairigimad 47f87f9
Merge pull request #2 from symfony/master
zairigimad fb936ac
Merge pull request #3 from symfony/master
zairigimad 354c828
Create DataPersister Maker
zairigimad f7409ab
Add Question to ask for resource
zairigimad 424b749
Add Check if Entity to Inject Doctrine Persister, and fix tests
zairigimad 221a17e
Add Service Declaration with decorator
zairigimad 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Maker; | ||
|
||
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; | ||
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; | ||
use Symfony\Bundle\MakerBundle\FileManager; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator; | ||
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\ConfirmationQuestion; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
/** | ||
* @author Imad ZAIRIG <imadzairig@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class MakeDataPersister extends AbstractMaker | ||
{ | ||
/** @var ResourceNameCollectionFactoryInterface */ | ||
protected $ressourceNameCollectionFactory; | ||
/** @var ResourceMetadataFactoryInterface */ | ||
protected $ressourceMetaDataFactory; | ||
protected $fileManager; | ||
protected $doctrineHelper; | ||
protected $resourcesClassNames = []; | ||
|
||
public function __construct( | ||
FileManager $fileManager, | ||
DoctrineHelper $doctrineHelper, | ||
$ressourceNameCollectionFactory = null, | ||
$ressourceMetaDataFactory = null | ||
) { | ||
$this->fileManager = $fileManager; | ||
$this->doctrineHelper = $doctrineHelper; | ||
$this->ressourceNameCollection = $ressourceNameCollectionFactory; | ||
$this->ressourceMetaDataFactory = $ressourceMetaDataFactory; | ||
} | ||
|
||
public static function getCommandName(): string | ||
{ | ||
return 'make:api:data-persister'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig) | ||
{ | ||
$command | ||
->setDescription('Creates a API Platform Data Persister') | ||
->addArgument('name', InputArgument::OPTIONAL, 'The name of the Data Persister class (e.g. <fg=yellow>CustomDataPersister</>)') | ||
->addArgument('resource', InputArgument::OPTIONAL, 'The name of the resource class') | ||
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeDataPersister.txt')); | ||
$inputConfig->setArgumentAsNonInteractive('resource'); | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command) | ||
{ | ||
if (null === $input->getArgument('resource')) { | ||
$argument = $command->getDefinition()->getArgument('resource'); | ||
$question = $this->createResourceClassQuestion($argument->getDescription()); | ||
$value = $io->askQuestion($question); | ||
$input->setArgument('resource', $value); | ||
$doctrineOption = new InputOption('is_doctrine_persister', 'a', InputOption::VALUE_NONE, 'Would you like your persister to call the core Doctrine persister?'); | ||
$command->getDefinition()->addOption($doctrineOption); | ||
$this->resourcesClassNames = array_flip($this->getResources()); | ||
|
||
if (\in_array($value, $this->getResources()) && $this->doctrineHelper->isClassAMappedEntity($this->resourcesClassNames[$value])) { | ||
$description = $command->getDefinition()->getOption('is_doctrine_persister')->getDescription(); | ||
$question = new ConfirmationQuestion($description, false); | ||
$value = $io->askQuestion($question); | ||
|
||
$input->setOption('is_doctrine_persister', $value); | ||
} | ||
} | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
{ | ||
$templateVariables = []; | ||
$resourceShortName = $input->getArgument('resource'); | ||
$this->resourcesClassNames = array_flip($this->getResources()); | ||
|
||
if ($resourceShortName && \in_array($resourceShortName, $this->resourcesClassNames)) { | ||
$resourceClasseName = $this->resourcesClassNames[$resourceShortName]; | ||
$templateVariables['resource_short_name'] = $resourceShortName; | ||
$templateVariables['resource_class_name'] = $resourceClasseName; | ||
} | ||
|
||
$dataPersisterClassNameDetails = $generator->createClassNameDetails( | ||
$input->getArgument('name'), | ||
'DataPersister\\', | ||
'DataPersister' | ||
); | ||
|
||
if ($input->getOption('is_doctrine_persister')) { | ||
$templateVariables['is_doctrine_persister'] = true; | ||
if (!$this->fileManager->fileExists($path = 'config/services.yaml')) { | ||
throw new RuntimeCommandException('The file "config/packages/security.yaml" does not exist. This command requires that file to exist so that it can be updated.'); | ||
} | ||
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($path)); | ||
|
||
$servicesData = $manipulator->getData(); | ||
$servicesData['services'] = [$dataPersisterClassNameDetails->getFullName() => ['decorates' => 'api_platform.doctrine.orm.data_persister']] + $servicesData['services']; | ||
$manipulator->setData($servicesData); | ||
$this->fileManager->dumpFile($path, $manipulator->getContents()); | ||
} | ||
|
||
$generator->generateClass( | ||
$dataPersisterClassNameDetails->getFullName(), | ||
'api/DataPersister.tpl.php', | ||
$templateVariables | ||
); | ||
|
||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
|
||
$io->text([ | ||
'Next:', | ||
sprintf('- Open the <info>%s</info> class and add the code you need', $dataPersisterClassNameDetails->getFullName()), | ||
'Find the documentation at <fg=yellow>https://api-platform.com/docs/core/data-persisters/#creating-a-custom-data-persister</>', | ||
]); | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies) | ||
{ | ||
$dependencies->addClassDependency( | ||
ContextAwareDataPersisterInterface::class, | ||
'api' | ||
); | ||
} | ||
|
||
private function createResourceClassQuestion(string $questionText): Question | ||
{ | ||
$question = new Question($questionText); | ||
$question->setAutocompleterValues(array_values($this->getResources())); | ||
|
||
return $question; | ||
} | ||
|
||
private function getResources(): array | ||
{ | ||
$collection = $this->ressourceNameCollection->create(); | ||
$resources = []; | ||
|
||
foreach ($collection as $className) { | ||
$resources[$className] = $this->ressourceMetaDataFactory->create($className)->getShortName(); | ||
} | ||
|
||
return $resources; | ||
} | ||
} |
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,5 @@ | ||
The <info>%command.name%</info> command generates a new Data Persister class. | ||
|
||
<info>php %command.full_name% CustomDataPersister</info> | ||
|
||
If the argument is missing, the command will ask for the message class 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,45 @@ | ||
<?= "<?php\n" ?> | ||
|
||
namespace <?= $namespace; ?>; | ||
|
||
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; | ||
<?= isset($resource_class_name) ? "use $resource_class_name;\n" : '' ?> | ||
|
||
final class <?= $class_name; ?> implements ContextAwareDataPersisterInterface | ||
{ | ||
<?php if (isset($is_doctrine_persister) && isset($resource_short_name)): ?> | ||
private $decorated; | ||
|
||
public function __construct(ContextAwareDataPersisterInterface $decorated) | ||
{ | ||
$this->decorated = $decorated; | ||
} | ||
|
||
<?php endif ?> | ||
public function supports($data, array $context = []): bool | ||
{ | ||
<?php if (isset($is_doctrine_persister) && isset($resource_short_name)): ?> | ||
return $this->decorated->supports($data, $context); | ||
<?php elseif (isset($resource_short_name)):?> | ||
return $data instanceof <?= $resource_short_name; ?>; | ||
<?php else: ?> | ||
return true; | ||
<?php endif ?> | ||
} | ||
|
||
public function persist($data, array $context = []) | ||
{ | ||
<?php if (isset($is_doctrine_persister) && isset($resource_short_name)): ?> | ||
$data = $this->decorated->persist($data, $context); | ||
|
||
<?php endif ?> | ||
return $data; | ||
} | ||
|
||
public function remove($data, array $context = []) | ||
{ | ||
<?php if (isset($is_doctrine_persister) && isset($resource_short_name)): ?> | ||
return $this->decorated->persist($data, $context); | ||
<?php endif ?> | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Tests\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\Maker\MakeDataPersister; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestCase; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails; | ||
|
||
class MakeDataPersisterTest extends MakerTestCase | ||
{ | ||
public function getTestDetails() | ||
{ | ||
yield 'api_data_persister' => [MakerTestDetails::createTest( | ||
$this->getMakerInstance(MakeDataPersister::class), | ||
[ | ||
// data persister name | ||
'CustomDataPersister', | ||
' ', | ||
])->assert(function (string $output, string $directory) { | ||
$this->assertFileExists($directory.'/src/DataPersister/CustomDataPersister.php'); | ||
}), | ||
]; | ||
yield 'entity_with_doctrine_persister' => [MakerTestDetails::createTest( | ||
$this->getMakerInstance(MakeDataPersister::class), | ||
[ | ||
'ArticleDataPersister', | ||
'Article', | ||
'yes', | ||
]) | ||
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeDataPersister') | ||
->assert(function (string $output, string $directory) { | ||
$this->assertFileExists($directory.'/src/DataPersister/ArticleDataPersister.php'); | ||
}), | ||
]; | ||
yield 'entity_without_doctrine_persister' => [MakerTestDetails::createTest( | ||
$this->getMakerInstance(MakeDataPersister::class), | ||
[ | ||
'ArticleBlogDataPersister', | ||
'Article', | ||
]) | ||
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeDataPersister') | ||
->assert(function (string $output, string $directory) { | ||
$this->assertFileExists($directory.'/src/DataPersister/ArticleBlogDataPersister.php'); | ||
}), | ||
]; | ||
yield 'model_class_persister' => [MakerTestDetails::createTest( | ||
$this->getMakerInstance(MakeDataPersister::class), | ||
[ | ||
'BookDataPersister', | ||
'Book', | ||
]) | ||
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeDataPersister') | ||
->assert(function (string $output, string $directory) { | ||
$this->assertFileExists($directory.'/src/DataPersister/BookDataPersister.php'); | ||
}), | ||
]; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Model; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
|
||
/** | ||
* @ApiResource() | ||
*/ | ||
class Book | ||
{ | ||
private $id; | ||
|
||
private $title; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ApiResource() | ||
* @ORM\Entity() | ||
*/ | ||
class Article | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
*/ | ||
private $title; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
} |
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.
It might make sense to allow the user to interactively specify what class the data persister will persist for - e.g.
App\Entity\CheeseListing
. It would be even cooler if this read the API Platform metadata to list all the API Platform resource classes :). This would help us generate a bettersupports()
method. Or, they could leave this blank and we would generate thesupports()
method like they have now.And, to go further, if the resource class they choose is an entity, we could ask them something like this:
If they said yes, we would inject the core Doctrine ORM persister like done here - https://symfonycasts.com/screencast/api-platform-extending/decoration-deep-dive
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.
yes, great ideas, I'm gonna Try to add this, and make Seperate PRs for the other Makers :)
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.
@weaverryan the Maker is now working with the update of services and with the different cases.
I would love to see your feedback if there are somethings to refactor in the code.