Passed
Push — master ( a91c75...3d6eb6 )
by Caen
03:47 queued 12s
created

Hyperlinks::formatHtmlPath()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 16
rs 9.6111
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 link to an HTML file, allowing for pretty URLs, if enabled.
27
     *
28
     * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFormatHtmlPathTest
29
     */
30
    public function formatHtmlPath(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
                if ($destination === DocumentationPage::getOutputDirectory().'/index.html') {
38
                    return DocumentationPage::getOutputDirectory().'/';
39
                }
40
41
                return substr($destination, 0, -5);
42
            }
43
        }
44
45
        return $destination;
46
    }
47
48
    /**
49
     * Inject the proper number of `../` before the links in Blade templates.
50
     *
51
     * @param  string  $destination  relative to output directory on compiled site
52
     * @return string
53
     *
54
     * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFileHelperRelativeLinkTest
55
     */
56
    public function relativeLink(string $destination): string
57
    {
58
        if (str_starts_with($destination, '../')) {
59
            return $destination;
60
        }
61
62
        $nestCount = substr_count($this->kernel->currentPage(), '/');
63
        $route = '';
64
        if ($nestCount > 0) {
65
            $route .= str_repeat('../', $nestCount);
66
        }
67
        $route .= $this->formatHtmlPath($destination);
68
69
        return str_replace('//', '/', $route);
70
    }
71
72
    /**
73
     * Gets a relative web link to the given image stored in the _site/media folder.
74
     */
75
    public function image(string $name): string
76
    {
77
        if (str_starts_with($name, 'http')) {
78
            return $name;
79
        }
80
81
        return $this->relativeLink('media/'.basename($name));
82
    }
83
84
    /**
85
     * Check if a site base URL has been set in config (or .env).
86
     */
87
    public function hasSiteUrl(): bool
88
    {
89
        return ! blank(config('site.url'));
90
    }
91
92
    /**
93
     * Return a qualified URI path to the supplied path if a base URL is set.
94
     *
95
     * @param  string  $path  optional relative path suffix. Omit to return base url.
96
     * @param  string|null  $default  optional default value to return if no site url is set.
97
     * @return string
98
     *
99
     * @throws BaseUrlNotSetException If no site URL is set and no default is provided
100
     */
101
    public function url(string $path = '', ?string $default = null): string
102
    {
103
        $path = $this->formatHtmlPath(trim($path, '/'));
104
105
        if ($this->hasSiteUrl()) {
106
            return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/');
107
        }
108
109
        if ($default !== null) {
110
            return $default.'/'.($path ?? '');
111
        }
112
113
        throw new BaseUrlNotSetException();
114
    }
115
}
116