PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / CLI / Commands.php

Commands.php in WPGraphQL trunk, at src/CLI/Commands.php

106 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPGraphQL CLI Commands
4 *
5 * @package WPGraphQL
6 * @since 2.7.0
7 */
8
9 namespace WPGraphQL\CLI;
10
11 use WPGraphQL;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Class - Commands
19 */
20 class Commands extends \WP_CLI_Command {
21 /**
22 * Generate a static schema.
23 *
24 * Defaults to creating a schema.graphql file in the IDL format at the root
25 * of the plugin.
26 *
27 * [--output=<output>]
28 * : The file path to save the schema to.
29 *
30 * @todo: Provide alternative formats (AST? INTROSPECTION JSON?) and options for output file-type?
31 * @todo: Add Unit Tests
32 *
33 * ## EXAMPLE
34 *
35 * # Generate a static schema
36 * $ wp graphql generate-static-schema
37 *
38 * # Generate a static schema and save it to a specific file
39 * $ wp graphql generate-static-schema --output=/path/to/file.graphql
40 *
41 * @alias generate
42 * @subcommand generate-static-schema
43 *
44 * @param array<string> $args Positional arguments.
45 * @param array<string, mixed> $assoc_args Associative arguments.
46 */
47 public function generate_static_schema( $args, $assoc_args ): void {
48
49 // Check if the output flag is set
50 if ( isset( $assoc_args['output'] ) ) {
51 // Check if the output file path is writable and its parent directory exists
52 // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable -- this can be run anywhere.
53 if ( ! is_writable( dirname( $assoc_args['output'] ) ) ) {
54 \WP_CLI::error( 'The output file path is not writable or its parent directory does not exist.' );
55 }
56 $file_path = $assoc_args['output'];
57 } else {
58 $file_path = get_temp_dir() . 'schema.graphql';
59 }
60
61 if ( ! defined( 'GRAPHQL_REQUEST' ) ) {
62 define( 'GRAPHQL_REQUEST', true );
63 }
64
65 /**
66 * Fires when initializing a GraphQL request from WP-CLI.
67 *
68 * @hookGroup request-lifecycle
69 * @since 0.0.32
70 */
71 do_action( 'init_graphql_request' );
72
73 /**
74 * Generate the Schema
75 */
76 \WP_CLI::line( 'Getting the Schema...' );
77
78 // Set the introspection query flag
79 WPGraphQL::set_is_introspection_query( true );
80
81 // Get the schema
82 $schema = WPGraphQL::get_schema();
83
84 /**
85 * Format the Schema
86 */
87 \WP_CLI::line( 'Formatting the Schema...' );
88 $printed = \GraphQL\Utils\SchemaPrinter::doPrint( $schema );
89
90 /**
91 * Save the Schema to the file
92 */
93 \WP_CLI::line( 'Saving the Schema...' );
94
95 file_put_contents( $file_path, $printed ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_file_put_contents -- we verified the path is writable above.
96
97 // Reset the introspection query flag
98 WPGraphQL::set_is_introspection_query( false );
99
100 /**
101 * All done!
102 */
103 \WP_CLI::success( sprintf( 'All done. Schema output to %s.', $file_path ) );
104 }
105 }
106