1
1
<?php
2
- /************************************************************************
3
- *
4
- * Copyright 2024 Adobe
2
+ /**
3
+ * Copyright 2025 Adobe
5
4
* All Rights Reserved.
6
- *
7
- * NOTICE: All information contained herein is, and remains
8
- * the property of Adobe and its suppliers, if any. The intellectual
9
- * and technical concepts contained herein are proprietary to Adobe
10
- * and its suppliers and are protected by all applicable intellectual
11
- * property laws, including trade secret and copyright laws.
12
- * Dissemination of this information or reproduction of this material
13
- * is strictly forbidden unless prior written permission is obtained
14
- * from Adobe.
15
- * ************************************************************************
16
5
*/
17
6
declare (strict_types=1 );
18
7
19
8
namespace Magento \Csp \Model \SubresourceIntegrity \Storage ;
20
9
21
- use Magento \ Deploy \ Package \ Package ;
22
- use Magento \Framework \App \ Area ;
10
+ use Psr \ Log \ LoggerInterface ;
11
+ use Magento \Framework \Filesystem ;
23
12
use Magento \Framework \App \Filesystem \DirectoryList ;
24
13
use Magento \Framework \Exception \FileSystemException ;
25
- use Magento \Framework \ Filesystem ;
14
+ use Magento \Csp \ Model \ SubresourceIntegrity \ StorageInterface ;
26
15
27
16
/**
28
- * Persistence of sri hashes in the local file system
17
+ * Filesystem based SRI hashed storage.
29
18
*/
30
- class File
19
+ class File implements StorageInterface
31
20
{
32
21
/**
33
- * Constant for sri hashes filename
22
+ * Name of a storage file.
23
+ *
24
+ * @var string
34
25
*/
35
26
private const FILENAME = 'sri-hashes.json ' ;
36
27
@@ -40,71 +31,95 @@ class File
40
31
private Filesystem $ filesystem ;
41
32
42
33
/**
43
- * Constructor
44
- *
34
+ * @var LoggerInterface
35
+ */
36
+ private LoggerInterface $ logger ;
37
+
38
+ /**
45
39
* @param Filesystem $filesystem
40
+ * @param LoggerInterface $logger
46
41
*/
47
42
public function __construct (
48
- Filesystem $ filesystem
43
+ Filesystem $ filesystem ,
44
+ LoggerInterface $ logger
49
45
) {
50
46
$ this ->filesystem = $ filesystem ;
47
+ $ this ->logger = $ logger ;
51
48
}
52
49
53
50
/**
54
- * Load data from filesystem
55
- *
56
- * @param string|null $area
57
- * @return string|bool
58
- * @throws FileSystemException
51
+ * @inheritDoc
59
52
*/
60
- public function load (?string $ area = null ): string | bool
53
+ public function load (?string $ context ): ? string
61
54
{
62
- $ staticDir = $ this ->filesystem ->getDirectoryRead (DirectoryList::STATIC_VIEW );
55
+ try {
56
+ $ staticDir = $ this ->filesystem ->getDirectoryRead (
57
+ DirectoryList::STATIC_VIEW
58
+ );
63
59
64
- if ( $ area ) {
65
- $ path = $ area . DIRECTORY_SEPARATOR . self :: FILENAME ;
66
- if ($ staticDir ->isFile ($ path )) {
67
- return $ staticDir -> readFile ( $ path ) ;
60
+ $ path = $ this -> resolveFilePath ( $ context );
61
+
62
+ if (! $ staticDir ->isFile ($ path )) {
63
+ return null ;
68
64
}
65
+
66
+ return $ staticDir ->readFile ($ path );
67
+ } catch (FileSystemException $ exception ) {
68
+ $ this ->logger ->critical ($ exception );
69
+
70
+ return null ;
69
71
}
70
- return false ;
71
72
}
72
73
73
74
/**
74
- * Save File to Local Storage by area
75
- *
76
- * @param string $data
77
- * @param string|null $area
78
- * @return bool
79
- * @throws FileSystemException
75
+ * @inheritDoc
80
76
*/
81
- public function save (string $ data , ?string $ area = null ): bool
77
+ public function save (string $ data , ?string $ context ): bool
82
78
{
83
- $ staticDir = $ this ->filesystem ->getDirectoryWrite (DirectoryList::STATIC_VIEW );
79
+ try {
80
+ $ staticDir = $ this ->filesystem ->getDirectoryWrite (
81
+ DirectoryList::STATIC_VIEW
82
+ );
83
+
84
+ return (bool ) $ staticDir ->writeFile (
85
+ $ this ->resolveFilePath ($ context ),
86
+ $ data ,
87
+ 'w '
88
+ );
89
+ } catch (FileSystemException $ exception ) {
90
+ $ this ->logger ->critical ($ exception );
84
91
85
- if ($ area ) {
86
- $ path = $ area . DIRECTORY_SEPARATOR . self ::FILENAME ;
87
- return (bool )$ staticDir ->writeFile ($ path , $ data , 'w ' );
92
+ return false ;
88
93
}
89
- return false ;
90
94
}
91
95
92
96
/**
93
- * Delete all Sri Hashes files
94
- *
95
- * @throws FileSystemException
97
+ * @inheritDoc
96
98
*/
97
- public function remove (): bool
99
+ public function remove (? string $ context ): bool
98
100
{
99
- $ staticDir = $ this ->filesystem ->getDirectoryWrite (DirectoryList::STATIC_VIEW );
101
+ try {
102
+ $ staticDir = $ this ->filesystem ->getDirectoryWrite (
103
+ DirectoryList::STATIC_VIEW
104
+ );
100
105
101
- //delete all json files from all areas
102
- foreach ([Package::BASE_AREA , Area::AREA_FRONTEND , Area::AREA_ADMINHTML ] as $ area ) {
103
- $ path = $ area . DIRECTORY_SEPARATOR . self ::FILENAME ;
104
- if ($ staticDir ->isFile ($ path )) {
105
- $ staticDir ->delete ($ path );
106
- }
106
+ return $ staticDir ->delete ($ this ->resolveFilePath ($ context ));
107
+ } catch (FileSystemException $ exception ) {
108
+ $ this ->logger ->critical ($ exception );
109
+
110
+ return false ;
107
111
}
108
- return true ;
112
+ }
113
+
114
+ /**
115
+ * Resolves a storage file path for a given context.
116
+ *
117
+ * @param string|null $context
118
+ *
119
+ * @return string
120
+ */
121
+ private function resolveFilePath (?string $ context ): string
122
+ {
123
+ return ($ context ? $ context . DIRECTORY_SEPARATOR : '' ) . self ::FILENAME ;
109
124
}
110
125
}
0 commit comments