Passed
Pull Request — v1 (#27)
by
unknown
07:20
created

Manifest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 159
rs 10
c 2
b 0
f 0
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getCssModuleTags() 0 6 1
A getCssRelPreloadPolyfill() 0 3 1
A getJsModuleTags() 0 6 1
A getCriticalCssTags() 0 6 1
A getCssInlineTags() 0 3 1
A getSafariNomoduleFix() 0 3 1
A getModule() 0 6 1
A getModuleHash() 0 6 1
A invalidateCaches() 0 3 1
A getFileFromManifest() 0 6 1
A getFile() 0 3 1
1
<?php
2
/**
3
 * Twigpack plugin for Craft CMS 3.x
4
 *
5
 * Twigpack is the conduit between Twig and webpack, with manifest.json &
6
 * webpack-dev-server HMR support
7
 *
8
 * @link      https://nystudio107.com/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\twigpack\services;
13
14
use nystudio107\twigpack\Twigpack;
15
use nystudio107\twigpack\helpers\Manifest as ManifestHelper;
16
17
use craft\base\Component;
18
19
use yii\web\NotFoundHttpException;
20
21
/** @noinspection MissingPropertyAnnotationsInspection */
22
23
/**
24
 * @author    nystudio107
25
 * @package   Twigpack
26
 * @since     1.0.0
27
 */
28
class Manifest extends Component
29
{
30
    // Public Methods
31
    // =========================================================================
32
33
    /**
34
     * Return the HTML tags to include the CSS
35
     *
36
     * @param string     $moduleName
37
     * @param bool       $async
38
     * @param null|array $config
39
     *
40
     * @return string
41
     * @throws NotFoundHttpException
42
     */
43
    public function getCssModuleTags(string $moduleName, bool $async = false, $config = null): string
44
    {
45
        $settings = Twigpack::$plugin->getSettings();
46
        $config = $config ?? $settings->getAttributes();
47
48
        return ManifestHelper::getCssModuleTags($config, $moduleName, $async);
49
    }
50
51
    /**
52
     * Returns the CSS file in $path wrapped in <style></style> tags
53
     *
54
     * @param $path
55
     *
56
     * @return string
57
     */
58
    public function getCssInlineTags(string $path): string
59
    {
60
        return ManifestHelper::getCssInlineTags($path);
61
    }
62
63
    /**
64
     * @param array       $config
65
     * @param null|string $name
66
     *
67
     * @return string
68
     */
69
    public function getCriticalCssTags($name = null, $config = null): string
70
    {
71
        $settings = Twigpack::$plugin->getSettings();
72
        $config = $config ?? $settings->getAttributes();
73
74
        return ManifestHelper::getCriticalCssTags($config, $name);
75
    }
76
77
    /**
78
     * Returns the uglified loadCSS rel=preload Polyfill as per:
79
     * https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example
80
     *
81
     * @return string
82
     */
83
    public function getCssRelPreloadPolyfill(): string
84
    {
85
        return ManifestHelper::getCssRelPreloadPolyfill();
86
    }
87
88
    /**
89
     * Return the HTML tags to include the JavaScript module
90
     *
91
     * @param string     $moduleName
92
     * @param bool       $async
93
     * @param null|array $config
94
     *
95
     * @return null|string
96
     * @throws NotFoundHttpException
97
     */
98
    public function getJsModuleTags(string $moduleName, bool $async = false, $config = null)
99
    {
100
        $settings = Twigpack::$plugin->getSettings();
101
        $config = $config ?? $settings->getAttributes();
102
103
        return ManifestHelper::getJsModuleTags($config, $moduleName, $async);
104
    }
105
106
    /**
107
     * Return the Safari 10.1 nomodule JavaScript fix
108
     *
109
     * @return string
110
     */
111
    public function getSafariNomoduleFix(): string
112
    {
113
        return ManifestHelper::getSafariNomoduleFix();
114
    }
115
116
    /**
117
     * Return the URI to a module
118
     *
119
     * @param string $moduleName
120
     * @param string $type
121
     * @param null   $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
122
     *
123
     * @return null|string
124
     * @throws NotFoundHttpException
125
     */
126
    public function getModule(string $moduleName, string $type = 'modern', $config = null)
127
    {
128
        $settings = Twigpack::$plugin->getSettings();
129
        $config = $config ?? $settings->getAttributes();
130
131
        return ManifestHelper::getModule($config, $moduleName, $type);
132
    }
133
134
	/**
135
	 * Return the HASH value from a module
136
	 *
137
	 * @param string $moduleName
138
	 * @param string $type
139
	 * @param null   $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
140
	 *
141
	 * @return null|string
142
	 * @throws NotFoundHttpException
143
	 */
144
	public function getModuleHash(string $moduleName, string $type = 'modern', $config = null)
145
	{
146
		$settings = Twigpack::$plugin->getSettings();
147
		$config = $config ?? $settings->getAttributes();
148
149
		return ManifestHelper::getModuleHash($config, $moduleName, $type);
150
	}
151
152
	/**
153
     * Returns the contents of a file from a URI path
154
     *
155
     * @param string $path
156
     *
157
     * @return string
158
     */
159
    public function getFile(string $path): string
160
    {
161
        return ManifestHelper::getFile($path);
162
    }
163
164
    /**
165
     * Returns the contents of a file from the $fileName in the manifest
166
     *
167
     * @param string $fileName
168
     * @param string $type
169
     * @param null   $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
170
     *
171
     * @return string
172
     */
173
    public function getFileFromManifest(string $fileName, string $type = 'legacy', $config = null): string
174
    {
175
        $settings = Twigpack::$plugin->getSettings();
176
        $config = $config ?? $settings->getAttributes();
177
178
        return ManifestHelper::getFileFromManifest($config, $fileName, $type);
179
    }
180
181
    /**
182
     * Invalidate the manifest cache
183
     */
184
    public function invalidateCaches()
185
    {
186
        ManifestHelper::invalidateCaches();
187
    }
188
}
189