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