|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Route; |
|
6
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
|
7
|
|
|
use Pagerfanta\Pagerfanta; |
|
8
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
|
9
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface; |
|
10
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var PagerfantaFactory |
|
19
|
|
|
*/ |
|
20
|
|
|
private $pagerfantaRepresentationFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param PagerfantaFactory $pagerfantaRepresentationFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(PagerfantaFactory $pagerfantaRepresentationFactory) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->pagerfantaRepresentationFactory = $pagerfantaRepresentationFactory; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository) |
|
34
|
|
|
{ |
|
35
|
|
|
$resources = $this->getResources($requestConfiguration, $repository); |
|
36
|
|
|
|
|
37
|
|
|
if ($resources instanceof Pagerfanta) { |
|
38
|
|
|
$request = $requestConfiguration->getRequest(); |
|
39
|
|
|
$resources->setCurrentPage($request->query->get('page', 1)); |
|
40
|
|
|
$resources->setMaxPerPage($request->query->get('limit', $requestConfiguration->getPaginationMaxPerPage())); |
|
41
|
|
|
|
|
42
|
|
|
if (!$requestConfiguration->isHtmlRequest()) { |
|
43
|
|
|
$route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all())); |
|
44
|
|
|
|
|
45
|
|
|
return $this->pagerfantaRepresentationFactory->createRepresentation($resources, $route); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $resources; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RequestConfiguration $requestConfiguration |
|
54
|
|
|
* @param RepositoryInterface $repository |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getResources(RequestConfiguration $requestConfiguration, RepositoryInterface $repository) |
|
59
|
|
|
{ |
|
60
|
|
|
if (null !== $repositoryMethod = $requestConfiguration->getRepositoryMethod()) { |
|
61
|
|
|
$callable = [$repository, $repositoryMethod]; |
|
62
|
|
|
|
|
63
|
|
|
$resources = call_user_func_array($callable, $requestConfiguration->getRepositoryArguments()); |
|
64
|
|
|
|
|
65
|
|
|
return $resources; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!$requestConfiguration->isPaginated() && !$requestConfiguration->isLimited()) { |
|
69
|
|
|
return $repository->findAll(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!$requestConfiguration->isPaginated()) { |
|
73
|
|
|
return $repository->findBy($requestConfiguration->getCriteria(), $requestConfiguration->getSorting(), $requestConfiguration->getLimit()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $repository->createPaginator($requestConfiguration->getCriteria(), $requestConfiguration->getSorting()); |
|
77
|
|
|
} |
|
78
|
|
|
} |