@@ -185,70 +185,94 @@ public function __construct(
185
185
}
186
186
187
187
/**
188
- * Return one-level child directories for specified path
188
+ * Create sub directories if DB storage is used
189
189
*
190
- * @param string $path Parent directory path
191
- * @return \Magento\Framework\Data\Collection\Filesystem
192
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
193
- * @SuppressWarnings(PHPMD.NPathComplexity)
190
+ * @param string $path
191
+ * @return void
194
192
*/
195
- public function getDirsCollection ($ path )
193
+ protected function createSubDirectories ($ path )
196
194
{
197
195
if ($ this ->_coreFileStorageDb ->checkDbUsage ()) {
198
196
/** @var \Magento\MediaStorage\Model\File\Storage\Directory\Database $subDirectories */
199
197
$ subDirectories = $ this ->_directoryDatabaseFactory ->create ();
200
- $ subDirectories ->getSubdirectories ($ path );
201
- foreach ($ subDirectories as $ directory ) {
198
+ $ directories = $ subDirectories ->getSubdirectories ($ path );
199
+ foreach ($ directories as $ directory ) {
202
200
$ fullPath = rtrim ($ path , '/ ' ) . '/ ' . $ directory ['name ' ];
203
201
$ this ->_directory ->create ($ fullPath );
204
202
}
205
203
}
204
+ }
206
205
206
+ /**
207
+ * Prepare and get conditions for exclude directories
208
+ *
209
+ * @return array
210
+ */
211
+ protected function getConditionsForExcludeDirs ()
212
+ {
207
213
$ conditions = ['reg_exp ' => [], 'plain ' => []];
208
214
209
215
if ($ this ->_dirs ['exclude ' ]) {
210
216
foreach ($ this ->_dirs ['exclude ' ] as $ dir ) {
211
- $ conditions [$ dir-> getAttribute ( 'regexp ' ) ? 'reg_exp ' : 'plain ' ][$ dir ] = true ;
217
+ $ conditions [! empty ( $ dir[ 'regexp ' ] ) ? 'reg_exp ' : 'plain ' ][$ dir[ ' name ' ] ] = true ;
212
218
}
213
219
}
214
220
215
221
// "include" section takes precedence and can revoke directory exclusion
216
222
if ($ this ->_dirs ['include ' ]) {
217
223
foreach ($ this ->_dirs ['include ' ] as $ dir ) {
218
- unset($ conditions ['regexp ' ][( string ) $ dir] , $ conditions ['plain ' ][$ dir ]);
224
+ unset($ conditions ['reg_exp ' ][$ dir[ ' name ' ]] , $ conditions ['plain ' ][$ dir[ ' name ' ] ]);
219
225
}
220
226
}
221
227
228
+ return $ conditions ;
229
+ }
230
+
231
+ /**
232
+ * Remove excluded directories from collection
233
+ *
234
+ * @param \Magento\Framework\Data\Collection\Filesystem $collection
235
+ * @param array $conditions
236
+ * @return \Magento\Framework\Data\Collection\Filesystem
237
+ */
238
+ protected function removeItemFromCollection ($ collection , $ conditions )
239
+ {
222
240
$ regExp = $ conditions ['reg_exp ' ] ? '~ ' . implode ('| ' , array_keys ($ conditions ['reg_exp ' ])) . '~i ' : null ;
223
- $ collection = $ this ->getCollection (
224
- $ path
225
- )->setCollectDirs (
226
- true
227
- )->setCollectFiles (
228
- false
229
- )->setCollectRecursively (
230
- false
231
- );
232
241
$ storageRootLength = strlen ($ this ->_cmsWysiwygImages ->getStorageRoot ());
233
242
234
243
foreach ($ collection as $ key => $ value ) {
235
244
$ rootChildParts = explode ('/ ' , substr ($ value ->getFilename (), $ storageRootLength ));
236
245
237
- if (array_key_exists (
238
- $ rootChildParts [0 ],
239
- $ conditions ['plain ' ]
240
- ) || $ regExp && preg_match (
241
- $ regExp ,
242
- $ value ->getFilename ()
243
- )
244
- ) {
246
+ if (array_key_exists ($ rootChildParts [1 ], $ conditions ['plain ' ])
247
+ || ($ regExp && preg_match ($ regExp , $ value ->getFilename ()))) {
248
+
245
249
$ collection ->removeItemByKey ($ key );
246
250
}
247
251
}
248
252
249
253
return $ collection ;
250
254
}
251
255
256
+ /**
257
+ * Return one-level child directories for specified path
258
+ *
259
+ * @param string $path Parent directory path
260
+ * @return \Magento\Framework\Data\Collection\Filesystem
261
+ */
262
+ public function getDirsCollection ($ path )
263
+ {
264
+ $ this ->createSubDirectories ($ path );
265
+
266
+ $ collection = $ this ->getCollection ($ path )
267
+ ->setCollectDirs (true )
268
+ ->setCollectFiles (false )
269
+ ->setCollectRecursively (false );
270
+
271
+ $ conditions = $ this ->getConditionsForExcludeDirs ();
272
+
273
+ return $ this ->removeItemFromCollection ($ collection , $ conditions );
274
+ }
275
+
252
276
/**
253
277
* Return files
254
278
*
0 commit comments