|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\services\locators; |
|
3
|
|
|
|
|
4
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
5
|
|
|
use FilesystemIterator; |
|
6
|
|
|
use GlobIterator; |
|
7
|
|
|
|
|
8
|
|
|
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
9
|
|
|
exit( 'No direct script access allowed' ); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class FileLocator |
|
16
|
|
|
* |
|
17
|
|
|
* finds filepaths from folder paths or FQCNs (Fully Qualified Class Name) from partial FQCNs |
|
18
|
|
|
* |
|
19
|
|
|
* @package Event Espresso |
|
20
|
|
|
* @author Brent Christensen |
|
21
|
|
|
* @since 4.9.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class FileLocator extends Locator { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string $file_mask |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $file_mask = '*.php'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array $filepaths |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filepaths = array(); |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $file_mask |
|
39
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setFileMask( $file_mask ) { |
|
42
|
|
|
if ( ! is_string( $file_mask ) ) { |
|
43
|
|
|
throw new InvalidDataTypeException( '$file_mask', $file_mask, 'string' ); |
|
44
|
|
|
} |
|
45
|
|
|
$this->file_mask = $file_mask; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @access public |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getFilePaths() { |
|
55
|
|
|
return $this->filepaths; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @access public |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
|
|
public function count() { |
|
66
|
|
|
return count( $this->filepaths ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* given a path to a valid directory, or an array of valid paths, |
|
73
|
|
|
* will find all files that match the provided mask |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
* @param array|string $directory_paths |
|
77
|
|
|
* @return \FilesystemIterator |
|
78
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function locate( $directory_paths ) { |
|
|
|
|
|
|
81
|
|
|
if ( ! ( is_string( $directory_paths ) || is_array( $directory_paths ) ) ) { |
|
82
|
|
|
throw new InvalidDataTypeException( '$directory_paths', $directory_paths, 'string or array' ); |
|
83
|
|
|
} |
|
84
|
|
|
foreach ( (array) $directory_paths as $directory_path ) { |
|
85
|
|
|
foreach ( $this->findFilesByPath( $directory_path ) as $key => $file ) { |
|
86
|
|
|
$this->filepaths[ $key ] = \EEH_File::standardise_directory_separators( $file ); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return $this->filepaths; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* given a path to a valid directory, will find all files that match the provided mask |
|
96
|
|
|
* |
|
97
|
|
|
* @access protected |
|
98
|
|
|
* @param string $directory_path |
|
99
|
|
|
* @return \FilesystemIterator |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function findFilesByPath( $directory_path = '' ) { |
|
102
|
|
|
$iterator = new GlobIterator ( |
|
103
|
|
|
\EEH_File::end_with_directory_separator( $directory_path ) . $this->file_mask |
|
104
|
|
|
); |
|
105
|
|
|
foreach ( $this->flags as $flag ) { |
|
106
|
|
|
$iterator->setFlags( $flag ); |
|
107
|
|
|
} |
|
108
|
|
|
return $iterator; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
// End of file FileLocator.php |
|
115
|
|
|
// Location: /FileLocator.php |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.