Passed
Push — v1 ( f6b6a9...6b5c66 )
by Andrew
18:02 queued 13:21
created

src/services/Manifest.php (4 issues)

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
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
40
     *
41
     * @return string
42
     * @throws NotFoundHttpException
43
     */
44
    public function getCssModuleTags(string $moduleName, bool $async = false, $config = null, array $attributes = []): string
45
    {
46
        $settings = Twigpack::$plugin->getSettings();
47
        $config = $config ?? $settings->getAttributes();
48
49
        return ManifestHelper::getCssModuleTags($config, $moduleName, $async, $attributes);
50
    }
51
52
    /**
53
     * Returns the CSS file in $path wrapped in <style></style> tags
54
     *
55
     * @param $path
56
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
57
     *
58
     * @return string
59
     */
60
    public function getCssInlineTags(string $path, array $attributes = []): string
61
    {
62
        return ManifestHelper::getCssInlineTags($path, $attributes);
63
    }
64
65
    /**
66
     * @param array $config
67
     * @param null|string $name
68
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
69
     *
70
     * @return string
71
     * @throws \Twig\Error\LoaderError
72
     */
73
    public function getCriticalCssTags($name = null, $config = null, array $attributes = []): string
74
    {
75
        $settings = Twigpack::$plugin->getSettings();
76
        $config = $config ?? $settings->getAttributes();
77
78
        return ManifestHelper::getCriticalCssTags($config, $name, $attributes);
79
    }
80
81
    /**
82
     * Returns the uglified loadCSS rel=preload Polyfill as per:
83
     * https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example
84
     *
85
     * @return string
86
     */
87
    public function getCssRelPreloadPolyfill(): string
88
    {
89
        return ManifestHelper::getCssRelPreloadPolyfill();
0 ignored issues
show
Deprecated Code introduced by
The function nystudio107\twigpack\hel...CssRelPreloadPolyfill() has been deprecated: in 1.2.0 ( Ignorable by Annotation )

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

89
        return /** @scrutinizer ignore-deprecated */ ManifestHelper::getCssRelPreloadPolyfill();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
    }
91
92
    /**
93
     * Return the HTML tags to include the JavaScript module
94
     *
95
     * @param string     $moduleName
96
     * @param bool       $async
97
     * @param null|array $config
98
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
99
     *
100
     * @return null|string
101
     * @throws NotFoundHttpException
102
     */
103
    public function getJsModuleTags(string $moduleName, bool $async = false, $config = null, array $attributes = [])
104
    {
105
        $settings = Twigpack::$plugin->getSettings();
106
        $config = $config ?? $settings->getAttributes();
107
108
        return ManifestHelper::getJsModuleTags($config, $moduleName, $async, $attributes);
109
    }
110
111
    /**
112
     * Return the Safari 10.1 nomodule JavaScript fix
113
     *
114
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
115
     *
116
     * @return string
117
     */
118
    public function getSafariNomoduleFix(array $attributes = []): string
119
    {
120
        return ManifestHelper::getSafariNomoduleFix($attributes);
121
    }
122
123
    /**
124
     * Return the URI to a module
125
     *
126
     * @param string $moduleName
127
     * @param string $type
128
     * @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...
129
     *
130
     * @return null|string
131
     * @throws NotFoundHttpException
132
     */
133
    public function getModule(string $moduleName, string $type = 'modern', $config = null)
134
    {
135
        $settings = Twigpack::$plugin->getSettings();
136
        $config = $config ?? $settings->getAttributes();
137
138
        return ManifestHelper::getModule($config, $moduleName, $type);
139
    }
140
141
    /**
142
     * Return the HASH value from a module
143
     *
144
     * @param string $moduleName
145
     * @param string $type
146
     * @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...
147
     *
148
     * @return null|string
149
     * @throws NotFoundHttpException
150
     */
151
    public function getModuleHash(string $moduleName, string $type = 'modern', $config = null)
152
    {
153
        $settings = Twigpack::$plugin->getSettings();
154
        $config = $config ?? $settings->getAttributes();
155
156
        return ManifestHelper::getModuleHash($config, $moduleName, $type);
157
    }
158
159
    /**
160
     * Returns the contents of a file from a URI path
161
     *
162
     * @param string $path
163
     *
164
     * @return string
165
     */
166
    public function getFile(string $path): string
167
    {
168
        return ManifestHelper::getFile($path);
169
    }
170
171
    /**
172
     * Returns the contents of a file from the $fileName in the manifest
173
     *
174
     * @param string $fileName
175
     * @param string $type
176
     * @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...
177
     *
178
     * @return string
179
     */
180
    public function getFileFromManifest(string $fileName, string $type = 'legacy', $config = null): string
181
    {
182
        $settings = Twigpack::$plugin->getSettings();
183
        $config = $config ?? $settings->getAttributes();
184
185
        return ManifestHelper::getFileFromManifest($config, $fileName, $type);
186
    }
187
188
    /**
189
     * Invalidate the manifest cache
190
     */
191
    public function invalidateCaches()
192
    {
193
        ManifestHelper::invalidateCaches();
194
    }
195
}
196