Passed
Push — master ( 9ca6d3...427e8a )
by Caen
03:12 queued 12s
created

Hyperlinks::mediaLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Framework\Exceptions\BaseUrlNotSetException;
8
use Hyde\Pages\DocumentationPage;
9
10
/**
11
 * Contains helpers and logic for resolving web paths for compiled files.
12
 *
13
 * It's bound to the HydeKernel instance, and is an integral part of the framework.
14
 *
15
 * @see \Hyde\Framework\Testing\Feature\Foundation\HyperlinksTest
16
 */
17
class Hyperlinks
18
{
19
    protected HydeKernel $kernel;
20
21
    public function __construct(HydeKernel $kernel)
22
    {
23
        $this->kernel = $kernel;
24
    }
25
26
    /**
27
     * Format a web link to an HTML file, allowing for pretty URLs, if enabled.
28
     *
29
     * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkformatLinkTest
30
     */
31
    public function formatLink(string $destination): string
32
    {
33
        if (config('site.pretty_urls', false) === true) {
34
            if (str_ends_with($destination, '.html')) {
35
                if ($destination === 'index.html') {
36
                    return '/';
37
                }
38
39
                if ($destination === DocumentationPage::outputDirectory().'/index.html') {
40
                    return DocumentationPage::outputDirectory().'/';
41
                }
42
43
                return substr($destination, 0, -5);
44
            }
45
        }
46
47
        return $destination;
48
    }
49
50
    /**
51
     * Inject the proper number of `../` before the links in Blade templates.
52
     *
53
     * @param  string  $destination  relative to output directory on compiled site
54
     *
55
     * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFileHelperRelativeLinkTest
56
     */
57
    public function relativeLink(string $destination): string
58
    {
59
        if (str_starts_with($destination, '../')) {
60
            return $destination;
61
        }
62
63
        $nestCount = substr_count($this->kernel->currentPage() ?? '', '/');
64
        $route = '';
65
        if ($nestCount > 0) {
66
            $route .= str_repeat('../', $nestCount);
67
        }
68
        $route .= $this->formatLink($destination);
69
70
        return str_replace('//', '/', $route);
71
    }
72
73
    /**
74
     * Gets a relative web link to the given file stored in the _site/media folder.
75
     *
76
     * @todo Add default option that throws if file is not present in _site/media (or just) _media?
77
     */
78
    public function mediaLink(string $destination): string
79
    {
80
        return $this->relativeLink("media/$destination");
81
    }
82
83
    /**
84
     * Gets a relative web link to the given image stored in the _site/media folder.
85
     * If the image is remote (starts with http) it will be returned as is.
86
     *
87
     * If true is passed as the second argument, and a base URL is set,
88
     * the image will be returned with a qualified absolute URL.
89
     */
90
    public function image(string $name, bool $preferQualifiedUrl = false): string
91
    {
92
        if (str_starts_with($name, 'http')) {
93
            return $name;
94
        }
95
96
        if ($preferQualifiedUrl && $this->hasSiteUrl()) {
97
            return $this->url('media/'.basename($name));
98
        }
99
100
        return $this->relativeLink('media/'.basename($name));
101
    }
102
103
    /**
104
     * Check if a site base URL has been set in config (or .env).
105
     */
106
    public function hasSiteUrl(): bool
107
    {
108
        return ! blank(config('site.url'));
109
    }
110
111
    /**
112
     * Return a qualified URL to the supplied path if a base URL is set.
113
     *
114
     * @param  string  $path  optional relative path suffix. Omit to return base url.
115
     *
116
     * @throws BaseUrlNotSetException If no site URL is set and no default is provided
117
     */
118
    public function url(string $path = ''): string
119
    {
120
        $path = $this->formatLink(trim($path, '/'));
121
122
        if ($this->hasSiteUrl()) {
123
            return rtrim(rtrim((string) config('site.url'), '/')."/$path", '/');
124
        }
125
126
        throw new BaseUrlNotSetException();
127
    }
128
}
129