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

Cecilapp / Cecil / 7126338718

07 Dec 2023 09:37AM UTC coverage: 80.668% (-1.9%) from 82.534%
7126338718

Pull #1676

github

web-flow
Merge db214c75c into 814daa587
Pull Request #1676: 8.x dev

174 of 212 new or added lines in 28 files covered. (82.08%)

102 existing lines in 10 files now uncovered.

2779 of 3445 relevant lines covered (80.67%)

0.81 hits per line

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

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

UNCOV
144
                $this->generatedPages->add($alteredPage);
×
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

© 2025 Coveralls, Inc