Skip to content

Commit

Permalink
Build Blueprints as a .phar file (#28)
Browse files Browse the repository at this point in the history
Uses [the Box project](https://box-project.github.io/box) to build the
Blueprints library to a `.phar` library.

To build the file, run:

```shell
vendor/bin/box compile
```

To try the built .phar file, run:

```shell
rm -rf new-wp/* && USE_PHAR=1 php blueprint_compiling.php
```

## Follow-up work

The built file is 1.3MB compressed, and most of that comes from external
libraries. Let's identify the largest libraries, replace them with
custom and tiny implementation, and see if we can reduce the size to,
say, 100KB.
  • Loading branch information
adamziel authored Feb 23, 2024
1 parent 0d25560 commit e819f95
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 265 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ outdir/
test.php
run_phar.php
run_src.php
.phive
vendor-bin
new-wp
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
## Setup

Install composer dependencies:
Install [composer](https://getcomposer.org/) and the required composer dependencies via

```shell
composer install
```

## Useful commands

Run tests with

```shell
Expand All @@ -24,11 +26,17 @@ Run a Blueprint with
php blueprint_compiling.php
```

Setup WordPress with SQLite using asynchronous resource fetching as follows:
## Building to .phar

The Blueprints library is distributed as a .phar library. To build the .phar file, run:

```shell
php setup_wordpress.php
cd outdir/wordpress
php wp-cli.phar core install --url=localhost:8550 --title="My test site" --admin_user=admin --admin_email="admin@localhost.com" --admin_password="password"
php wp-cli.phar server --port=8550
vendor/bin/box compile
```

To try the built .phar file, run:

```shell
rm -rf new-wp/* && USE_PHAR=1 php blueprint_compiling.php
```

3 changes: 0 additions & 3 deletions bin/build_phar.sh

This file was deleted.

17 changes: 7 additions & 10 deletions blueprint_compiling.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

use WordPress\Blueprints\ContainerBuilder;
use WordPress\Blueprints\Model\BlueprintBuilder;
use WordPress\Blueprints\Runtime\NativePHPRuntime;
use function WordPress\Blueprints\run_blueprint;

require 'vendor/autoload.php';
if ( getenv( 'USE_PHAR' ) ) {
require __DIR__ . '/dist/blueprints.phar';
} else {
require 'vendor/autoload.php';
}

$blueprint = BlueprintBuilder::create()
->withWordPressVersion( 'https://wordpress.org/latest.zip' )
Expand Down Expand Up @@ -34,12 +37,6 @@
->toBlueprint();


$c = ( new ContainerBuilder() )->build(
new NativePHPRuntime(
__DIR__ . '/new-wp'
)
);

$results = $c['blueprint.engine']->runBlueprint( $blueprint );
$results = run_blueprint( $blueprint, __DIR__ . '/new-wp' );

var_dump( $results );
13 changes: 13 additions & 0 deletions box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"main": "vendor/autoload.php",
"output": "dist/blueprints.phar",
"compactors": [
"KevinGH\\Box\\Compactor\\Php"
],
"compression": "GZ",
"annotations": false,
"directories": [
"vendor/symfony/process"
],
"force-autodiscovery": true
}
20 changes: 11 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{
"require": {
"json-mapper/json-mapper": "^2.21",
"symfony/event-dispatcher": "^7.0",
"justinrainbow/json-schema": "^5.2",
"symfony/filesystem": "^7.0",
"psr/simple-cache": "^3.0",
"symfony/process": "^7.0",
"symfony/http-client": "^7.0",
"symfony/http-kernel": "^7.0",
"pimple/pimple": "^3.0",
"opis/json-schema": "^2.3",
"nette/php-generator": "^4.1",
"json-mapper/json-mapper": "^2.21"
"psr/simple-cache": "^3.0",
"opis/json-schema": "^2.3"
},
"require-dev": {
"pestphp/pest": "^2.33",
"jane-php/json-schema": "^7.6"
"nette/php-generator": "^4.1",
"jane-php/json-schema": "^7.6",
"bamarni/composer-bin-plugin": "^1.8"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
"php-http/discovery": true,
"bamarni/composer-bin-plugin": true
}
},
"autoload": {
"psr-4": {
"WordPress\\": "src/WordPress"
"WordPress\\": "src/WordPress",
"Symfony\\Component\\Process\\": "vendor/symfony/process"
},
"files": [
"src/WordPress/Blueprints/functions.php",
"src/WordPress/Zip/functions.php",
"src/WordPress/Blueprints/functions.php",
"src/WordPress/Streams/stream_str_replace.php"
]
}
Expand Down
Loading

0 comments on commit e819f95

Please sign in to comment.