Skip to content

Commit 8eec7c8

Browse files
committed
feat(maker): allow symfony makers namespace configuration
1 parent 8c27606 commit 8eec7c8

File tree

13 files changed

+151
-6
lines changed

13 files changed

+151
-6
lines changed

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,12 @@ private function registerMakerConfiguration(ContainerBuilder $container, array $
964964
return;
965965
}
966966

967+
$namespaceprefix = $config['maker']['namespace_prefix'] ?? '';
968+
if ('' !== $namespaceprefix) {
969+
$namespaceprefix = \trim($namespaceprefix, '\\') . '\\';
970+
}
971+
$container->setParameter('api_platform.maker.namespace_prefix', $namespaceprefix);
972+
967973
$loader->load('maker.php');
968974
}
969975

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ private function addMakerSection(ArrayNodeDefinition $rootNode): void
657657
->children()
658658
->arrayNode('maker')
659659
->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
660+
->children()
661+
->scalarNode('namespace_prefix')->defaultValue('')->info('Add a prefix to all maker generated classes. e.g set it to "Api" to set the maker namespace to "App\\Api\\" (if the maker.root_namespace config is App). e.g. App\\Api\\State\\MyStateProcessor')->end()
662+
->end()
660663
->end()
661664
->end();
662665
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
$services = $container->services();
1818

1919
$services->set('api_platform.maker.command.state_processor', 'ApiPlatform\Symfony\Maker\MakeStateProcessor')
20-
->args([service('api_platform.metadata.resource.name_collection_factory')])
20+
->args([param('api_platform.maker.namespace_prefix')])
2121
->tag('maker.command');
2222

2323
$services->set('api_platform.maker.command.state_provider', 'ApiPlatform\Symfony\Maker\MakeStateProvider')
24-
->args([service('api_platform.metadata.resource.name_collection_factory')])
24+
->args([param('api_platform.maker.namespace_prefix')])
2525
->tag('maker.command');
2626

2727
$services->set('api_platform.maker.command.filter', 'ApiPlatform\Symfony\Maker\MakeFilter')
28-
->args([service('api_platform.metadata.resource.name_collection_factory')])
28+
->args([param('api_platform.maker.namespace_prefix')])
2929
->tag('maker.command');
3030
};

src/Symfony/Maker/MakeFilter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
use Symfony\Component\Console\Command\Command;
2424
use Symfony\Component\Console\Input\InputArgument;
2525
use Symfony\Component\Console\Input\InputInterface;
26+
use Symfony\Component\Console\Input\InputOption;
2627

2728
final class MakeFilter extends AbstractMaker
2829
{
30+
public function __construct(private readonly string $namespacePrefix = '')
31+
{
32+
}
33+
2934
/**
3035
* {@inheritdoc}
3136
*/
@@ -50,6 +55,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
5055
$command
5156
->addArgument('type', InputArgument::REQUIRED, \sprintf('Choose a type for your filter (<fg=yellow>%s</>)', self::getFilterTypesAsString()))
5257
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your filter (e.g. <fg=yellow>AwesomeFilter</>)')
58+
->addOption('namespace-prefix', 'p', InputOption::VALUE_REQUIRED, 'Specify the namespace prefix to use for the filter class', $this->namespacePrefix.'Filter')
5359
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeFilter.txt'));
5460
}
5561

@@ -75,7 +81,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
7581

7682
$filterNameDetails = $generator->createClassNameDetails(
7783
name: $input->getArgument('name'),
78-
namespacePrefix: 'Filter\\'
84+
namespacePrefix: \trim($input->getOption('namespace-prefix'), '\\') . '\\'
7985
);
8086
$filterName = \sprintf('%sFilter', ucfirst($type->value));
8187

src/Symfony/Maker/MakeStateProcessor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
use Symfony\Component\Console\Command\Command;
2222
use Symfony\Component\Console\Input\InputArgument;
2323
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
25+
26+
use function trim;
2427

