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

Cecilapp / Cecil / 14974508969

12 May 2025 02:10PM UTC coverage: 83.193% (-0.07%) from 83.261%
14974508969

push

github

ArnaudLigny
fix: menu url if remote

0 of 3 new or added lines in 1 file covered. (0.0%)

3064 of 3683 relevant lines covered (83.19%)

0.83 hits per line

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

87.5
/src/Url.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;
15

16
use Cecil\Assets\Asset;
17
use Cecil\Builder;
18
use Cecil\Collection\Menu\Entry as MenuEntry;
19
use Cecil\Collection\Page\Page;
20
use Cecil\Config;
21
use Cecil\Renderer\Page as PageRenderer;
22
use Cecil\Util;
23
use Cocur\Slugify\Slugify;
24

25
class Url
26
{
27
    /** @var Builder */
28
    protected $builder;
29

30
    /** @var Config */
31
    protected $config;
32

33
    /** @var string */
34
    protected $url;
35

36
    /** @var Page Slugifier */
37
    private static $slugifier;
38

39
    /**
40
     * Creates an URL from a Page, a Menu Entry, an Asset or a string.
41
     *
42
     * @param Builder                          $builder
43
     * @param Page|MenuEntry|Asset|string|null $value
44
     * @param array|null                       $options Rendering options, e.g.: ['canonical' => true, 'format' => 'html', 'language' => 'fr']
45
     */
46
    public function __construct(Builder $builder, $value, ?array $options = null)
47
    {
48
        $this->builder = $builder;
1✔
49
        $this->config = $builder->getConfig();
1✔
50
        if (!self::$slugifier instanceof Slugify) {
1✔
51
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
1✔
52
        }
53

54
        // handles options
55
        $canonical = null; // if true prefix url with baseurl config
1✔
56
        $format = null;    // output format
1✔
57
        $language = null;  // force language
1✔
58
        extract(\is_array($options) ? $options : [], EXTR_IF_EXISTS);
1✔
59

60
        // canonical URL?
61
        $base = '';
1✔
62
        if ($this->config->isEnabled('canonicalurl') || $canonical === true) {
1✔
63
            $base = rtrim((string) $this->config->get('baseurl'), '/');
1✔
64
        }
65
        if ($canonical === false) {
1✔
66
            $base = '';
1✔
67
        }
68

69
        // if value is empty (i.e.: `url()`) returns home URL
70
        if (\is_null($value) || empty($value) || $value == '/') {
1✔
71
            $this->url = $base . '/';
1✔
72

73
            return;
1✔
74
        }
75

76
        switch (true) {
77
            case $value instanceof Page: // $value is a Page
1✔
78
                /** @var Page $value */
79
                if (!$format) {
1✔
80
                    $format = $value->getVariable('output');
1✔
81
                    if (\is_array($value->getVariable('output'))) {
1✔
82
                        $default = array_search('html', $value->getVariable('output')) ?: 0;
1✔
83
                        $format = $value->getVariable('output')[$default];
1✔
84
                    }
85
                    if (!$format) {
1✔
86
                        $format = 'html';
1✔
87
                    }
88
                }
89
                $this->url = $base . '/' . ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
1✔
90
                if ($canonical && $value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL
1✔
91
                    $this->url = $value->getVariable('canonical')['url'];
×
92
                }
93
                break;
1✔
94
            case $value instanceof MenuEntry: // $value is a Menu Entry
1✔
95
                /** @var MenuEntry $value */
NEW
96
                if (Util\File::isRemote($value['url'])) {
×
NEW
97
                    $this->url = $value['url'];
×
NEW
98
                    break;
×
99
                }
100
                $this->url = $base . '/' . ltrim($value['url'], '/');
×
101
                break;
×
102
            case $value instanceof Asset: // $value is an Asset
1✔
103
                /** @var Asset $value */
104
                $this->url = $base . '/' . ltrim($value['path'], '/');
1✔
105
                if ($value->isImageInCdn()) {
1✔
106
                    $this->url = (string) $value;
×
107
                }
108
                break;
1✔
109
            case \is_string($value): // others cases
1✔
110
                /** @var string $value */
111
                // $value is a potential Page ID
112
                $pageId = self::$slugifier->slugify($value);
1✔
113
                // should force language?
114
                $lang = '';
1✔
115
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
1✔
116
                    $pageId = "$language/$pageId";
1✔
117
                    $lang = "$language/";
1✔
118
                }
119
                switch (true) {
120
                    case Util\File::isRemote($value): // $value is an external URL
1✔
121
                        $this->url = $value;
1✔
122
                        break;
1✔
123
                    case $this->builder->getPages()->has($pageId): // $pageId exists in pages collection
1✔
124
                        $this->url = (string) new self($this->builder, $this->builder->getPages()->get($pageId), $options);
1✔
125
                        break;
1✔
126
                    default:
127
                        // remove double language prefix
128
                        if ($lang && Util\Str::startsWith($value, $lang)) {
1✔
129
                            $value = substr($value, \strlen($lang));
1✔
130
                        }
131
                        $this->url = $base . '/' . $lang . ltrim($value, '/');
1✔
132
                }
133
        }
134
    }
135

136
    /**
137
     * If called like a string returns built URL.
138
     */
139
    public function __toString(): string
140
    {
141
        return $this->getUrl();
1✔
142
    }
143

144
    /**
145
     * Returns built URL.
146
     */
147
    public function getUrl(): string
148
    {
149
        return (string) $this->url;
1✔
150
    }
151
}
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