Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude([
'src/Bridge/Symfony/Maker/Resources/skeleton',
'tests/Fixtures/app/var',
])
->notPath('src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php')
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Security: **BC** Fix `ApiProperty` `security` attribute expression being passed a class string for the `object` variable on updates/creates - null is now passed instead if the object is not available (#4184)
* Security: `ApiProperty` now supports a `security_post_denormalize` attribute, which provides access to the `object` variable for the object being updated/created and `previous_object` for the object before it was updated (#4184)
* Maker: Add `make:data-provider` and `make :data-persister` commands to generate a data provider / persister (#3850)
* JSON Schema: Add support for generating property schema with numeric constraint restrictions (#4225)
* JSON Schema: Add support for generating property schema with Collection restriction (#4182)
* JSON Schema: Add support for generating property schema format for Url and Hostname (#4185)
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"symfony/form": "^3.4 || ^4.4 || ^5.1",
"symfony/framework-bundle": "^4.4 || ^5.1",
"symfony/http-client": "^4.4 || ^5.1",
"symfony/maker-bundle": "^1.24",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^4.4 || ^5.1",
"symfony/phpunit-bridge": "^5.1.7",
Expand Down Expand Up @@ -114,7 +115,8 @@
},
"autoload-dev": {
"psr-4": {
"ApiPlatform\\Core\\Tests\\": "tests/"
"ApiPlatform\\Core\\Tests\\": "tests/",
"App\\": "tests/Fixtures/app/var/tmp/src/"
}
},
"config": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ parameters:
- tests/ProphecyTrait.php
- tests/Behat/CoverageContext.php
- tests/Fixtures/TestBundle/Security/AbstractSecurityUser.php
# Templates for Maker
- src/Bridge/Symfony/Maker/Resources/skeleton
earlyTerminatingMethodCalls:
PHPUnit\Framework\Constraint\Constraint:
- fail
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<directory>vendor</directory>
<directory>src/Bridge/NelmioApiDoc</directory>
<directory>src/Bridge/FosUser</directory>
<directory>src/Bridge/Symfony/Maker/Resources/skeleton</directory>
<file>.php-cs-fixer.dist.php</file>
<file>src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php</file>
<file>src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubsetLegacy.php</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerElasticsearchConfiguration($container, $config, $loader);
$this->registerDataTransformerConfiguration($container);
$this->registerSecurityConfiguration($container, $loader);
$this->registerMakerConfiguration($container, $config, $loader);

$container->registerForAutoconfiguration(DataPersisterInterface::class)
->addTag('api_platform.data_persister');
Expand Down Expand Up @@ -743,6 +744,15 @@ private function registerOpenApiConfiguration(ContainerBuilder $container, array
$container->setParameter('api_platform.openapi.license.url', $config['openapi']['license']['url']);
}

private function registerMakerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
{
if (!$this->isConfigEnabled($container, $config['maker'])) {
return;
}

$loader->load('maker.xml');
}

private function buildDeprecationArgs(string $version, string $message): array
{
return method_exists(Definition::class, 'getDeprecation')
Expand Down
12 changes: 12 additions & 0 deletions src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use FOS\UserBundle\FOSUserBundle;
use GraphQL\GraphQL;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Bundle\MercureBundle\MercureBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Definition\BaseNode;
Expand Down Expand Up @@ -209,6 +210,7 @@ public function getConfigTreeBuilder()
$this->addMessengerSection($rootNode);
$this->addElasticsearchSection($rootNode);
$this->addOpenApiSection($rootNode);
$this->addMakerSection($rootNode);

$this->addExceptionToStatusSection($rootNode);

Expand Down Expand Up @@ -629,6 +631,16 @@ private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
}
}

private function addMakerSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('maker')
->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->end()
->end();
}

private function buildDeprecationArgs(string $version, string $message): array
{
return method_exists(BaseNode::class, 'getDeprecation')
Expand Down
19 changes: 19 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/maker.xml
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>
116 changes: 116 additions & 0 deletions src/Bridge/Symfony/Maker/MakeDataPersister.php
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';
}

Copy link
Member

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 to setDescription (seems new to make it lazy).

/**
* {@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/</>',
]);
}
}
122 changes: 122 additions & 0 deletions src/Bridge/Symfony/Maker/MakeDataProvider.php
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 src/Bridge/Symfony/Maker/Resources/help/MakeDataPersister.txt
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.
5 changes: 5 additions & 0 deletions src/Bridge/Symfony/Maker/Resources/help/MakeDataProvider.txt
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.
Loading