2528
final class MakeStateProcessor extends AbstractMaker
2629
{
30+
public function __construct(private readonly string $namespacePrefix = '')
31+
{
32+
}
2733
/**
2834
* {@inheritdoc}
2935
*/
@@ -47,6 +53,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4753
{
4854
$command
4955
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state processor (e.g. <fg=yellow>AwesomeStateProcessor</>)')
56+
->addOption('namespace-prefix', 'p', InputOption::VALUE_REQUIRED, 'Specify the namespace prefix to use for the state processor class', $this->namespacePrefix.'State')
5057
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProcessor.txt'));
5158
}
5259

@@ -64,7 +71,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6471
{
6572
$stateProcessorClassNameDetails = $generator->createClassNameDetails(
6673
$input->getArgument('name'),
67-
'State\\'
74+
\trim($input->getOption('namespace-prefix'), '\\') . '\\'
6875
);
6976

7077
$generator->generateClass(

src/Symfony/Maker/MakeStateProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
use Symfony\Component\Console\Command\Command;
2222
use Symfony\Component\Console\Input\InputArgument;
2323
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
2425

2526
final class MakeStateProvider extends AbstractMaker
2627
{
28+
public function __construct(private readonly string $namespacePrefix = '')
29+
{
30+
}
31+
2732
/**
2833
* {@inheritdoc}
2934
*/
@@ -47,6 +52,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4752
{
4853
$command
4954
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state provider (e.g. <fg=yellow>AwesomeStateProvider</>)')
55+
->addOption('namespace-prefix', 'p', InputOption::VALUE_REQUIRED, 'Specify the namespace prefix to use for the state provider class', $this->namespacePrefix.'State')
5056
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProvider.txt'));
5157
}
5258

@@ -64,7 +70,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6470
{
6571
$stateProviderClassNameDetails = $generator->createClassNameDetails(
6672
$input->getArgument('name'),
67-
'State\\'
73+
\trim($input->getOption('namespace-prefix'), '\\') . '\\'
6874
);
6975

7076
$generator->generateClass(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace App\Api\Filter;
2+
3+
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
4+
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
5+
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
6+
use ApiPlatform\Metadata\Operation;
7+
use Doctrine\ORM\QueryBuilder;
8+
9+
class CustomOrmFilter implements FilterInterface
10+
{
11+
use BackwardCompatibleFilterDescriptionTrait; // Here for backward compatibility, keep it until 5.0.
12+
13+
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
14+
{
15+
// Retrieve the parameter and it's value
16+
// $parameter = $context['parameter'];
17+
// $value = $parameter->getValue();
18+
19+
// Retrieve the property
20+
// $property = $parameter->getProperty();
21+
22+
// Retrieve alias and parameter name
23+
// $alias = $queryBuilder->getRootAliases()[0];
24+
// $parameterName = $queryNameGenerator->generateParameterName($property);
25+
26+
// TODO: make your awesome query using the $queryBuilder
27+
// $queryBuilder->
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace App\Api\State;
2+
3+
use ApiPlatform\Metadata\Operation;
4+
use ApiPlatform\State\ProcessorInterface;
5+
6+
class CustomStateProcessor implements ProcessorInterface
7+
{
8+
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
9+
{
10+
// Handle the state
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace App\Api\State;
2+
3+
use ApiPlatform\Metadata\Operation;
4+
use ApiPlatform\State\ProviderInterface;
5+
6+
class CustomStateProvider implements ProviderInterface
7+
{
8+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
9+
{
10+
// Retrieve the state from somewhere
11+
}
12+
}

tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
229229
],
230230
'maker' => [
231231
'enabled' => true,
232+
'namespace_prefix' => ''
232233
],
233234
'use_symfony_listeners' => false,
234235
'handle_symfony_errors' => false,

0 commit comments

Comments
 (0)