@@ -103,27 +103,8 @@ public function afterCreateRequireJsMixinsAsset(
103103 FileManager $ subject ,
104104 File $ result
105105 ): File {
106- if (PHP_SAPI == 'cli ' ) {
107- if (in_array ($ result ->getContentType (), self ::CONTENT_TYPES )) {
108- try {
109- $ content = $ result ->getContent ();
110- } catch (\Exception $ e ) {
111- $ content = null ;
112- }
113- $ path = $ result ->getPath ();
114-
115- if ($ content !== null ) {
116- $ integrity = $ this ->integrityFactory ->create (
117- [
118- "data " => [
119- 'hash ' => $ this ->hashGenerator ->generate ($ content ),
120- 'path ' => $ path
121- ]
122- ]
123- );
124- $ this ->integrityCollector ->collect ($ integrity );
125- }
126- }
106+ if (PHP_SAPI === 'cli ' ) {
107+ $ this ->generateHash ($ result );
127108 }
128109
129110 return $ result ;
@@ -133,39 +114,50 @@ public function afterCreateRequireJsMixinsAsset(
133114 * Generates integrity for static JS asset.
134115 *
135116 * @param FileManager $subject
136- * @param File $result
117+ * @param File|false $result
137118 *
138119 * @return File
139120 *
140121 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
141122 */
142123 public function afterCreateStaticJsAsset (
143124 FileManager $ subject ,
144- File $ result
125+ File | false $ result
145126 ): File {
146- if (PHP_SAPI == 'cli ' ) {
147- if (in_array ($ result ->getContentType (), self ::CONTENT_TYPES )) {
148- try {
149- $ content = $ result ->getContent ();
150- } catch (\Exception $ e ) {
151- $ content = null ;
152- }
153- $ path = $ result ->getPath ();
154-
155- if ($ content !== null ) {
156- $ integrity = $ this ->integrityFactory ->create (
157- [
158- "data " => [
159- 'hash ' => $ this ->hashGenerator ->generate ($ content ),
160- 'path ' => $ path
161- ]
162- ]
163- );
164- $ this ->integrityCollector ->collect ($ integrity );
165- }
166- }
127+ if ($ result !== false && PHP_SAPI === 'cli ' ) {
128+ $ this ->generateHash ($ result );
167129 }
168130
169131 return $ result ;
170132 }
133+
134+ /**
135+ * Generates hash for the given file result if it matches the supported content types.
136+ *
137+ * @param File $result
138+ * @return void
139+ */
140+ private function generateHash (File $ result ): void
141+ {
142+ if (in_array ($ result ->getContentType (), self ::CONTENT_TYPES )) {
143+ try {
144+ $ content = $ result ->getContent ();
145+ } catch (\Exception $ e ) {
146+ $ content = null ;
147+ }
148+ $ path = $ result ->getPath ();
149+
150+ if ($ content !== null ) {
151+ $ integrity = $ this ->integrityFactory ->create (
152+ [
153+ "data " => [
154+ 'hash ' => $ this ->hashGenerator ->generate ($ content ),
155+ 'path ' => $ path
156+ ]
157+ ]
158+ );
159+ $ this ->integrityCollector ->collect ($ integrity );
160+ }
161+ }
162+ }
171163}
0 commit comments