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

Cecilapp / Cecil / 6588525571

20 Oct 2023 01:41PM UTC coverage: 82.252% (-0.06%) from 82.308%
6588525571

Pull #1822

github

web-flow
Merge 87e460a18 into 37a2fde2e
Pull Request #1822: feat: path prefix for default language

12 of 12 new or added lines in 5 files covered. (100.0%)

2827 of 3437 relevant lines covered (82.25%)

0.82 hits per line

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

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

144
                $this->generatedPages->add($alteredPage);
1✔
145
            }
146
        }
147
    }
148
}
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