|
10 | 10 | use \WP_CLI\Dispatcher;
|
11 | 11 | use \WP_CLI\Iterators\Transform;
|
12 | 12 |
|
| 13 | +const PHAR_STREAM_PREFIX = 'phar://'; |
| 14 | + |
13 | 15 | function inside_phar() {
|
14 |
| - return 0 === strpos( WP_CLI_ROOT, 'phar://' ); |
| 16 | + return 0 === strpos( WP_CLI_ROOT, PHAR_STREAM_PREFIX ); |
15 | 17 | }
|
16 | 18 |
|
17 | 19 | // Files that need to be read by external programs have to be extracted from the Phar archive.
|
@@ -87,19 +89,6 @@ function load_command( $name ) {
|
87 | 89 | }
|
88 | 90 | }
|
89 | 91 |
|
90 |
| -function load_all_commands() { |
91 |
| - $cmd_dir = WP_CLI_ROOT . '/php/commands'; |
92 |
| - |
93 |
| - $iterator = new \DirectoryIterator( $cmd_dir ); |
94 |
| - |
95 |
| - foreach ( $iterator as $filename ) { |
96 |
| - if ( '.php' != substr( $filename, -4 ) ) |
97 |
| - continue; |
98 |
| - |
99 |
| - include_once "$cmd_dir/$filename"; |
100 |
| - } |
101 |
| -} |
102 |
| - |
103 | 92 | /**
|
104 | 93 | * Like array_map(), except it returns a new iterator, instead of a modified array.
|
105 | 94 | *
|
@@ -197,10 +186,15 @@ function assoc_args_to_str( $assoc_args ) {
|
197 | 186 | $str = '';
|
198 | 187 |
|
199 | 188 | foreach ( $assoc_args as $key => $value ) {
|
200 |
| - if ( true === $value ) |
| 189 | + if ( true === $value ) { |
201 | 190 | $str .= " --$key";
|
202 |
| - else |
| 191 | + } elseif( is_array( $value ) ) { |
| 192 | + foreach( $value as $_ => $v ) { |
| 193 | + $str .= assoc_args_to_str( array( $key => $v ) ); |
| 194 | + } |
| 195 | + } else { |
203 | 196 | $str .= " --$key=" . escapeshellarg( $value );
|
| 197 | + } |
204 | 198 | }
|
205 | 199 |
|
206 | 200 | return $str;
|
@@ -453,6 +447,19 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
|
453 | 447 | * IMPORTANT: Automatic HTML escaping is disabled!
|
454 | 448 | */
|
455 | 449 | function mustache_render( $template_name, $data = array() ) {
|
| 450 | + // Transform absolute path to relative path inside of Phar |
| 451 | + if ( inside_phar() && 0 === stripos( $template_name, PHAR_STREAM_PREFIX ) ) { |
| 452 | + $search = ''; |
| 453 | + $replace = ''; |
| 454 | + if ( file_exists( WP_CLI_ROOT . '/vendor/autoload.php' ) ) { |
| 455 | + $search = dirname( __DIR__ ); |
| 456 | + $replace = WP_CLI_ROOT; |
| 457 | + } elseif ( file_exists( dirname( dirname( WP_CLI_ROOT ) ) . '/autoload.php' ) ) { |
| 458 | + $search = dirname( dirname( dirname( __DIR__ ) ) ); |
| 459 | + $replace = dirname( dirname( dirname( WP_CLI_ROOT ) ) ); |
| 460 | + } |
| 461 | + $template_name = str_replace( $search, $replace, $template_name ); |
| 462 | + } |
456 | 463 | if ( ! file_exists( $template_name ) )
|
457 | 464 | $template_name = WP_CLI_ROOT . "/templates/$template_name";
|
458 | 465 |
|
@@ -840,3 +847,111 @@ function parse_str_to_argv( $arguments ) {
|
840 | 847 | function basename( $path, $suffix = '' ) {
|
841 | 848 | return urldecode( \basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
|
842 | 849 | }
|
| 850 | + |
| 851 | +/** |
| 852 | + * Checks whether the output of the current script is a TTY or a pipe / redirect |
| 853 | + * |
| 854 | + * Returns true if STDOUT output is being redirected to a pipe or a file; false is |
| 855 | + * output is being sent directly to the terminal. |
| 856 | + * |
| 857 | + * If an env variable SHELL_PIPE exists, returned result depends it's |
| 858 | + * value. Strings like 1, 0, yes, no, that validate to booleans are accepted. |
| 859 | + * |
| 860 | + * To enable ASCII formatting even when shell is piped, use the |
| 861 | + * ENV variable SHELL_PIPE=0 |
| 862 | + * |
| 863 | + * @access public |
| 864 | + * |
| 865 | + * @return bool |
| 866 | + */ |
| 867 | +function isPiped() { |
| 868 | + $shellPipe = getenv('SHELL_PIPE'); |
| 869 | + |
| 870 | + if ($shellPipe !== false) { |
| 871 | + return filter_var($shellPipe, FILTER_VALIDATE_BOOLEAN); |
| 872 | + } else { |
| 873 | + return (function_exists('posix_isatty') && !posix_isatty(STDOUT)); |
| 874 | + } |
| 875 | +} |
| 876 | + |
| 877 | +/** |
| 878 | + * Expand within paths to their matching paths. |
| 879 | + * |
| 880 | + * Has no effect on paths which do not use glob patterns. |
| 881 | + * |
| 882 | + * @param string|array $paths Single path as a string, or an array of paths. |
| 883 | + * @param int $flags Flags to pass to glob. |
| 884 | + * |
| 885 | + * @return array Expanded paths. |
| 886 | + */ |
| 887 | +function expand_globs( $paths, $flags = GLOB_BRACE ) { |
| 888 | + $expanded = array(); |
| 889 | + |
| 890 | + foreach ( (array) $paths as $path ) { |
| 891 | + $matching = array( $path ); |
| 892 | + |
| 893 | + if ( preg_match( '/[' . preg_quote( '*?[]{}!', '/' ) . ']/', $path ) ) { |
| 894 | + $matching = glob( $path, $flags ) ?: array(); |
| 895 | + } |
| 896 | + |
| 897 | + $expanded = array_merge( $expanded, $matching ); |
| 898 | + } |
| 899 | + |
| 900 | + return array_unique( $expanded ); |
| 901 | +} |
| 902 | + |
| 903 | +/** |
| 904 | + * Get a Phar-safe version of a path. |
| 905 | + * |
| 906 | + * For paths inside a Phar, this strips the outer filesystem's location to |
| 907 | + * reduce the path to what it needs to be within the Phar archive. |
| 908 | + * |
| 909 | + * Use the __FILE__ or __DIR__ constants as a starting point. |
| 910 | + * |
| 911 | + * @param string $path An absolute path that might be within a Phar. |
| 912 | + * |
| 913 | + * @return string A Phar-safe version of the path. |
| 914 | + */ |
| 915 | +function phar_safe_path( $path ) { |
| 916 | + |
| 917 | + if ( ! inside_phar() ) { |
| 918 | + return $path; |
| 919 | + } |
| 920 | + |
| 921 | + return str_replace( |
| 922 | + PHAR_STREAM_PREFIX . WP_CLI_PHAR_PATH . '/', |
| 923 | + PHAR_STREAM_PREFIX, |
| 924 | + $path |
| 925 | + ); |
| 926 | +} |
| 927 | + |
| 928 | +/** |
| 929 | + * Check whether a given Command object is part of the bundled set of |
| 930 | + * commands. |
| 931 | + * |
| 932 | + * This function accepts both a fully qualified class name as a string as |
| 933 | + * well as an object that extends `WP_CLI\Dispatcher\CompositeCommand`. |
| 934 | + * |
| 935 | + * @param \WP_CLI\Dispatcher\CompositeCommand|string $command |
| 936 | + * |
| 937 | + * @return bool |
| 938 | + */ |
| 939 | +function is_bundled_command( $command ) { |
| 940 | + static $classes; |
| 941 | + |
| 942 | + if ( null === $classes ) { |
| 943 | + $classes = array(); |
| 944 | + $class_map = WP_CLI_VENDOR_DIR . '/composer/autoload_commands_classmap.php'; |
| 945 | + if ( file_exists( WP_CLI_VENDOR_DIR . '/composer/') ) { |
| 946 | + $classes = include $class_map; |
| 947 | + } |
| 948 | + } |
| 949 | + |
| 950 | + if ( is_object( $command ) ) { |
| 951 | + $command = get_class( $command ); |
| 952 | + } |
| 953 | + |
| 954 | + return is_string( $command ) |
| 955 | + ? array_key_exists( $command, $classes ) |
| 956 | + : false; |
| 957 | +} |
0 commit comments