diff --git a/.distignore b/.distignore index b964b40..95b52fb 100644 --- a/.distignore +++ b/.distignore @@ -6,6 +6,8 @@ .travis.yml behat.yml circle.yml +phpcs.xml.dist +phpunit.xml.dist bin/ features/ utils/ diff --git a/.gitignore b/.gitignore index ff49419..580eb7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,11 @@ .DS_Store +.phpcs.xml wp-cli.local.yml node_modules/ vendor/ *.zip *.tar.gz composer.lock +phpunit.xml +phpcs.xml *.log diff --git a/composer.json b/composer.json index 05e7dde..18c3fa7 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "wp-cli/wp-cli": "^2" }, "require-dev": { - "wp-cli/wp-cli-tests": "^2.0.7" + "wp-cli/wp-cli-tests": "^2.1" }, "config": { "process-timeout": 7200, diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..542077c --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,58 @@ + + + Custom ruleset for WP-CLI server-command + + + + + . + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */src/Server_Command\.php$ + + diff --git a/router.php b/router.php index 9c3bdc6..86b2f3a 100644 --- a/router.php +++ b/router.php @@ -8,11 +8,16 @@ * * We duplicate it because WordPress is not loaded yet. */ -function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { +function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { global $wp_filter, $merged_filters; - $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); - $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); + $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority ); + + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $wp_filter[ $tag ][ $priority ][ $idx ] = array( + 'function' => $function_to_add, + 'accepted_args' => $accepted_args, + ); unset( $merged_filters[ $tag ] ); return true; } @@ -22,30 +27,35 @@ function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) * * We duplicate it because WordPress is not loaded yet. */ -function _wp_filter_build_unique_id($tag, $function, $priority) { +function _wp_filter_build_unique_id( $tag, $function, $priority ) { global $wp_filter; static $filter_id_count = 0; - if ( is_string($function) ) + if ( is_string( $function ) ) { return $function; + } - if ( is_object($function) ) { + if ( is_object( $function ) ) { // Closures are currently implemented as objects $function = array( $function, '' ); } else { $function = (array) $function; } - if (is_object($function[0]) ) { + if ( is_object( $function[0] ) ) { // Object Class Calling - if ( function_exists('spl_object_hash') ) { - return spl_object_hash($function[0]) . $function[1]; + if ( function_exists( 'spl_object_hash' ) ) { + return spl_object_hash( $function[0] ) . $function[1]; } else { - $obj_idx = get_class($function[0]).$function[1]; - if ( !isset($function[0]->wp_filter_id) ) { - if ( false === $priority ) + $obj_idx = get_class( $function[0] ) . $function[1]; + if ( ! isset( $function[0]->wp_filter_id ) ) { + if ( false === $priority ) { return false; - $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count; + } + $obj_idx .= isset( $wp_filter[ $tag ][ $priority ] ) + ? count( (array) $wp_filter[ $tag ][ $priority ] ) + : $filter_id_count; + $function[0]->wp_filter_id = $filter_id_count; ++$filter_id_count; } else { @@ -54,60 +64,72 @@ function _wp_filter_build_unique_id($tag, $function, $priority) { return $obj_idx; } - } else if ( is_string($function[0]) ) { + } elseif ( is_string( $function[0] ) ) { // Static Calling return $function[0] . '::' . $function[1]; } } function _get_full_host( $url ) { + // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url $parsed_url = parse_url( $url ); $host = $parsed_url['host']; - if ( isset( $parsed_url['port'] ) && $parsed_url['port'] != 80 ) + if ( isset( $parsed_url['port'] ) && 80 !== $parsed_url['port'] ) { $host .= ':' . $parsed_url['port']; + } return $host; } // We need to trick WordPress into using the URL set by `wp server`, especially on multisite. -add_filter( 'option_home', function ( $url ) { - $GLOBALS['_wp_cli_original_url'] = $url; - - return 'http://' . $_SERVER['HTTP_HOST']; -}, 20 ); - -add_filter( 'option_siteurl', function ( $url ) { - if ( !isset( $GLOBALS['_wp_cli_original_url'] ) ) - get_option('home'); // trigger the option_home filter +add_filter( + 'option_home', + function ( $url ) { + $GLOBALS['wpcli_server_original_url'] = $url; + + return 'http://' . $_SERVER['HTTP_HOST']; + }, + 20 +); + +add_filter( + 'option_siteurl', + function ( $url ) { + if ( ! isset( $GLOBALS['wpcli_server_original_url'] ) ) { + get_option( 'home' ); // trigger the option_home filter + } - $home_url_host = _get_full_host( $GLOBALS['_wp_cli_original_url'] ); - $site_url_host = _get_full_host( $url ); + $home_url_host = _get_full_host( $GLOBALS['wpcli_server_original_url'] ); + $site_url_host = _get_full_host( $url ); - if ( $site_url_host == $home_url_host ) { - $url = str_replace( $site_url_host, $_SERVER['HTTP_HOST'], $url ); - } + if ( $site_url_host === $home_url_host ) { + $url = str_replace( $site_url_host, $_SERVER['HTTP_HOST'], $url ); + } - return $url; -}, 20 ); + return $url; + }, + 20 +); $_SERVER['SERVER_ADDR'] = gethostbyname( $_SERVER['SERVER_NAME'] ); -$root = $_SERVER['DOCUMENT_ROOT']; -$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ) )['path'], '/' ); +$wpcli_server_root = $_SERVER['DOCUMENT_ROOT']; +// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url +$wpcli_server_path = '/' . ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ) )['path'], '/' ); -if ( file_exists( $root.$path ) ) { - if ( is_dir( $root.$path ) && substr( $path, -1 ) !== '/' ) { - header( "Location: $path/" ); +if ( file_exists( $wpcli_server_root . $wpcli_server_path ) ) { + if ( is_dir( $wpcli_server_root . $wpcli_server_path ) && substr( $wpcli_server_path, -1 ) !== '/' ) { + header( "Location: $wpcli_server_path/" ); exit; } - if ( strpos( $path, '.php' ) !== false ) { - chdir( dirname( $root.$path ) ); - require_once $root.$path; + if ( strpos( $wpcli_server_path, '.php' ) !== false ) { + chdir( dirname( $wpcli_server_root . $wpcli_server_path ) ); + require_once $wpcli_server_root . $wpcli_server_path; } else { return false; } } else { - chdir( $root ); + chdir( $wpcli_server_root ); require_once 'index.php'; } diff --git a/server-command.php b/server-command.php index 3714abb..d1f5d56 100644 --- a/server-command.php +++ b/server-command.php @@ -4,16 +4,20 @@ return; } -$autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; -if ( file_exists( $autoload ) ) { - require_once $autoload; +$wpcli_server_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php'; +if ( file_exists( $wpcli_server_autoloader ) ) { + require_once $wpcli_server_autoloader; } -WP_CLI::add_command( 'server', 'Server_Command', array( - 'before_invoke' => function() { - $min_version = '5.4'; - if ( version_compare( PHP_VERSION, $min_version, '<' ) ) { - WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." ); - } - } -) ); +WP_CLI::add_command( + 'server', + 'Server_Command', + array( + 'before_invoke' => function() { + $min_version = '5.4'; + if ( version_compare( PHP_VERSION, $min_version, '<' ) ) { + WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." ); + } + }, + ) +); diff --git a/src/Server_Command.php b/src/Server_Command.php index 93a2ed6..ce1be23 100644 --- a/src/Server_Command.php +++ b/src/Server_Command.php @@ -1,5 +1,7 @@ 'localhost', - 'port' => 8080, + public function __invoke( $_, $assoc_args ) { + $defaults = array( + 'host' => 'localhost', + 'port' => 8080, 'docroot' => ! is_null( WP_CLI::get_runner()->config['path'] ) ? WP_CLI::get_runner()->config['path'] : false, - 'config' => get_cfg_var( 'cfg_file_path' ) + 'config' => get_cfg_var( 'cfg_file_path' ), ); $assoc_args = array_merge( $defaults, $assoc_args ); $docroot = $assoc_args['docroot']; - if ( !$docroot ) { + if ( ! $docroot ) { $config_path = WP_CLI::get_runner()->project_config_path; - if ( !$config_path ) { + if ( ! $config_path ) { $docroot = ABSPATH; } else { $docroot = dirname( $config_path ); @@ -79,27 +81,28 @@ function __invoke( $_, $assoc_args ) { } // Get the path to the router file - $command_root = WP_CLI\Utils\phar_safe_path( dirname( __DIR__ ) ); - $router_path = $command_root . '/router.php'; + $command_root = Utils\phar_safe_path( dirname( __DIR__ ) ); + $router_path = $command_root . '/router.php'; if ( ! file_exists( $router_path ) ) { WP_CLI::error( "Couldn't find router.php" ); } - $cmd = \WP_CLI\Utils\esc_cmd( '%s -S %s -t %s -c %s %s', + $cmd = Utils\esc_cmd( + '%s -S %s -t %s -c %s %s', WP_CLI::get_php_binary(), $assoc_args['host'] . ':' . $assoc_args['port'], $docroot, $assoc_args['config'], - \WP_CLI\Utils\extract_from_phar( $router_path ) + Utils\extract_from_phar( $router_path ) ); $descriptors = array( STDIN, STDOUT, STDERR ); // https://bugs.php.net/bug.php?id=60181 $options = array(); - if ( \WP_CLI\Utils\is_windows() ) { - $options["bypass_shell"] = TRUE; + if ( Utils\is_windows() ) { + $options['bypass_shell'] = true; } - exit( proc_close( proc_open( $cmd, $descriptors, $pipes, NULL, NULL, $options ) ) ); + exit( proc_close( proc_open( $cmd, $descriptors, $pipes, null, null, $options ) ) ); } }