Skip to content

Commit 436fc0a

Browse files
Permit WP-CLI to be built as a phar when dependency of another project
1 parent afe561b commit 436fc0a

3 files changed

Lines changed: 67 additions & 24 deletions

File tree

php/boot-phar.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?php
22

3-
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar' );
4-
5-
include WP_CLI_ROOT . '/php/wp-cli.php';
3+
if ( file_exists( 'phar://wp-cli.phar/php/wp-cli.php' ) ) {
4+
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar' );
5+
include WP_CLI_ROOT . '/php/wp-cli.php';
6+
} elseif ( file_exists( 'phar://wp-cli.phar/vendor/wp-cli/wp-cli/php/wp-cli.php' ) ) {
7+
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar/vendor/wp-cli/wp-cli' );
8+
include WP_CLI_ROOT . '/php/wp-cli.php';
9+
} else {
10+
echo "Couldn't find 'php/wp-cli.php'. Was this Phar built correctly?";
11+
exit(1);
12+
}
613

php/utils.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ function extract_from_phar( $path ) {
3535

3636
function load_dependencies() {
3737
if ( inside_phar() ) {
38-
require WP_CLI_ROOT . '/vendor/autoload.php';
38+
if ( file_exists( WP_CLI_ROOT . '/vendor/autoload.php' ) ) {
39+
require WP_CLI_ROOT . '/vendor/autoload.php';
40+
} elseif ( file_exists( dirname( dirname( WP_CLI_ROOT ) ) . '/autoload.php' ) ) {
41+
require dirname( dirname( WP_CLI_ROOT ) ) . '/autoload.php';
42+
}
3943
return;
4044
}
4145

utils/make-phar.php

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
define( 'WP_CLI_ROOT', dirname( dirname( __FILE__ ) ) );
44

5-
require WP_CLI_ROOT . '/vendor/autoload.php';
5+
if ( file_exists( WP_CLI_ROOT . '/vendor/autoload.php' ) ) {
6+
define( 'WP_CLI_BASE_PATH', WP_CLI_ROOT );
7+
define( 'WP_CLI_VENDOR_DIR' , WP_CLI_ROOT . '/vendor' );
8+
} elseif ( file_exists( dirname( dirname( WP_CLI_ROOT ) ) . '/autoload.php' ) ) {
9+
define( 'WP_CLI_BASE_PATH', dirname( dirname( dirname( WP_CLI_ROOT ) ) ) );
10+
define( 'WP_CLI_VENDOR_DIR' , dirname( dirname( WP_CLI_ROOT ) ) );
11+
} else {
12+
echo 'Missing vendor/autoload.php';
13+
exit(1);
14+
}
15+
require WP_CLI_VENDOR_DIR . '/autoload.php';
616
require WP_CLI_ROOT . '/php/utils.php';
717

818
use Symfony\Component\Finder\Finder;
@@ -36,7 +46,7 @@
3646
}
3747

3848
function add_file( $phar, $path ) {
39-
$key = str_replace( WP_CLI_ROOT, '', $path );
49+
$key = str_replace( WP_CLI_BASE_PATH, '', $path );
4050

4151
if ( !BE_QUIET )
4252
echo "$key - $path\n";
@@ -45,7 +55,7 @@ function add_file( $phar, $path ) {
4555
}
4656

4757
function set_file_contents( $phar, $path, $content ) {
48-
$key = str_replace( WP_CLI_ROOT, '', $path );
58+
$key = str_replace( WP_CLI_BASE_PATH, '', $path );
4959

5060
if ( !BE_QUIET )
5161
echo "$key - $path\n";
@@ -65,17 +75,17 @@ function set_file_contents( $phar, $path, $content ) {
6575
->name('*.php')
6676
->in(WP_CLI_ROOT . '/php')
6777
->in(WP_CLI_ROOT . '/features')
68-
->in(WP_CLI_ROOT . '/vendor/wp-cli')
69-
->in(WP_CLI_ROOT . '/vendor/mustache')
70-
->in(WP_CLI_ROOT . '/vendor/rmccue/requests')
71-
->in(WP_CLI_ROOT . '/vendor/composer')
72-
->in(WP_CLI_ROOT . '/vendor/psr')
73-
->in(WP_CLI_ROOT . '/vendor/seld')
74-
->in(WP_CLI_ROOT . '/vendor/symfony')
75-
->in(WP_CLI_ROOT . '/vendor/nb/oxymel')
76-
->in(WP_CLI_ROOT . '/vendor/ramsey/array_column')
77-
->in(WP_CLI_ROOT . '/vendor/mustangostang')
78-
->in(WP_CLI_ROOT . '/vendor/justinrainbow/json-schema')
78+
->in(WP_CLI_VENDOR_DIR . '/wp-cli')
79+
->in(WP_CLI_VENDOR_DIR . '/mustache')
80+
->in(WP_CLI_VENDOR_DIR . '/rmccue/requests')
81+
->in(WP_CLI_VENDOR_DIR . '/composer')
82+
->in(WP_CLI_VENDOR_DIR . '/psr')
83+
->in(WP_CLI_VENDOR_DIR . '/seld')
84+
->in(WP_CLI_VENDOR_DIR . '/symfony')
85+
->in(WP_CLI_VENDOR_DIR . '/nb/oxymel')
86+
->in(WP_CLI_VENDOR_DIR . '/ramsey/array_column')
87+
->in(WP_CLI_VENDOR_DIR . '/mustangostang')
88+
->in(WP_CLI_VENDOR_DIR . '/justinrainbow/json-schema')
7989
->exclude('test')
8090
->exclude('tests')
8191
->exclude('Test')
@@ -87,6 +97,27 @@ function set_file_contents( $phar, $path, $content ) {
8797
add_file( $phar, $file );
8898
}
8999

100+
// Include base project files, because the autoloader will load them
101+
if ( WP_CLI_BASE_PATH !== WP_CLI_ROOT ) {
102+
$finder = new Finder();
103+
$finder
104+
->files()
105+
->ignoreVCS(true)
106+
->name('*.php')
107+
->in(WP_CLI_BASE_PATH . '/src')
108+
->exclude('test')
109+
->exclude('tests')
110+
->exclude('Test')
111+
->exclude('Tests');
112+
foreach ( $finder as $file ) {
113+
add_file( $phar, $file );
114+
}
115+
// Any PHP files in the project root
116+
foreach ( glob( WP_CLI_BASE_PATH . '/*.php' ) as $file ) {
117+
add_file( $phar, $file );
118+
}
119+
}
120+
90121
// other files
91122
$finder = new Finder();
92123
$finder
@@ -100,20 +131,21 @@ function set_file_contents( $phar, $path, $content ) {
100131
add_file( $phar, $file );
101132
}
102133

103-
add_file( $phar, WP_CLI_ROOT . '/vendor/autoload.php' );
134+
add_file( $phar, WP_CLI_VENDOR_DIR . '/autoload.php' );
104135
add_file( $phar, WP_CLI_ROOT . '/ci/behat-tags.php' );
105-
add_file( $phar, WP_CLI_ROOT . '/vendor/composer/ca-bundle/res/cacert.pem' );
106-
add_file( $phar, WP_CLI_ROOT . '/vendor/composer/composer/LICENSE' );
107-
add_file( $phar, WP_CLI_ROOT . '/vendor/composer/composer/res/composer-schema.json' );
108-
add_file( $phar, WP_CLI_ROOT . '/vendor/rmccue/requests/library/Requests/Transport/cacert.pem' );
136+
add_file( $phar, WP_CLI_VENDOR_DIR . '/composer/ca-bundle/res/cacert.pem' );
137+
add_file( $phar, WP_CLI_VENDOR_DIR . '/composer/composer/LICENSE' );
138+
add_file( $phar, WP_CLI_VENDOR_DIR . '/composer/composer/res/composer-schema.json' );
139+
add_file( $phar, WP_CLI_VENDOR_DIR . '/rmccue/requests/library/Requests/Transport/cacert.pem' );
109140

110141
set_file_contents( $phar, WP_CLI_ROOT . '/VERSION', $current_version );
111142

143+
$phar_boot = str_replace( WP_CLI_BASE_PATH, '', WP_CLI_ROOT . '/php/boot-phar.php' );
112144
$phar->setStub( <<<EOB
113145
#!/usr/bin/env php
114146
<?php
115147
Phar::mapPhar();
116-
include 'phar://wp-cli.phar/php/boot-phar.php';
148+
include 'phar://wp-cli.phar/{$phar_boot}';
117149
__HALT_COMPILER();
118150
?>
119151
EOB

0 commit comments

Comments
 (0)