• 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/Renderer/Page.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\Renderer;
15

16
use Cecil\Collection\Page\Page as PageItem;
17
use Cecil\Config;
18

19
/**
20
 * Class Renderer\Page.
21
 */
22
class Page
23
{
24
    /** @var Config */
25
    protected $config;
26

27
    public function __construct(Config $config)
28
    {
29
        $this->config = $config;
1✔
30
    }
31

32
    /**
33
     * Returns the path of the rendered file, based on the output format properties.
34
     *
35
     * Use cases:
36
     *   - default: path + filename + extension (e.g.: 'blog/post-1/index.html')
37
     *   - with subpath: path + subpath + filename + extension (e.g.: 'blog/post-1/amp/index.html')
38
     *   - ugly URL: path + extension (e.g.: '404.html', 'sitemap.xml', 'robots.txt')
39
     *   - path only (e.g.: '_redirects')
40
     *   - i18n: language code + default (e.g.: 'fr/blog/page/index.html')
41
     *
42
     * @param PageItem $page
43
     * @param string   $format Output format (ie: 'html', 'amp', 'json', etc.)
44
     */
45
    public function getOutputFilePath(PageItem $page, string $format): string
46
    {
47
        $path = $page->getPath();
1✔
48
        $subpath = (string) $this->config->getOutputFormatProperty($format, 'subpath');
1✔
49
        $filename = (string) $this->config->getOutputFormatProperty($format, 'filename');
1✔
50
        $extension = (string) $this->config->getOutputFormatProperty($format, 'extension');
1✔
51
        $uglyurl = (bool) $page->getVariable('uglyurl');
1✔
52
        $language = $page->getVariable('language');
1✔
53
        // is ugly URL?
54
        if ($uglyurl) {
1✔
55
            $filename = '';
1✔
56
        }
57
        // add extension if exists
58
        if ($extension) {
1✔
59
            $extension = sprintf('.%s', $extension);
1✔
60
        }
61
        // homepage special case (need "index")
62
        if (empty($path) && empty($filename)) {
1✔
63
            $path = 'index';
1✔
64
        }
65
        // do not prefix path with language code for the default language
66
        if ($language === null || ($language == $this->config->getLanguageDefault() && !$this->config->get('language.prefix'))) {
1✔
67
            $language = '';
1✔
68
        }
69
        // do not prefix path of the home page with language code for the default language
70
        if ($language == $this->config->getLanguageDefault() && $page->getType() == 'homepage') {
1✔
71
            $language = '';
×
72
        }
73

74
        return \Cecil\Util::joinPath($language, $path, $subpath, $filename) . $extension;
1✔
75
    }
76

77
    /**
78
     * Returns the public URL.
79
     *
80
     * @param PageItem $page
81
     * @param string   $format Output format (ie: 'html', 'amp', 'json', etc.), 'html' by default
82
     */
83
    public function getUrl(PageItem $page, string $format = 'html'): string
84
    {
85
        $output = $this->getOutputFilePath($page, $format);
1✔
86

87
        // remove "index.html" if not uglyurl
88
        if (!($page->getVariable('uglyurl') ?? false)) {
1✔
89
            $output = str_replace('index.html', '', $output);
1✔
90
        }
91

92
        return $output;
1✔
93
    }
94
}
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