forked from WordPress/phpdoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-command.php
More file actions
188 lines (155 loc) · 5.15 KB
/
class-command.php
File metadata and controls
188 lines (155 loc) · 5.15 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace WP_Parser;
use Tightenco\Collect\Support\Collection;
use WP_CLI;
use WP_CLI_Command;
use WP_Error;
// Temporary toggle until we figure out what's going wrong with the plugins taxonomy.
const USE_PLUGIN_PREFIX = false;
/**
* Converts PHPDoc markup into a template ready for import to a WordPress blog.
*/
class Command extends WP_CLI_Command {
/**
* Generate a JSON file containing the PHPDoc markup, and save to filesystem.
*
* @synopsis <directory> [<output_file>] [--ignore_files]
*
* @param array $args The arguments to pass to the command.
* @param array $assoc_args The associated arguments to pass to the command.
*/
public function export( $args, $assoc_args ) {
$directory = realpath( $args[0] );
$output_file = empty( $args[1] ) ? 'phpdoc.json' : $args[1];
$ignore_files = $this->getIgnoreFiles( $assoc_args );
$json = $this->_get_phpdoc_data( $directory, 'json', $ignore_files );
$result = file_put_contents( $output_file, $json );
WP_CLI::line();
if ( $result === false ) {
WP_CLI::error( sprintf( 'Problem writing %1$s bytes of data to %2$s', strlen( $json ), $output_file ) );
exit;
}
WP_CLI::success( sprintf( 'Data exported to %1$s', $output_file ) );
WP_CLI::line();
}
/**
* Read a JSON file containing the PHPDoc markup, convert it into WordPress posts, and insert into DB.
*
* @synopsis <file> [--quick] [--import-internal]
*
* @param array $args The arguments to pass to the command.
* @param array $assoc_args The associated arguments to pass to the command.
*/
public function import( $args, $assoc_args ) {
list( $file ) = $args;
WP_CLI::line();
// Get the data from the <file>, and check it's valid.
$phpdoc = false;
if ( is_readable( $file ) ) {
$phpdoc = file_get_contents( $file );
}
if ( ! $phpdoc ) {
WP_CLI::error( sprintf( "Can't read %1\$s. Does the file exist?", $file ) );
exit;
}
$phpdoc = json_decode( $phpdoc, true );
if ( is_null( $phpdoc ) ) {
WP_CLI::error( sprintf( "JSON in %1\$s can't be decoded :(", $file ) );
exit;
}
// Import data
$this->_do_import( $phpdoc, isset( $assoc_args['import-internal'] ) );
}
/**
* Generate JSON containing the PHPDoc markup, convert it into WordPress posts, and insert into DB.
*
* @subcommand create
* @synopsis <directory> [--quick] [--import-internal] [--user] [--ignore_files]
*
* @param array $args The arguments to pass to the command.
* @param array $assoc_args The associated arguments to pass to the command.
*/
public function create( $args, $assoc_args ) {
list( $directory ) = $args;
$directory = realpath( $directory );
$ignore_files = $this->getIgnoreFiles( $assoc_args );
if ( empty( $directory ) ) {
WP_CLI::error( sprintf( "Can't read %1\$s. Does the file exist?", $directory ) );
exit;
}
WP_CLI::line();
$data = $this->_get_phpdoc_data( $directory, 'array', $ignore_files );
// Import data
$this->_do_import( $data, isset( $assoc_args['import-internal'] ) );
}
/**
* Generate the data from the PHPDoc markup.
*
* @param string $path Directory or file to scan for PHPDoc
* @param string $format What format the data is returned in: [json|array].
* @param array $ignore_files What files to ignore.
*
* @return string|array
*/
protected function _get_phpdoc_data( $path, $format = 'json', $ignore_files = [] ) {
$ignore_files = ! empty( $ignore_files ) ? $ignore_files : [
'vendor',
'vendor_prefixed',
'node_modules',
'integration-tests',
'tests',
'build',
'config',
'grunt',
'deploy_keys',
'js',
'languages',
'webpack',
'images',
'css',
];
WP_CLI::line( sprintf( 'Extracting PHPDoc from %1$s. This may take a few minutes...', $path ) );
// Collect the files.
$is_file = is_file( $path );
$files = $is_file ? [ $path ] : Utils::get_files( $path, $ignore_files );
$path = $is_file ? dirname( $path ) : $path;
if ( $files instanceof WP_Error ) {
WP_CLI::error( sprintf( 'Problem with %1$s: %2$s', $path, $files->get_error_message() ) );
exit;
}
// Loop through files and get plugin data.
$plugin_data = PluginFinder::find( $files );
$runner = new Runner( $path, $plugin_data );
$output = $runner->parse_files( $files );
if ( $format === 'json' ) {
return json_encode( $output, JSON_PRETTY_PRINT );
}
return $output;
}
/**
* Import the PHPDoc $data into WordPress posts and taxonomies
*
* @param Collection $data
* @param bool $import_ignored If true, functions marked `@ignore` will be imported.
*/
protected function _do_import( Collection $data, $import_ignored = false ) {
if ( ! wp_get_current_user()->exists() ) {
WP_CLI::error( 'Please specify a valid user: --user=<id|login>' );
exit;
}
// Run the importer
$importer = new Importer();
$importer->setLogger( new WP_CLI_Logger() );
$importer->import( $data, $import_ignored );
WP_CLI::line();
}
/**
* @param $assoc_args
*
* @return array
*/
protected function getIgnoreFiles( $assoc_args ): array {
$ignore_files = empty( $assoc_args['ignore_files'] ) ? [] : explode( ',', $assoc_args['ignore_files'] );
return $ignore_files;
}
}