-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathCommands.php
183 lines (152 loc) · 6.63 KB
/
Commands.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
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
<?php
namespace Aivec\Plugins\DocParser\API;
use Aivec\Plugins\DocParser\CLI\CliErrorException;
use Aivec\Plugins\DocParser\CLI\Logger;
use Aivec\Plugins\DocParser\Importer\Importer;
use Aivec\Plugins\DocParser\Importer\Parser;
use Aivec\Plugins\DocParser\Models\ImportConfig;
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
// phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
/**
* Converts PHPDoc markup into a template ready for import to a WordPress blog.
*/
class Commands
{
/**
* Generate JSON containing the PHPDoc markup, convert it into WordPress posts, and insert into DB.
*
* @param string $directory
* @param bool $trashOldRefs
* @param null|bool $quick
* @param null|bool $importInternal
* @throws CliErrorException Thrown when an error occurs.
* @return void
*/
public function create($directory, $trashOldRefs = false, $quick = null, $importInternal = null) {
if (empty($directory)) {
throw new CliErrorException(sprintf("Can't read %1\$s. Does the file exist?", $directory), 1);
}
do_action('avcpdp_command_print_line', '');
$parser_meta_filen = 'docparser.config.json';
$parser_meta_filep = '';
if ($directory === '.') {
$parser_meta_filep = "./{$parser_meta_filen}";
} else {
$parser_meta_filep = "{$directory}/{$parser_meta_filen}";
}
do_action('avcpdp_command_print_line', sprintf('Getting source meta data from %1$s', $parser_meta_filep));
if (!file_exists($parser_meta_filep)) {
throw new CliErrorException(sprintf('Missing required file: %1$s', $parser_meta_filep), 1);
}
$metaf = file_get_contents($parser_meta_filep);
if (empty($metaf)) {
throw new CliErrorException(sprintf("Can't read %1\$s. Possible permissions error.", $parser_meta_filep), 1);
}
$parser_meta = json_decode($metaf, true);
if ($parser_meta === null) {
throw new CliErrorException(sprintf(
'%1$s is malformed. Make sure the file is in proper JSON format.',
$parser_meta_filen
), 1);
}
$types = ['plugin', 'theme', 'composer-packages'];
$validtypesm = 'Valid types are "plugin", "theme", and "composer-package"';
if (empty($parser_meta['type'])) {
throw new CliErrorException("The \"type\" key is missing.\r\n{$validtypesm}", 1);
}
if (!in_array($parser_meta['type'], $types, true)) {
throw new CliErrorException($validtypesm, 1);
}
if (empty($parser_meta['name'])) {
throw new CliErrorException('The "name" key is missing or contains an empty value.', 1);
}
if (!is_string($parser_meta['name'])) {
throw new CliErrorException('"name" must be a string.', 1);
}
if (isset($parser_meta['exclude'])) {
if (!is_array($parser_meta['exclude'])) {
throw new CliErrorException('"exclude" must be an array of strings.', 1);
}
foreach ($parser_meta['exclude'] as $target) {
if (!is_string($target)) {
throw new CliErrorException('"exclude" must be an array of strings.', 1);
}
}
}
if (isset($parser_meta['excludeStrict'])) {
if (!is_bool($parser_meta['excludeStrict'])) {
throw new CliErrorException('"excludeStrict" must be a boolean.', 1);
}
}
if (isset($parser_meta['version'])) {
if (!is_string($parser_meta['version'])) {
throw new CliErrorException('"version" must be a string.', 1);
}
}
// handle file/folder exclusions
$exclude = !empty($parser_meta['exclude']) ? $parser_meta['exclude'] : [];
add_filter('wp_parser_exclude_directories', function () use ($exclude) {
return $exclude;
});
$exclude_strict = isset($parser_meta['excludeStrict']) ? (bool)$parser_meta['excludeStrict'] : false;
add_filter('wp_parser_exclude_directories_strict', function () use ($exclude_strict) {
return $exclude_strict;
});
$version = isset($parser_meta['version']) ? $parser_meta['version'] : null;
$data = $this->_get_phpdoc_data($directory, 'array');
$data = [
'config' => new ImportConfig(
$parser_meta['type'],
$parser_meta['name'],
$version,
$exclude,
$exclude_strict
),
'trash_old_refs' => $trashOldRefs,
'files' => $data,
];
// Import data
$this->_do_import($data, isset($quick), isset($importInternal));
}
/**
* 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].
* @throws CliErrorException Thrown when an error occurs.
* @return string|array
*/
protected function _get_phpdoc_data($path, $format = 'json') {
do_action('avcpdp_command_print_line', sprintf('Extracting PHPDoc from %1$s. This may take a few minutes...', $path));
$is_file = is_file($path);
$files = $is_file ? [$path] : Parser::getWpFiles($path);
$path = $is_file ? dirname($path) : $path;
if ($files instanceof \WP_Error) {
throw new CliErrorException(sprintf('Problem with %1$s: %2$s', $path, $files->get_error_message()), 1);
}
$output = Parser::parseFiles($files, $path);
if ('json' == $format) {
return json_encode($output, JSON_PRETTY_PRINT);
}
return $output;
}
/**
* Import the PHPDoc $data into WordPress posts and taxonomies
*
* @param array $data
* @param bool $skip_sleep If true, the sleep() calls are skipped.
* @param bool $import_ignored If true, functions marked `@ignore` will be imported.
* @throws CliErrorException Thrown when an error occurs.
* @return void
*/
protected function _do_import(array $data, $skip_sleep = false, $import_ignored = false) {
if (!wp_get_current_user()->exists()) {
throw new CliErrorException('Please specify a valid user: --user=<id|login>', 1);
}
// Run the importer
$importer = new Importer($data['config'], $data['trash_old_refs']);
$importer->setLogger(new Logger());
$importer->import($data['files'], $skip_sleep, $import_ignored);
do_action('avcpdp_command_print_line', '');
}
}