Skip to content

Commit 4323f72

Browse files
committed
feat: add Maker command for state processor
feat: add Maker command for state provider
1 parent 1470b1f commit 4323f72

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
@@ -234,7 +234,8 @@ parameters:
234234
- src/Core/Util/RequestParser.php
235235
- src/Core/Validator/EventListener/ValidateListener.php
236236
# Symfony cache
237-
- tests/Fixtures/app/var/cache
237+
- tests/Fixtures/app/var/
238+
- tests/Fixtures/Symfony/Maker
238239
# Deprecated integrations (will be removed in API Platform 3)
239240
- src/Core/Bridge/NelmioApiDoc/*
240241
- tests/Core/Bridge/NelmioApiDoc/*
@@ -259,7 +260,7 @@ parameters:
259260
- tests/Fixtures/TestBundle/Security/AbstractSecurityUser.php
260261
# Templates for Maker
261262
- src/Core/Bridge/Symfony/Maker/Resources/skeleton
262-
- src/Bridge/Symfony/Maker/Resources/skeleton
263+
- src/Symfony/Maker/Resources/skeleton
263264
# Rector because new API Platform 3.0 classes don't exist yet
264265
- src/Core/Bridge/Rector
265266
- src/Core/Bridge/Symfony/Bundle/Command/RectorCommand.php
@@ -324,7 +325,7 @@ parameters:
324325

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

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

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)