Passed
Push — master ( ada723...49af47 )
by Caen
03:13 queued 12s
created

Hyperlinks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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