forked from wp-cli/wp-cli.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.php
233 lines (221 loc) · 6.83 KB
/
command.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace WP_CLI_Org;
use WP_CLI;
/**
* WP-CLI commands for generating the docs
*/
/**
* Generate the homepage from the repo's README.md
*
* @when before_wp_load
*/
function generate_homepage() {
$ret = trim( shell_exec( 'which wp' ) );
if ( empty( $ret ) ) {
WP_CLI::error( 'Could not find path to wp executable.' );
}
if ( 'link' === filetype( $ret ) ) {
$ret = readlink( $ret );
}
$readme_path = dirname( dirname( $ret ) ) . '/README.md';
if ( ! is_file( $readme_path ) ) {
WP_CLI::error( 'Could not find README.md in wp executable PATH. Please make sure wp executable points to git clone.' );
}
$contents = file_get_contents( $readme_path );
$search = <<<EOT
WP-CLI
======
EOT;
$replace = <<<EOT
---
layout: default
title: Command line interface for WordPress
---
EOT;
$contents = str_replace( $search, $replace, $contents );
file_put_contents( dirname( __FILE__ ) . '/index.md', $contents );
WP_CLI::success( 'Updated index.md from project README.md.' );
}
WP_CLI::add_command( 'website generate-homepage', '\WP_CLI_Org\generate_homepage' );
/**
* Generate the contributing page from the repo's CONTRIBUTING.md
*
* @when before_wp_load
*/
function generate_contributing() {
$ret = trim( shell_exec( 'which wp' ) );
if ( empty( $ret ) ) {
WP_CLI::error( 'Could not find path to wp executable.' );
}
if ( 'link' === filetype( $ret ) ) {
$ret = readlink( $ret );
}
$contributing_path = dirname( dirname( $ret ) ) . '/CONTRIBUTING.md';
if ( ! is_file( $contributing_path ) ) {
WP_CLI::error( 'Could not find CONTRIBUTING.md in wp executable PATH. Please make sure wp executable points to git clone.' );
}
$contents = file_get_contents( $contributing_path );
$search = <<<EOT
Contributing
============
EOT;
$replace = <<<EOT
---
layout: doc
title: Contributing
category: Contributing
description: An introduction to the contributing process.
quick_links:
- Reporting a bug
- Creating a pull request
- Improving our documentation
---
EOT;
$contents = str_replace( $search, $replace, $contents );
file_put_contents( dirname( __FILE__ ) . '/docs/contributing/index.md', $contents );
WP_CLI::success( 'Updated docs/contributing/index.md from project CONTRIBUTING.md.' );
}
WP_CLI::add_command( 'website generate-contributing', '\WP_CLI_Org\generate_contributing' );
/**
* Dump the list of internal APIs, as JSON.
*
* Used to build user-facing docs of public APIs.
*
* @subcommand api-dump
*/
function api_dump() {
$apis = array();
$functions = get_defined_functions();
foreach( $functions['user'] as $function ) {
$reflection = new \ReflectionFunction( $function );
$phpdoc = $reflection->getDocComment();
if ( false === stripos( $phpdoc, '@access public' ) ) {
continue;
}
$apis[] = get_simple_representation( $reflection );
}
$classes = get_declared_classes();
foreach( $classes as $class ) {
if ( false === stripos( $class, 'WP_CLI' ) ) {
continue;
}
$reflection = new \ReflectionClass( $class );
foreach( $reflection->getMethods() as $method ) {
$method_reflection = new \ReflectionMethod( $method->class, $method->name );
$phpdoc = $method_reflection->getDocComment();
if ( false === stripos( $phpdoc, '@access public' ) ) {
continue;
}
$apis[] = get_simple_representation( $method_reflection );
}
}
echo json_encode( $apis );
}
\WP_CLI::add_command( 'cli api-dump', '\WP_CLI_Org\api_dump' );
/**
* Get a simple representation of a function or method
*
* @param Reflection
* @return array
*/
function get_simple_representation( $reflection ) {
$signature = $reflection->getName();
$parameters = array();
foreach( $reflection->getParameters() as $parameter ) {
$parameter_signature = '$' . $parameter->getName();
if ( $parameter->isOptional() ) {
$default_value = $parameter->getDefaultValue();
if ( false === $default_value ) {
$parameter_signature .= ' = false';
} else if ( array() === $default_value ) {
$parameter_signature .= ' = array()';
} else if ( '' === $default_value ) {
$parameter_signature .= " = ''";
} else if ( null === $default_value ) {
$parameter_signature .= ' = null';
} else if ( true === $default_value ) {
$parameter_signature .= ' = true';
} else {
$parameter_signature .= ' = ' . $default_value;
}
}
$parameters[] = $parameter_signature;
}
if ( ! empty( $parameters ) ) {
$signature = $signature . '( ' . implode( ', ', $parameters ) . ' )';
} else {
$signature = $signature . '()';
}
$phpdoc = $reflection->getDocComment();
$type = strtolower( str_replace( 'Reflection', '', get_class( $reflection ) ) );
$class = '';
switch ( $type ) {
case 'function':
$full_name = $reflection->getName();
break;
case 'method':
$separator = $reflection->isStatic() ? '::' : '->';
$class = $reflection->class;
$full_name = $class . $separator . $reflection->getName();
$signature = $class . $separator . $signature;
break;
}
return array(
'phpdoc' => parse_docblock( $phpdoc ),
'type' => $type,
'signature' => $signature,
'short_name' => $reflection->getShortName(),
'full_name' => $full_name,
'class' => $class,
);
}
/**
* Parse PHPDoc into a structured representation
*/
function parse_docblock( $docblock ) {
$ret = array(
'description' => '',
'parameters' => array(),
);
$extra_line = '';
$in_param = false;
foreach( preg_split("/(\r?\n)/", $docblock ) as $line ){
if ( preg_match('/^(?=\s+?\*[^\/])(.+)/', $line, $matches ) ) {
$info = trim( $matches[1] );
$info = preg_replace( '/^(\*\s+?)/', '', $info );
if ( $in_param ) {
list( $param, $key ) = $in_param;
$ret['parameters'][ $param_name ][ $key ][2] .= PHP_EOL . $info;
if ( '}' === substr( $info, -1 ) ) {
$in_param = false;
}
} else if ( $info[0] !== "@" ) {
$ret['description'] .= PHP_EOL . "{$extra_line}{$info}";
} else {
preg_match( '/@(\w+)/', $info, $matches );
$param_name = $matches[1];
$value = str_replace( "@$param_name ", '', $info );
if ( ! isset( $ret['parameters'][ $param_name ] ) ) {
$ret['parameters'][ $param_name ] = array();
}
$ret['parameters'][ $param_name ][] = preg_split( '/[\s]+/', $value, 3 );
end( $ret['parameters'][ $param_name ] );
$key = key( $ret['parameters'][ $param_name ] );
reset( $ret['parameters'][ $param_name ] );
if ( ! empty( $ret['parameters'][ $param_name ][ $key ][ 2 ] )
&& '{' === substr( $ret['parameters'][ $param_name ][ $key ][ 2 ] , -1 ) ) {
$in_param = array( $param_name, $key );
}
}
$extra_line = '';
} else {
$extra_line .= PHP_EOL;
}
}
$ret['description'] = str_replace( '\/', '/', trim( $ret['description'], PHP_EOL ) );
$bits = explode( PHP_EOL, $ret['description'] );
$ret['short_description'] = array_shift( $bits );
$long_description = trim( implode( PHP_EOL, $bits ), PHP_EOL );
$ret['long_description'] = $long_description;
return $ret;
}