Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions #472

Closed
wants to merge 1 commit into from
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
80 changes: 0 additions & 80 deletions src/Action/ActionUtilTrait.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Action/PutItemAction.php

This file was deleted.

28 changes: 20 additions & 8 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,38 @@
<tag name="kernel.event_listener" event="kernel.view" method="onKernelView" priority="20" />
</service>

<!-- Action -->
<!-- HTTP -->

<service id="api_platform.action.get_collection" class="ApiPlatform\Core\Action\GetCollectionAction">
<service id="api_platform.http.item_data_provider" class="ApiPlatform\Core\Http\ItemDataProvider">
<argument type="service" id="api_platform.item_data_provider" />
</service>

<service id="api_platform.http.request_attributes_extractor" class="ApiPlatform\Core\Http\RequestAttributesExtractor">
<argument type="service" id="api_platform.item_data_provider" />
</service>

<service id="api_platform.http.action.get_collection" class="ApiPlatform\Core\Http\Action\GetCollectionAction">
<argument type="service" id="api_platform.collection_data_provider" />
<argument type="service" id="api_platform.http.request_attributes_extractor" />
</service>

<service id="api_platform.action.post_collection" class="ApiPlatform\Core\Action\PostCollectionAction">
<service id="api_platform.http.action.post_collection" class="ApiPlatform\Core\Http\Action\PostCollectionAction">
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="api_platform.http.request_attributes_extractor" />
</service>

<service id="api_platform.action.get_item" class="ApiPlatform\Core\Action\GetItemAction">
<argument type="service" id="api_platform.item_data_provider" />
<service id="api_platform.http.action.get_item" class="ApiPlatform\Core\Http\Action\GetItemAction">
<argument type="service" id="api_platform.http.item_data_provider" />
<argument type="service" id="api_platform.http.request_attributes_extractor" />
</service>

<service id="api_platform.action.put_item" class="ApiPlatform\Core\Action\PutItemAction">
<argument type="service" id="api_platform.item_data_provider" />
<service id="api_platform.http.action.put_item" class="ApiPlatform\Core\Http\Action\PutItemAction">
<argument type="service" id="api_platform.http.item_data_provider" />
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="api_platform.http.request_attributes_extractor" />
</service>

<service id="api_platform.action.delete_item" alias="api_platform.action.get_item" />
<service id="api_platform.http.action.delete_item" alias="api_platform.action.get_item" />
</services>

</container>
22 changes: 22 additions & 0 deletions src/Exception/NotFoundHttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Exception;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as SymfonyNotFoundHttpException;

/**
* @author Théo FIDRY <[email protected]>
*/
class NotFoundHttpException extends SymfonyNotFoundHttpException implements ExceptionInterface
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ApiPlatform\Core\Api\CollectionDataProviderInterface;
use ApiPlatform\Core\Api\PaginatorInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Http\RequestAttributesExtractorInterface;
use Symfony\Component\HttpFoundation\Request;

/**
Expand All @@ -23,13 +24,20 @@
*/
final class GetCollectionAction
{
use ActionUtilTrait;

/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;

public function __construct(CollectionDataProviderInterface $collectionDataProvider)
/**
* @var RequestAttributesExtractorInterface
*/
private $attributesExtractor;

public function __construct(CollectionDataProviderInterface $collectionDataProvider, RequestAttributesExtractorInterface $attributesExtractor)
{
$this->collectionDataProvider = $collectionDataProvider;
$this->attributesExtractor = $attributesExtractor;
}

/**
Expand All @@ -43,8 +51,11 @@ public function __construct(CollectionDataProviderInterface $collectionDataProvi
*/
public function __invoke(Request $request)
{
list($resourceClass, $operationName) = $this->extractAttributes($request);
$attributesBag = $this->attributesExtractor->extract($request);

return $this->collectionDataProvider->getCollection($resourceClass, $operationName);
return $this->collectionDataProvider->getCollection(
$attributesBag->getResourceClass(),
$attributesBag->getCollectionOperationName()
);
}
}
28 changes: 21 additions & 7 deletions src/Action/GetItemAction.php → src/Http/Action/GetItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Action;
namespace ApiPlatform\Core\Http\Action;

use ApiPlatform\Core\Api\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Http\ItemDataProvider;
use ApiPlatform\Core\Http\RequestAttributesExtractorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand All @@ -23,13 +25,20 @@
*/
final class GetItemAction
{
use ActionUtilTrait;

/**
* @var ItemDataProvider
*/
private $itemDataProvider;

public function __construct(ItemDataProviderInterface $itemDataProvider)
/**
* @var RequestAttributesExtractorInterface
*/
private $attributesExtractor;

public function __construct(ItemDataProviderInterface $itemDataProvider, RequestAttributesExtractorInterface $attributesExtractor)
{
$this->itemDataProvider = $itemDataProvider;
$this->itemDataProvider = new ItemDataProvider($itemDataProvider);
$this->attributesExtractor = $attributesExtractor;
}

/**
Expand All @@ -45,8 +54,13 @@ public function __construct(ItemDataProviderInterface $itemDataProvider)
*/
public function __invoke(Request $request, $id)
{
list($resourceClass, , $operationName) = $this->extractAttributes($request);
$attributesBag = $this->attributesExtractor->extract($request);

return $this->getItem($this->itemDataProvider, $resourceClass, $operationName, $id);
return $this->itemDataProvider->getItem(
$this->itemDataProvider,
$attributesBag->getResourceClass(),
$attributesBag->getItemOperationName(),
$id
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ApiPlatform\Core\Action;

use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Http\RequestAttributesExtractorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;

Expand All @@ -22,13 +23,20 @@
*/
final class PostCollectionAction
{
use ActionUtilTrait;

/**
* @var SerializerInterface
*/
private $serializer;

public function __construct(SerializerInterface $serializer)
/**
* @var RequestAttributesExtractorInterface
*/
private $attributesExtractor;

public function __construct(SerializerInterface $serializer, RequestAttributesExtractorInterface $attributesExtractor)
{
$this->serializer = $serializer;
$this->attributesExtractor = $attributesExtractor;
}

/**
Expand All @@ -42,9 +50,17 @@ public function __construct(SerializerInterface $serializer)
*/
public function __invoke(Request $request)
{
list($resourceClass, $operationName, , $format) = $this->extractAttributes($request);
$context = ['resource_class' => $resourceClass, 'collection_operation_name' => $operationName];
$attributesBag = $this->attributesExtractor->extract($request);
$context = [
'resource_class' => $attributesBag->getResourceClass(),
'collection_operation_name' => $attributesBag->getCollectionOperationName()
];

return $this->serializer->deserialize($request->getContent(), $resourceClass, $format, $context);
return $this->serializer->deserialize(
$request->getContent(),
$attributesBag->getResourceClass(),
$attributesBag->getFormat(),
$context
);
}
}
Loading