Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

HydeFront   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A injectTailwindConfig() 0 16 3
A version() 0 3 1
A cdnLink() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Facades;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Str;
9
10
use function sprintf;
11
12
/**
13
 * HydeFront is the NPM package that bundles the default precompiled CSS and JavaScript assets for HydePHP.
14
 *
15
 * This facade makes it easy to access these assets from the HydeFront CDN, automatically getting the correct version.
16
 */
17
class HydeFront
18
{
19
    /** @var string The HydeFront SemVer tag to load. This constant is set to match the styles used for the installed framework version. */
20
    protected const HYDEFRONT_VERSION = 'v3.4';
21
22
    /** @var string The HydeFront CDN path pattern used to assemble CDN links. */
23
    protected const HYDEFRONT_CDN_URL = 'https://cdn.jsdelivr.net/npm/hydefront@%s/dist/%s';
24
25
    /**
26
     * Get the current version of the HydeFront package.
27
     *
28
     * @return string {@see HYDEFRONT_VERSION}
29
     */
30
    public static function version(): string
31
    {
32
        return static::HYDEFRONT_VERSION;
33
    }
34
35
    /** Get the CDN link for the HydeFront CSS file. This will return the default `app.css` file. */
36
    public static function cdnLink(): string
37
    {
38
        return sprintf(static::HYDEFRONT_CDN_URL, static::version(), 'app.css');
39
    }
40
41
    /** This method is used to inject the project's Tailwind CSS configuration into the Play CDN integration so it can match the styles. */
42
    public static function injectTailwindConfig(): string
43
    {
44
        if (! file_exists(Hyde::path('tailwind.config.js'))) {
0 ignored issues
show
Bug introduced by
The method path() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        if (! file_exists(Hyde::/** @scrutinizer ignore-call */ path('tailwind.config.js'))) {
Loading history...
45
            return '';
46
        }
47
48
        $config = Str::between(file_get_contents(Hyde::path('tailwind.config.js')), '{', '}');
49
50
        // Remove the plugins array, as it is not used in the frontend build.
51
        if (str_contains($config, 'plugins: [')) {
52
            $tokens = explode('plugins: [', $config, 2);
53
            $tokens[1] = Str::after($tokens[1], ']');
54
            $config = implode('', $tokens);
55
        }
56
57
        return preg_replace('/\s+/', ' ', "/* tailwind.config.js */ \n".rtrim($config, ",\n\r"));
58
    }
59
}
60