Skip to content

Commit cbb96f6

Browse files
Merge pull request #4423 from nikophil/maker/state-processor
feat: add Maker command for state processor and provider
2 parents 5f4ceb6 + 4323f72 commit cbb96f6

14 files changed

+421
-5
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
->exclude([
1717
'src/Core/Bridge/Symfony/Maker/Resources/skeleton',
1818
'tests/Fixtures/app/var',
19+
'tests/Fixtures/Symfony/Maker',
1920
])
2021
->notPath('src/Symfony/Bundle/DependencyInjection/Configuration.php')
2122
->notPath('src/Annotation/ApiFilter.php') // temporary

phpstan.neon.dist

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ parameters:
235235
- src/Core/Util/RequestParser.php
236236
- src/Core/Validator/EventListener/ValidateListener.php
237237
# Symfony cache
238-
- tests/Fixtures/app/var/cache
238+
- tests/Fixtures/app/var/
239+
- tests/Fixtures/Symfony/Maker
239240
# Deprecated integrations (will be removed in API Platform 3)
240241
- src/Core/Bridge/NelmioApiDoc/*
241242
- tests/Core/Bridge/NelmioApiDoc/*
@@ -260,7 +261,7 @@ parameters:
260261
- tests/Fixtures/TestBundle/Security/AbstractSecurityUser.php
261262
# Templates for Maker
262263
- src/Core/Bridge/Symfony/Maker/Resources/skeleton
263-
- src/Bridge/Symfony/Maker/Resources/skeleton
264+
- src/Symfony/Maker/Resources/skeleton
264265
# Rector because new API Platform 3.0 classes don't exist yet
265266
- src/Core/Bridge/Rector
266267
- src/Core/Bridge/Symfony/Bundle/Command/RectorCommand.php
@@ -325,7 +326,7 @@ parameters:
325326

326327
# Expected, due to deprecations
327328
- '#Method ApiPlatform\\PathResolver\\OperationPathResolverInterface::resolveOperationPath\(\) invoked with 4 parameters, 3 required\.#'
328-
-
329+
-
329330
message: '#If condition is always false.#'
330331
path: src/Core
331332

@@ -351,10 +352,10 @@ parameters:
351352
message: '#Call to an undefined method Symfony\\Component\\PropertyInfo\\Type::getCollectionKeyType\(\)#'
352353
path: src
353354
# Skipped tests, we do this on purpose
354-
-
355+
-
355356
message: "#^Unreachable statement - code above always terminates.$#"
356357
path: tests
357-
-
358+
-
358359
message: "#^Unreachable statement - code above always terminates.$#"
359360
path: src/Core/Bridge/Doctrine/EventListener/PublishMercureUpdatesListener.php
360361
-

src/Symfony/Bundle/Resources/config/maker.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
1515
<tag name="maker.command" />
1616
</service>
17+
18+
<service id="api_platform.maker.command.state_processor" class="ApiPlatform\Symfony\Maker\MakeStateProcessor">
19+
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
20+
<tag name="maker.command" />
21+
</service>
22+
23+
<service id="api_platform.maker.command.state_provider" class="ApiPlatform\Symfony\Maker\MakeStateProvider">
24+
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
25+
<tag name="maker.command" />
26+
</service>
1727
</services>
1828

1929
</container>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Symfony\Maker;
15+
16+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
17+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
18+
use Symfony\Bundle\MakerBundle\Generator;
19+
use Symfony\Bundle\MakerBundle\InputConfiguration;
20+
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
25+
final class MakeStateProcessor extends AbstractMaker
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public static function getCommandName(): string
31+
{
32+
return 'make:state-processor';
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public static function getCommandDescription(): string
39+
{
40+
return 'Creates an API Platform state processor';
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function configureCommand(Command $command, InputConfiguration $inputConfig)
47+
{
48+
$command
49+
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state processor (e.g. <fg=yellow>AwesomeStateProcessor</>)')
50+
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProcessor.txt'));
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function configureDependencies(DependencyBuilder $dependencies)
57+
{
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
64+
{
65+
$stateProcessorClassNameDetails = $generator->createClassNameDetails(
66+
$input->getArgument('name'),
67+
'State\\'
68+
);
69+
70+
$generator->generateClass(
71+
$stateProcessorClassNameDetails->getFullName(),
72+
__DIR__.'/Resources/skeleton/StateProcessor.tpl.php'
73+
);
74+
$generator->writeChanges();
75+
76+
$this->writeSuccessMessage($io);
77+
$io->text([
78+
'Next: Open your new state processor class and start customizing it.',
79+
]);
80+
}
81+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Symfony\Maker;
15+
16+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
17+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
18+
use Symfony\Bundle\MakerBundle\Generator;
19+
use Symfony\Bundle\MakerBundle\InputConfiguration;
20+
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
25+
final class MakeStateProvider extends AbstractMaker
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public static function getCommandName(): string
31+
{
32+
return 'make:state-provider';
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public static function getCommandDescription(): string
39+
{
40+
return 'Creates an API Platform state provider';
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function configureCommand(Command $command, InputConfiguration $inputConfig)
47+
{
48+
$command
49+
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state provider (e.g. <fg=yellow>AwesomeStateProvider</>)')
50+
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProvider.txt'));
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function configureDependencies(DependencyBuilder $dependencies)
57+
{
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
64+
{
65+
$stateProviderClassNameDetails = $generator->createClassNameDetails(
66+
$input->getArgument('name'),
67+
'State\\'
68+
);
69+
70+
$generator->generateClass(
71+
$stateProviderClassNameDetails->getFullName(),
72+
__DIR__.'/Resources/skeleton/StateProvider.tpl.php'
73+
);
74+
$generator->writeChanges();
75+
76+
$this->writeSuccessMessage($io);
77+
$io->text([
78+
'Next: Open your new state provider class and start customizing it.',
79+
]);
80+
}
81+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The <info>%command.name%</info> command generates a new API Platform state processor class.
2+
3+
<info>php %command.full_name% AwesomeStateProcessor</info>
4+
5+
If the argument is missing, the command will ask for the class name interactively.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The <info>%command.name%</info> command generates a new API Platform state provider class.
2+
3+
<info>php %command.full_name% AwesomeStateProvider</info>
4+
5+
If the argument is missing, the command will ask for the class name interactively.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
echo "<?php\n"; ?>
3+
4+
namespace <?php echo $namespace; ?>;
5+
6+
use ApiPlatform\Metadata\Operation;
7+
use ApiPlatform\State\ProcessorInterface;
8+
9+
class <?php echo $class_name; ?> implements ProcessorInterface
10+
{
11+
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
12+
{
13+
// Handle the state
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
echo "<?php\n"; ?>
3+
4+
namespace <?php echo $namespace; ?>;
5+
6+
use ApiPlatform\Metadata\Operation;
7+
use ApiPlatform\State\ProviderInterface;
8+
9+
class <?php echo $class_name; ?> implements ProviderInterface
10+
{
11+
public function provide(Operation $operation, array $uriVariables = [], array $context = [])<?php if (\PHP_VERSION_ID >= 80000) { ?>: object|array|null<?php } ?>
12+
13+
{
14+
// Retrieve the state from somewhere
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\State;
4+
5+
use ApiPlatform\Metadata\Operation;
6+
use ApiPlatform\State\ProcessorInterface;
7+
8+
class CustomStateProcessor implements ProcessorInterface
9+
{
10+
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
11+
{
12+
// Handle the state
13+
}
14+
}

0 commit comments

Comments
 (0)