Skip to content
Closed
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
8 changes: 8 additions & 0 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,14 @@
protected ?bool $hideHydraOperation = null,
?bool $jsonStream = null,
protected array $extraProperties = [],

/**
* Let the resource class be registered as a service.
* Use it only if the class does not hold values.
*
* @var bool|null
*/
public bool $registerAsController = false,

Check failure on line 981 in src/Metadata/ApiResource.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

PHPDoc type for property ApiPlatform\Metadata\ApiResource::$registerAsController with type bool|null is not subtype of native type bool.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure for the name. Any better idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it could work with any service and not only controller, maybe registerAsService or exposeAsService (?)
Or the inverted excludeFromContainer.

but the issue is that such name is really related to the symfony configuration, and it might be weird on laravel side to have a useless option.

What's the benefit of $definition->setAbstract(true)->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']); for dto ?
Also, couldn't it be automatically detected/added only if the class is not already registered/autowired (as a service/controller) (?)

But given the "wrong" use I did with ApiResource to document some controller route, I wonder if the right solution wouldn't to expose another dedicated attribute for my usage (which could be used on Controller class or directly on a route) like #[ApiRoute] (?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But given the "wrong" use I did with ApiResource to document some controller route, I wonder if the right solution wouldn't to expose another dedicated attribute for my usage (which could be used on Controller class or directly on a route) like #[ApiRoute] (?)

This feature might already exists
#7402 (comment)

) {
parent::__construct(
shortName: $shortName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('api_platform.uri_variables.transformer');
$container->registerForAutoconfiguration(ParameterProviderInterface::class)
->addTag('api_platform.parameter_provider');
$container->registerAttributeForAutoconfiguration(ApiResource::class, static function (ChildDefinition $definition): void {
$definition->setAbstract(true)
->addTag('api_platform.resource')
->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
$container->registerAttributeForAutoconfiguration(ApiResource::class, static function (ChildDefinition $definition, ApiResource $attribute): void {
$definition->addTag('api_platform.resource');
if ($attribute->registerAsController) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the name did you want to do the opposite ?

if (!$attribute->registerAsController) {

$definition->setAbstract(true)->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
};
});
$container->registerAttributeForAutoconfiguration(
AsResourceMutator::class,
Expand Down
Loading