FileListStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
16 use wcmf\lib\util\StringUtil;
17 
18 /**
19  * FileListStrategy implements a list of key value pairs that is retrieved
20  * from an configuration section.
21  *
22  * Configuration examples:
23  * @code
24  * // .ini files
25  * {"type":"file","paths":["path/to/files"],"pattern":"\\\\.ini$"}
26  *
27  * // all files recursive
28  * {"type":"file","paths":["path/to/files/*"]}
29  *
30  * // multiple paths
31  * {"type":"file","paths":["path/to/files","path/to/files2/*"]}
32  * @endcode
33  *
34  * @author ingo herwig <ingo@wemove.com>
35  */
36 class FileListStrategy implements ListStrategy {
37 
38  /**
39  * @see ListStrategy::getList
40  * $options is an associative array with keys 'paths' and 'pattern' (optional)
41  */
42  public function getList($options, $valuePattern=null, $key=null, $language=null) {
43  if (!isset($options['paths']) || !is_array($options['paths'])) {
44  throw new ConfigurationException("No array 'paths' given in list options: "+StringUtil::getDump($options));
45  }
46  $paths = $options['paths'];
47  $pattern = $valuePattern ? $valuePattern : (isset($options['pattern']) ? '/'.$options['pattern'].'/' : '/./');
48 
49  $fileUtil = new FileUtil();
50  $list = [];
51  foreach ($paths as $path) {
52  $recursive = preg_match('/\/\*$/', $path);
53  if ($recursive) {
54  $path = preg_replace('/\*$/', '', $path);
55  }
56  // if multiple directories or recursive, we show the complete file path
57  $prependDirectory = sizeof($paths) > 1 || $recursive;
58 
59  $files = $fileUtil->getFiles($path, $pattern, $prependDirectory, $recursive);
60  foreach ($files as $file) {
61  if (!$key || $key == $file) {
62  $list[$file] = $file;
63  }
64  }
65  }
66  return $list;
67  }
68 
69  /**
70  * @see ListStrategy::isStatic
71  */
72  public function isStatic($options) {
73  return false;
74  }
75 }
76 ?>
getList($options, $valuePattern=null, $key=null, $language=null)
ListStrategy defines the interface for classes that retrieve value lists.
ConfigurationException signals an exception in the configuration.
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
FileListStrategy implements a list of key value pairs that is retrieved from an configuration section...