-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathChecksum_Base_Command.php
More file actions
98 lines (90 loc) · 2.34 KB
/
Checksum_Base_Command.php
File metadata and controls
98 lines (90 loc) · 2.34 KB
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
<?php
use WP_CLI\Utils;
/**
* Base command that all checksum commands rely on.
*
* @package wp-cli
*/
class Checksum_Base_Command extends WP_CLI_Command {
/**
* Normalizes directory separators to slashes.
*
* @param string $path Path to convert.
*
* @return string Path with all backslashes replaced by slashes.
*/
public static function normalize_directory_separators( $path ) {
return str_replace( '\\', '/', $path );
}
/**
* Read a remote file and return its contents.
*
* @param string $url URL of the remote file to read.
*
* @return mixed
*/
protected static function _read( $url ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Could be used in classes extending this class.
$headers = array( 'Accept' => 'application/json' );
/**
* @var \WpOrg\Requests\Response $response
*/
$response = Utils\http_request(
'GET',
$url,
null,
$headers,
array( 'timeout' => 30 )
);
if ( 200 === $response->status_code ) {
return $response->body;
}
WP_CLI::error( "Couldn't fetch response from {$url} (HTTP code {$response->status_code})." );
}
/**
* Recursively get the list of files for a given path.
*
* @param string $path Root path to start the recursive traversal in.
*
* @return array<string>
*/
protected function get_files( $path ) {
$filtered_files = array();
try {
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$path,
RecursiveDirectoryIterator::SKIP_DOTS
),
function ( $current ) use ( $path ) {
return $this->filter_file( self::normalize_directory_separators( substr( $current->getPathname(), strlen( $path ) ) ) );
}
),
RecursiveIteratorIterator::CHILD_FIRST
);
/**
* @var \SplFileInfo $file_info
*/
foreach ( $files as $file_info ) {
if ( $file_info->isFile() ) {
$filtered_files[] = self::normalize_directory_separators( substr( $file_info->getPathname(), strlen( $path ) ) );
}
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
return $filtered_files;
}
/**
* Whether to include the file in the verification or not.
*
* Can be overridden in subclasses.
*
* @param string $filepath Path to a file.
*
* @return bool
*/
protected function filter_file( $filepath ) {
return true;
}
}