Skip to content

Commit b70c9f6

Browse files
committed
Clean up command implementation
1 parent 647cd15 commit b70c9f6

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
use WP_CLI\Utils;
1212

1313

14-
define( 'WP_CLI_HANDBOOK_PATH', dirname( __DIR__ ) );
15-
16-
1714
/**
1815
* @when before_wp_load
1916
*/
20-
class Command {
17+
class Handbook_Command {
18+
/**
19+
* @return string
20+
*/
21+
protected static function get_handbook_path() {
22+
return dirname( __DIR__ );
23+
}
2124

2225
/**
2326
* Regenerates all doc pages.
@@ -111,7 +114,7 @@ public function gen_api_docs() {
111114

112115
EOT;
113116

114-
self::empty_dir( WP_CLI_HANDBOOK_PATH . '/internal-api/' );
117+
self::empty_dir( self::get_handbook_path() . '/internal-api/' );
115118

116119
foreach ( $categories as $name => $apis ) {
117120
$out .= '## ' . $name . PHP_EOL . PHP_EOL;
@@ -136,7 +139,7 @@ function ( $parameter ) {
136139
$api['has_related'] = ! empty( $api['related'] );
137140

138141
$api_doc = self::render( 'internal-api.mustache', $api );
139-
$path = WP_CLI_HANDBOOK_PATH . "/internal-api/{$api['api_slug']}.md";
142+
$path = self::get_handbook_path() . "/internal-api/{$api['api_slug']}.md";
140143
if ( ! is_dir( dirname( $path ) ) ) {
141144
mkdir( dirname( $path ) );
142145
}
@@ -145,7 +148,7 @@ function ( $parameter ) {
145148
$out .= PHP_EOL . PHP_EOL;
146149
}
147150

148-
file_put_contents( WP_CLI_HANDBOOK_PATH . '/internal-api.md', $out );
151+
file_put_contents( self::get_handbook_path() . '/internal-api.md', $out );
149152
WP_CLI::success( 'Generated internal-api/' );
150153
}
151154

@@ -188,7 +191,7 @@ public function gen_behat_docs() {
188191

189192
EOT;
190193

191-
self::empty_dir( WP_CLI_HANDBOOK_PATH . '/behat-steps/' );
194+
self::empty_dir( self::get_handbook_path() . '/behat-steps/' );
192195

193196
foreach ( $categories as $name => $apis ) {
194197
$out .= '## ' . $name . PHP_EOL . PHP_EOL;
@@ -213,7 +216,7 @@ function ( $parameter ) {
213216
$api['has_related'] = ! empty( $api['related'] );
214217

215218
$api_doc = self::render( 'behat-steps.mustache', $api );
216-
$path = WP_CLI_HANDBOOK_PATH . "/behat-steps/{$api['api_slug']}.md";
219+
$path = self::get_handbook_path() . "/behat-steps/{$api['api_slug']}.md";
217220
if ( ! is_dir( dirname( $path ) ) ) {
218221
mkdir( dirname( $path ) );
219222
}
@@ -222,7 +225,7 @@ function ( $parameter ) {
222225
$out .= PHP_EOL . PHP_EOL;
223226
}
224227

225-
file_put_contents( WP_CLI_HANDBOOK_PATH . '/behat-steps.md', $out );
228+
file_put_contents( self::get_handbook_path() . '/behat-steps.md', $out );
226229
WP_CLI::success( 'Generated behat-steps/' );
227230
}
228231

@@ -256,7 +259,7 @@ public function gen_commands( $args, $assoc_args ) {
256259
WP_CLI::error( sprintf( "Install non-bundled packages by running '%s' first.", 'bin/install_packages.sh' ) );
257260
}
258261

259-
self::empty_dir( WP_CLI_HANDBOOK_PATH . '/commands/' );
262+
self::empty_dir( self::get_handbook_path() . '/commands/' );
260263

261264
$wp = WP_CLI::runcommand(
262265
'cli cmd-dump',
@@ -347,9 +350,9 @@ private static function update_commands_data( $command, &$commands_data, $full )
347350
public function gen_commands_manifest() {
348351
$manifest = [];
349352
$paths = [
350-
WP_CLI_HANDBOOK_PATH . '/commands/*.md',
351-
WP_CLI_HANDBOOK_PATH . '/commands/*/*.md',
352-
WP_CLI_HANDBOOK_PATH . '/commands/*/*/*.md',
353+
self::get_handbook_path() . '/commands/*.md',
354+
self::get_handbook_path() . '/commands/*/*.md',
355+
self::get_handbook_path() . '/commands/*/*/*.md',
353356
];
354357
$commands_data = [];
355358
foreach ( WP_CLI::get_root_command()->get_subcommands() as $command ) {
@@ -358,7 +361,7 @@ public function gen_commands_manifest() {
358361
foreach ( $paths as $path ) {
359362
foreach ( glob( $path ) as $file ) {
360363
$slug = basename( $file, '.md' );
361-
$cmd_path = str_replace( [ WP_CLI_HANDBOOK_PATH . '/commands/', '.md' ], '', $file );
364+
$cmd_path = str_replace( [ self::get_handbook_path() . '/commands/', '.md' ], '', $file );
362365
$title = '';
363366
$contents = file_get_contents( $file );
364367
if ( preg_match( '/^#\swp\s(.+)/', $contents, $matches ) ) {
@@ -396,7 +399,7 @@ public function gen_commands_manifest() {
396399
}
397400
}
398401
}
399-
file_put_contents( WP_CLI_HANDBOOK_PATH . '/bin/commands-manifest.json', json_encode( $manifest, JSON_PRETTY_PRINT ) );
402+
file_put_contents( self::get_handbook_path() . '/bin/commands-manifest.json', json_encode( $manifest, JSON_PRETTY_PRINT ) );
400403
$count = count( $manifest );
401404
WP_CLI::success( "Generated bin/commands-manifest.json of {$count} commands" );
402405
}
@@ -421,7 +424,7 @@ public function gen_hb_manifest() {
421424

422425
$files = new \RecursiveIteratorIterator(
423426
new \RecursiveCallbackFilterIterator(
424-
new \RecursiveDirectoryIterator( WP_CLI_HANDBOOK_PATH, \RecursiveDirectoryIterator::SKIP_DOTS ),
427+
new \RecursiveDirectoryIterator( self::get_handbook_path(), \RecursiveDirectoryIterator::SKIP_DOTS ),
425428
static function ( $file ) use ( $ignored_dirs ) {
426429
/** @var \SplFileInfo $file */
427430

@@ -448,7 +451,7 @@ static function ( $file ) use ( $ignored_dirs ) {
448451
continue;
449452
}
450453

451-
$rel_path = str_replace( WP_CLI_HANDBOOK_PATH . '/', '', $file->getPathname() );
454+
$rel_path = str_replace( self::get_handbook_path() . '/', '', $file->getPathname() );
452455

453456
$path = explode( '/', $rel_path );
454457
array_pop( $path );
@@ -477,7 +480,7 @@ static function ( $file ) use ( $ignored_dirs ) {
477480

478481
ksort( $manifest );
479482

480-
file_put_contents( WP_CLI_HANDBOOK_PATH . '/bin/handbook-manifest.json', json_encode( $manifest, JSON_PRETTY_PRINT ) );
483+
file_put_contents( self::get_handbook_path() . '/bin/handbook-manifest.json', json_encode( $manifest, JSON_PRETTY_PRINT ) );
481484
WP_CLI::success( 'Generated bin/handbook-manifest.json' );
482485
}
483486

@@ -840,7 +843,7 @@ private static function parse_docblock( $docblock ) {
840843
*/
841844
private static function render( string $path, $binding ): string {
842845
$m = new Mustache_Engine();
843-
$template = file_get_contents( WP_CLI_HANDBOOK_PATH . "/bin/templates/$path" );
846+
$template = file_get_contents( self::get_handbook_path() . "/bin/templates/$path" );
844847
return $m->render( $template, $binding );
845848
}
846849

@@ -864,5 +867,3 @@ private static function empty_dir( $dir ) {
864867
WP_CLI::log( sprintf( "Removed existing contents of '%s'", $dir ) );
865868
}
866869
}
867-
868-
WP_CLI::add_command( 'handbook', '\WP_CLI\Handbook\Command' );

bin/handbook-command.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
if ( ! class_exists( 'WP_CLI' ) ) {
4+
return;
5+
}
6+
7+
$wpcli_handbook_autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
8+
9+
if ( file_exists( $wpcli_handbook_autoloader ) ) {
10+
require_once $wpcli_handbook_autoloader;
11+
}
12+
13+
WP_CLI::add_command( 'handbook', '\WP_CLI\Handbook\Handbook_Command' );

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
"sort-packages": true
1010
},
1111
"autoload": {
12+
"psr-4": {
13+
"WP_CLI\\Handbook\\": "bin/"
14+
},
1215
"files": [
13-
"bin/command.php"
16+
"bin/handbook-command.php"
1417
]
1518
},
1619
"require-dev": {

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ parameters:
44
- bin
55
scanDirectories:
66
- vendor/wp-cli/wp-cli
7+
excludePaths:
8+
- bin/packages
79
treatPhpDocTypesAsCertain: false
810
strictRules:
911
strictCalls: true

wp-cli.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
require:
2-
- bin/command.php
2+
- bin/handbook-command.php

0 commit comments

Comments
 (0)