1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
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 Symplify\PHP7_Sculpin\Bundle\PaginationBundle; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderManager; |
15
|
|
|
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorInterface; |
16
|
|
|
use Symplify\PHP7_Sculpin\Core\Permalink\SourcePermalinkFactory; |
17
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\SourceInterface; |
18
|
|
|
|
19
|
|
|
final class PaginationGenerator implements GeneratorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DataProviderManager |
23
|
|
|
*/ |
24
|
|
|
private $dataProviderManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $maxPerPage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SourcePermalinkFactory |
33
|
|
|
*/ |
34
|
|
|
private $sourcePermalinkFactory; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
DataProviderManager $dataProviderManager, |
38
|
|
|
int $maxPerPage, |
39
|
|
|
SourcePermalinkFactory $sourcePermalinkFactory |
40
|
|
|
) { |
41
|
|
|
$this->dataProviderManager = $dataProviderManager; |
42
|
|
|
$this->maxPerPage = $maxPerPage; |
43
|
|
|
$this->sourcePermalinkFactory = $sourcePermalinkFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function generate(SourceInterface $source) : array |
50
|
|
|
{ |
51
|
|
|
$data = null; |
52
|
|
|
$config = $source->data()->get('pagination') ?: []; |
53
|
|
|
if (!isset($config['provider'])) { |
54
|
|
|
$config['provider'] = 'data.posts'; |
55
|
|
|
} |
56
|
|
|
if (preg_match('/^(data|page)\.(.+)$/', $config['provider'], $matches)) { |
57
|
|
|
switch ($matches[1]) { |
58
|
|
|
case 'data': |
59
|
|
|
$data = $this->dataProviderManager->dataProvider($matches[2])->provideData(); |
60
|
|
|
break; |
61
|
|
|
case 'page': |
62
|
|
|
$data = $source->data()->get($matches[2]); |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (null === $data) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$maxPerPage = isset($config['max_per_page']) ? $config['max_per_page'] : $this->maxPerPage; |
72
|
|
|
|
73
|
|
|
$slices = []; |
74
|
|
|
$slice = []; |
75
|
|
|
$totalItems = 0; |
76
|
|
|
foreach ($data as $k => $v) { |
77
|
|
|
if (count($slice) == $maxPerPage) { |
78
|
|
|
$slices[] = $slice; |
79
|
|
|
$slice = []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$slice[$k] = $v; |
83
|
|
|
++$totalItems; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (count($slice)) { |
87
|
|
|
$slices[] = $slice; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$sources = []; |
91
|
|
|
$pageNumber = 0; |
92
|
|
|
foreach ($slices as $slice) { |
93
|
|
|
++$pageNumber; |
94
|
|
|
$permalink = null; |
95
|
|
|
if ($pageNumber > 1) { |
96
|
|
|
$permalink = $this->sourcePermalinkFactory->create($source)->relativeFilePath(); |
97
|
|
|
$basename = basename($permalink); |
98
|
|
|
if (preg_match('/^(.+?)\.(.+)$/', $basename, $matches)) { |
99
|
|
|
if ('index' === $matches[1]) { |
100
|
|
|
$paginatedPage = ''; |
101
|
|
|
$index = '/index'; |
102
|
|
|
} else { |
103
|
|
|
$paginatedPage = $matches[1].'/'; |
104
|
|
|
$index = ''; |
105
|
|
|
} |
106
|
|
|
$permalink = dirname($permalink).'/'.$paginatedPage.'page/'.$pageNumber.$index.'.'.$matches[2]; |
107
|
|
|
} else { |
108
|
|
|
$permalink = dirname($permalink).'/'.$basename.'/page/'.$pageNumber.'.html'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (0 === strpos($permalink, './')) { |
112
|
|
|
$permalink = substr($permalink, 2); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$generatedSource = $source->duplicate( |
117
|
|
|
$source->sourceId().':page='.$pageNumber |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
if (null !== $permalink) { |
121
|
|
|
$generatedSource->data()->set('permalink', $permalink); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$generatedSource->data()->set('pagination.items', $slice); |
125
|
|
|
$generatedSource->data()->set('pagination.page', $pageNumber); |
126
|
|
|
$generatedSource->data()->set('pagination.total_pages', count($slices)); |
127
|
|
|
$generatedSource->data()->set('pagination.total_items', $totalItems); |
128
|
|
|
|
129
|
|
|
$sources[] = $generatedSource; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
for ($i = 0; $i < count($sources); ++$i) { |
|
|
|
|
133
|
|
|
$generatedSource = $sources[$i]; |
134
|
|
|
if (0 === $i) { |
135
|
|
|
$generatedSource->data()->set('pagination.previous_page', null); |
136
|
|
|
} else { |
137
|
|
|
$generatedSource->data()->set('pagination.previous_page', $sources[$i - 1]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($i + 1 < count($sources)) { |
141
|
|
|
$generatedSource->data()->set('pagination.next_page', $sources[$i + 1]); |
142
|
|
|
} else { |
143
|
|
|
$generatedSource->data()->set('pagination.next_page', null); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $sources; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: