Skip to content

Commit

Permalink
Adjust for compatibility with WordPress Playground
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Feb 24, 2024
1 parent 9ba10d6 commit 413e4cf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -34,6 +34,10 @@ The Blueprints library is distributed as a .phar library. To build the .phar fil
vendor/bin/box compile
```

Note that in box.json, the `"check-requirements"` option is set to `false`. Somehow, keeping it as `true` results in a
.phar file
that breaks HTTP requests in Playground. @TODO: Investigate why this is the case.

To try the built .phar file, run:

```shell
Expand Down
1 change: 1 addition & 0 deletions box.json
Expand Up @@ -4,6 +4,7 @@
"compactors": [
"KevinGH\\Box\\Compactor\\Php"
],
"check-requirements": false,
"compression": "GZ",
"annotations": false,
"directories": [
Expand Down
20 changes: 10 additions & 10 deletions src/WordPress/Blueprints/Compile/BlueprintCompiler.php
Expand Up @@ -44,16 +44,16 @@ protected function expandShorthandsIntoSteps( Blueprint $blueprint ) {
( new UrlResource() )
->setUrl( $blueprint->WordPressVersion )
);
$additional_steps[] = ( new InstallSqliteIntegrationStep() )
->setSqlitePluginZip(
( new UrlResource() )
->setUrl( 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip' )
);
$additional_steps[] = ( new WriteFileStep() )
->setPath( 'wp-cli.phar' )
->setData( ( new UrlResource() )->setUrl( 'https://playground.wordpress.net/wp-cli.phar' ) );
$additional_steps[] = ( new RunWordPressInstallerStep() )
->setOptions( new WordPressInstallationOptions() );
// $additional_steps[] = ( new InstallSqliteIntegrationStep() )
// ->setSqlitePluginZip(
// ( new UrlResource() )
// ->setUrl( 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip' )
// );
// $additional_steps[] = ( new WriteFileStep() )
// ->setPath( 'wp-cli.phar' )
// ->setData( ( new UrlResource() )->setUrl( 'https://playground.wordpress.net/wp-cli.phar' ) );
// $additional_steps[] = ( new RunWordPressInstallerStep() )
// ->setOptions( new WordPressInstallationOptions() );
}
if ( $blueprint->constants ) {
$step = new DefineWpConfigConstsStep();
Expand Down
Expand Up @@ -17,11 +17,16 @@ function run( SetSiteOptionsStep $input, Tracker $tracker ) {
// with a separate wp-cli command.
return $this->getRuntime()->evalPhpInSubProcess( <<<'CODE'
<?php
require 'wp-load.php';
ini_set('display_errors', '1');
error_reporting(E_ALL);
require getenv('DOCROOT'). '/wp-load.php';
$site_options = getenv("OPTIONS") ? json_decode(getenv("OPTIONS"), true) : [];
var_dump($site_options);
foreach($site_options as $name => $value) {
update_option($name, $value);
}
echo "Done!";
CODE,
[
'OPTIONS' => json_encode( $input->options ),
Expand Down
25 changes: 17 additions & 8 deletions src/WordPress/DataSource/UrlSource.php
Expand Up @@ -37,7 +37,7 @@ public function stream( $resourceIdentifier ) {
if ( $this->cache->has( $url ) ) {
// Return a stream resource.
// @TODO: Stream directly from the cache
$cached = $this->cache->get( $url );
$cached = $this->cache->get( $url );
$data_size = strlen( $cached );
$this->events->dispatch( new ProgressEvent(
$url,
Expand All @@ -52,26 +52,35 @@ public function stream( $resourceIdentifier ) {
}

$response = $this->client->request( 'GET', $url, [
'on_progress' => function ( int $dlNow, int $dlSize, array $info ) use ( $url ): void {
'on_progress' => function ( int $dlNow, int $dlSize, array $info ) use ( $url ): void {
$this->events->dispatch( new ProgressEvent(
$url,
$dlNow,
$dlSize
) );
},
// @TODO: Only use these unsecure options in in-browser Playground.
// We use a fake SSL server in there to MITM the HTTPS requests
// and funnel them through fetch() – and fetch() handles HTTPS
// security for us.
'verify_host' => false,
'verify_peer' => false,
'crypto_method' => \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT,
] );
$stream = StreamWrapper::createResource( $response, $this->client );
$stream = StreamWrapper::createResource( $response, $this->client );
if ( ! $stream ) {
throw new \Exception( 'Failed to download file' );
}
$onChunk = function ( $chunk ) use ( $url, $response, $stream ) {
// Handle response caching
// @TODO: don't buffer, just keep appending to the cache.
static $bufferedChunks = [];
$bufferedChunks[] = $chunk;
if ( feof( $stream ) ) {
$this->cache->set( $url, implode( '', $bufferedChunks ) );
}
// Buffering the response causes an out of memory error in the in-browser
// version of Playground
// static $bufferedChunks = [];
// $bufferedChunks[] = $chunk;
// if ( feof( $stream ) ) {
// $this->cache->set( $url, implode( '', $bufferedChunks ) );
// }
};
$onClose = function () use ( $response ) {
$response->cancel();
Expand Down

0 comments on commit 413e4cf

Please sign in to comment.