1111use 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
112115EOT;
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
189192EOT;
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 ' );
0 commit comments