-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-file-parser.php
153 lines (128 loc) · 3.61 KB
/
class-file-parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace keesiemeijer\WP_Plugin_Parser;
class File_Parser {
/**
* Log.
*
* @var array
*/
public $logger;
/**
* Settings to parse for files.
*
* @var array
*/
private $settings;
/**
* PHP file path's from a directory.
*
* @var array
*/
private $files;
public function __construct() {
$this->parse_init();
}
/**
* Set up properties and get the PHP file path's from a directory.
*
* @param array $settings Settings.
*/
public function parse_init() {
$this->settings = array(
'root' => '',
'exclude_dirs' => array(),
'exclude_strict' => false,
);
$this->logger = new Logger();
$this->files = null;
}
/**
* Public function to get the PHP file path's of a directory.
*
* @return array Array with PHP file path's.
*/
public function get_files() {
return $this->files ? $this->files : array();
}
/**
* Get al the PHP file path's from a directory.
*
* Sets the class properties files and path.
*
* @return false|void Returns false if files are not found.
*/
public function parse( $settings ) {
$settings = is_array( $settings ) ? $settings : array();
$this->settings = array_merge( $this->settings, (array) $settings );
if ( ! $this->settings['root'] ) {
$this->logger->log( __( "Root directory not found", 'wp-plugin-parser' ) );
return false;
}
$path = $this->settings['root'];
$is_file = is_file( $path );
if ( ! is_readable( $path ) ) {
if ( $is_file ) {
$this->logger->log( sprintf( __( 'Could not read file %s', 'wp-plugin-parser' ), '<code>' . $path . '</code>' ) );
} else {
$this->logger->log( sprintf( __( 'Could not read directory %s', 'wp-plugin-parser' ), '<code>' . $path . '</code>' ) );
}
return false;
}
$files = $is_file ? array( $path ) : $this->itarate_files( $path );
if ( $files instanceof \WP_Error ) {
$this->logger->log( $files->get_error_message() );
return false;
}
$this->files = $files;
}
/**
* Get all PHP files in a directory.
*
* @param string $path Path to root directory.
* @param array $settings Settings.
* @return array Array with PHP file path's/
*/
private function itarate_files( $path ) {
$settings = $this->settings;
$files = array();
$path = trailingslashit( $path );
$dirIterator = new \RecursiveDirectoryIterator( $path );
// Filter the excluded directories out.
$filterIterator = new \RecursiveCallbackFilterIterator( $dirIterator, function ( $current, $key, $iterator ) use ( $path, $settings ) {
if ( $current->isFile() && ( 'php' !== $current->getExtension() ) ) {
return false;
}
if ( ! $current->isDir() ) {
return true;
}
// Exclude directories if found in path
$directory_name = $current->getFilename();
if ( $settings['exclude_strict'] && in_array( $directory_name, $settings['exclude_dirs'] ) ) {
return false;
}
// Exclude directories found in the root directory.
$current_path = $current->getPathname();
foreach ( $settings['exclude_dirs'] as $dir ) {
if ( ( $path . untrailingslashit( $dir ) ) === $current_path ) {
return false;
}
}
return true;
} );
$iterableFiles = new \RecursiveIteratorIterator( $filterIterator );
try {
foreach ( $iterableFiles as $file ) {
if ( 'php' !== $file->getExtension() ) {
continue;
}
$files[] = $file->getPathname();
}
} catch ( \UnexpectedValueException $exc ) {
return new \WP_Error(
'unexpected_value_exception',
sprintf( 'Directory [%s] contained a directory we can not recurse into', $path )
);
}
return $files;
}
}