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