Passed
Push — master ( d6782e...149d2c )
by Caen
12:13 queued 11s
created

Hyperlinks::image()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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