• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 6254695617

20 Sep 2023 10:20PM UTC coverage: 82.303% (-0.07%) from 82.368%
6254695617

push

github

ArnaudLigny
fix: coveralls download link

2823 of 3430 relevant lines covered (82.3%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.51
/src/Generator/Pagination.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Generator;
15

16
use Cecil\Collection\Page\Collection as PagesCollection;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Collection\Page\Type;
19

20
/**
21
 * Class Generator\Pagination.
22
 */
23
class Pagination extends AbstractGenerator implements GeneratorInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function generate(): void
29
    {
30
        if ($this->config->get('pagination.enabled') === false) {
1✔
31
            return;
×
32
        }
33

34
        // filters list pages (home, sections and terms)
35
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
1✔
36
            return \in_array($page->getType(), [Type::HOMEPAGE, Type::SECTION, Type::TERM]);
1✔
37
        });
1✔
38
        /** @var Page $page */
39
        foreach ($filteredPages as $page) {
1✔
40
            $pages = $page->getPages()->filter(function (Page $page) {
1✔
41
                return $page->getVariable('published');
1✔
42
            });
1✔
43
            // if no sub-pages: by-pass
44
            if ($pages === null) {
1✔
45
                continue;
×
46
            }
47
            $path = $page->getPath();
1✔
48
            // site pagination configuration
49
            $paginationPerPage = \intval($this->config->get('pagination.max') ?? 5);
1✔
50
            $paginationPath = (string) $this->config->get('pagination.path') ?? 'page';
1✔
51
            // page pagination configuration
52
            $pagePagination = $page->getVariable('pagination');
1✔
53
            if ($pagePagination) {
1✔
54
                if (isset($pagePagination['enabled']) && $pagePagination['enabled'] === false) {
1✔
55
                    continue;
×
56
                }
57
                if (isset($pagePagination['max'])) {
1✔
58
                    $paginationPerPage = \intval($pagePagination['max']);
1✔
59
                }
60
                if (isset($pagePagination['path'])) {
1✔
61
                    $paginationPath = (string) $pagePagination['path'];
1✔
62
                }
63
            }
64
            $pagesTotal = \count($pages);
1✔
65
            // is pagination not necessary?
66
            if ($pagesTotal <= $paginationPerPage) {
1✔
67
                continue;
1✔
68
            }
69
            // sorts pages
70
            $pages = Section::sortSubPages($page, $pages);
1✔
71
            // builds paginator
72
            $paginatorPagesCount = \intval(ceil($pagesTotal / $paginationPerPage));
1✔
73
            for ($i = 0; $i < $paginatorPagesCount; $i++) {
1✔
74
                $itPagesInPagination = new \LimitIterator($pages->getIterator(), $i * $paginationPerPage, $paginationPerPage);
1✔
75
                $pagesInPagination = new PagesCollection(
1✔
76
                    $page->getId() . '-page-' . ($i + 1),
1✔
77
                    iterator_to_array($itPagesInPagination)
1✔
78
                );
1✔
79
                $alteredPage = clone $page;
1✔
80
                if ($i == 0) { // first page (ie: blog/page/1 -> blog)
1✔
81
                    $pageId = $page->getId();
1✔
82
                    $alteredPage
1✔
83
                        ->setVariable('alias', [
1✔
84
                            sprintf('%s/%s/%s', $path, $paginationPath, 1),
1✔
85
                        ]);
1✔
86
                } else { // others pages (ie: blog/page/X)
87
                    $pageId = Page::slugify(sprintf('%s/%s/%s', $page->getId(), $paginationPath, $i + 1));
1✔
88
                    $alteredPage
1✔
89
                        ->setId($pageId)
1✔
90
                        ->setVirtual(true)
1✔
91
                        ->setPath(Page::slugify(sprintf('%s/%s/%s', $path, $paginationPath, $i + 1)))
1✔
92
                        ->unVariable('menu')
1✔
93
                        ->unVariable('alias')
1✔
94
                        ->unVariable('aliases') // backward compatibility
1✔
95
                        ->unVariable('langref')
1✔
96
                        ->setVariable('paginated', true);
1✔
97
                }
98
                // set paginator values
99
                $paginator = [
1✔
100
                    'pages'       => $pagesInPagination,
1✔
101
                    'pages_total' => $pagesTotal,
1✔
102
                    'totalpages'  => $pagesTotal, // backward compatibility
1✔
103
                    'count'       => $paginatorPagesCount,
1✔
104
                    'current'     => $i + 1,
1✔
105
                ];
1✔
106
                // adds links
107
                $paginator['links'] = ['first' => $page->getId() ?: 'index'];
1✔
108
                if ($i == 1) {
1✔
109
                    $paginator['links'] += ['prev' => $page->getId() ?: 'index'];
1✔
110
                }
111
                if ($i > 1) {
1✔
112
                    $paginator['links'] += ['prev' => Page::slugify(sprintf(
1✔
113
                        '%s/%s/%s',
1✔
114
                        $page->getId(),
1✔
115
                        $paginationPath,
1✔
116
                        $i
1✔
117
                    ))];
1✔
118
                }
119
                $paginator['links'] += ['self' => $pageId ?: 'index'];
1✔
120
                if ($i < $paginatorPagesCount - 1) {
1✔
121
                    $paginator['links'] += ['next' => Page::slugify(sprintf(
1✔
122
                        '%s/%s/%s',
1✔
123
                        $page->getId(),
1✔
124
                        $paginationPath,
1✔
125
                        $i + 2
1✔
126
                    ))];
1✔
127
                }
128
                $paginator['links'] += ['last' => Page::slugify(sprintf(
1✔
129
                    '%s/%s/%s',
1✔
130
                    $page->getId(),
1✔
131
                    $paginationPath,
1✔
132
                    $paginatorPagesCount
1✔
133
                ))];
1✔
134
                $paginator['links'] += ['path' => Page::slugify(sprintf('%s/%s', $page->getId(), $paginationPath))];
1✔
135
                // set paginator to cloned page
136
                $alteredPage->setPaginator($paginator);
1✔
137
                $alteredPage->setVariable('pagination', $paginator); // backward compatibility
1✔
138
                // updates date with the first element of the collection
139
                $alteredPage->setVariable('date', $pagesInPagination->first()->getVariable('date'));
1✔
140

141
                $this->generatedPages->add($alteredPage);
1✔
142
            }
143
        }
144
    }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc