7
7
namespace Magento \Catalog \Model \View \Asset ;
8
8
9
9
use Magento \Framework \App \Config \ScopeConfigInterface ;
10
+ use Magento \Framework \App \Filesystem \DirectoryList ;
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Filesystem ;
13
+ use Magento \Framework \Filesystem \DriverPool ;
10
14
use Magento \Framework \View \Asset \ContextInterface ;
11
15
use Magento \Framework \View \Asset \File \NotFoundException ;
12
16
use Magento \Framework \View \Asset \LocalInterface ;
13
17
use Magento \Framework \View \Asset \Repository ;
18
+ use Magento \Catalog \Model \Product \Media \ConfigInterface ;
14
19
15
20
/**
16
21
* A locally available image placeholder file asset that can be referred with a file type
@@ -53,28 +58,49 @@ class Placeholder implements LocalInterface
53
58
*/
54
59
private $ scopeConfig ;
55
60
61
+ /**
62
+ * @var \Magento\Framework\Filesystem\Directory\WriteInterface
63
+ */
64
+ private $ directoryMedia ;
65
+
66
+ /**
67
+ * @var ConfigInterface
68
+ */
69
+ private $ mediaConfig ;
70
+
56
71
/**
57
72
* Placeholder constructor.
58
73
*
59
74
* @param ContextInterface $context
60
75
* @param ScopeConfigInterface $scopeConfig
61
76
* @param Repository $assetRepo
62
77
* @param string $type
78
+ * @param Filesystem|null $filesystem
79
+ * @param ConfigInterface|null $mediaConfig
80
+ *
63
81
*/
64
82
public function __construct (
65
83
ContextInterface $ context ,
66
84
ScopeConfigInterface $ scopeConfig ,
67
85
Repository $ assetRepo ,
68
- $ type
86
+ $ type ,
87
+ ?Filesystem $ filesystem = null ,
88
+ ?ConfigInterface $ mediaConfig = null
69
89
) {
70
90
$ this ->context = $ context ;
71
91
$ this ->scopeConfig = $ scopeConfig ;
72
92
$ this ->assetRepo = $ assetRepo ;
73
93
$ this ->type = $ type ;
94
+ $ filesystem = $ filesystem ?? ObjectManager::getInstance ()->get (Filesystem::class);
95
+ $ this ->mediaConfig = $ mediaConfig ?? ObjectManager::getInstance ()->get (ConfigInterface::class);
96
+ $ this ->directoryMedia = $ filesystem ->getDirectoryWrite (
97
+ DirectoryList::MEDIA ,
98
+ DriverPool::FILE
99
+ );
74
100
}
75
101
76
102
/**
77
- * { @inheritdoc}
103
+ * @inheritdoc
78
104
*/
79
105
public function getUrl ()
80
106
{
@@ -88,20 +114,20 @@ public function getUrl()
88
114
}
89
115
90
116
/**
91
- * { @inheritdoc}
117
+ * @inheritdoc
92
118
*/
93
119
public function getContentType ()
94
120
{
95
121
return $ this ->contentType ;
96
122
}
97
123
98
124
/**
99
- * { @inheritdoc}
125
+ * @inheritdoc
100
126
*/
101
127
public function getPath ()
102
128
{
103
129
if ($ this ->getFilePath () !== null ) {
104
- $ result = $ this ->getContext ()-> getPath ()
130
+ $ result = $ this ->getLocalMediaPath ()
105
131
. DIRECTORY_SEPARATOR . $ this ->getModule ()
106
132
. DIRECTORY_SEPARATOR . $ this ->getFilePath ();
107
133
} else {
@@ -119,7 +145,35 @@ public function getPath()
119
145
}
120
146
121
147
/**
122
- * {@inheritdoc}
148
+ * Get path for local media
149
+ *
150
+ * @return string
151
+ */
152
+ private function getLocalMediaPath ()
153
+ {
154
+ return $ this ->directoryMedia ->getAbsolutePath ($ this ->mediaConfig ->getBaseMediaPath ());
155
+ }
156
+
157
+ /**
158
+ * Get relative placeholder path
159
+ *
160
+ * @return string|null
161
+ */
162
+ public function getRelativePath ()
163
+ {
164
+ $ result = null ;
165
+ //will use system placeholder unless another specified in the config
166
+ if ($ this ->getFilePath () !== null ) {
167
+ $ result = DIRECTORY_SEPARATOR . DirectoryList::MEDIA
168
+ . DIRECTORY_SEPARATOR . $ this ->directoryMedia ->getRelativePath ($ this ->mediaConfig ->getBaseMediaPath ())
169
+ . DIRECTORY_SEPARATOR . $ this ->getModule ()
170
+ . DIRECTORY_SEPARATOR . $ this ->getFilePath ();
171
+ }
172
+ return $ result ;
173
+ }
174
+
175
+ /**
176
+ * @inheritdoc
123
177
*/
124
178
public function getSourceFile ()
125
179
{
@@ -137,15 +191,15 @@ public function getSourceContentType()
137
191
}
138
192
139
193
/**
140
- * { @inheritdoc}
194
+ * @inheritdoc
141
195
*/
142
196
public function getContent ()
143
197
{
144
198
return null ;
145
199
}
146
200
147
201
/**
148
- * { @inheritdoc}
202
+ * @inheritdoc
149
203
*/
150
204
public function getFilePath ()
151
205
{
@@ -163,7 +217,8 @@ public function getFilePath()
163
217
}
164
218
165
219
/**
166
- * {@inheritdoc}
220
+ * @inheritdoc
221
+ *
167
222
* @return ContextInterface
168
223
*/
169
224
public function getContext ()
@@ -172,7 +227,7 @@ public function getContext()
172
227
}
173
228
174
229
/**
175
- * { @inheritdoc}
230
+ * @inheritdoc
176
231
*/
177
232
public function getModule ()
178
233
{
0 commit comments