Skip to content

Commit

Permalink
Add new Blueprint examples (#68)
Browse files Browse the repository at this point in the history
- Adds a script - json_string_compiling.php running the Blueprint from a
string containing a JSON.
- Creates the 'examples' directory to group all nonessential scrips
added to showcase the lib's capabilities.
- Moves the blueprint_compiling.php script to the 'examples' directory.
- Fixes scrips to run with PHP >= 7.0.
  • Loading branch information
reimic authored Mar 11, 2024
1 parent d1e32e6 commit 1ec2e45
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,18 @@ composer install
vendor/bin/phpunit --testdox
```

### Run a Blueprint with
### Run Blueprints in a variety of ways

#### using the PHP Blueprint builder:

```shell
php examples/blueprint_compiling.php
```

#### using a string containg a Blueprint (in JSON):

```shell
php blueprint_compiling.php
php examples/json_string_compiling.php
```

### Regenerate models files from JSON schema with
Expand Down
12 changes: 5 additions & 7 deletions blueprint_compiling.php → examples/blueprint_compiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
// ->withContent( 'https://raw.githubusercontent.com/WordPress/theme-test-data/master/themeunittestdata.wordpress.xml' )
->withSiteUrl( 'http://localhost:8081' )
->andRunSQL( <<<'SQL'
CREATE TABLE `tmp_table` ( id INT );
INSERT INTO `tmp_table` VALUES (1);
INSERT INTO `tmp_table` VALUES (2);
SQL
CREATE TABLE `tmp_table` ( id INT );
INSERT INTO `tmp_table` VALUES (1);
INSERT INTO `tmp_table` VALUES (2);
SQL
)
->withFile( 'wordpress.txt', 'Data' )
->toBlueprint();
Expand All @@ -54,7 +54,7 @@ public static function getSubscribedEvents() {
];
}

protected ProgressBar $progress_bar;
protected $progress_bar;

public function __construct() {
ProgressBar::setFormatDefinition( 'custom', ' [%bar%] %current%/%max% -- %message%' );
Expand Down Expand Up @@ -84,5 +84,3 @@ public function onDone( DoneEvent $event ) {
'progressSubscriber' => $subscriber,
]
);

//var_dump( $results );
60 changes: 60 additions & 0 deletions examples/json_string_compiling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WordPress\Blueprints\ContainerBuilder;
use WordPress\Blueprints\Progress\DoneEvent;
use WordPress\Blueprints\Progress\ProgressEvent;
use function WordPress\Blueprints\run_blueprint;

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

$blueprint = '{"WordPressVersion":"https://wordpress.org/latest.zip","steps":[{"step":"mkdir","path":"dir"},{"step": "rm","path": "dir"}]}';

$subscriber = new class() implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
ProgressEvent::class => 'onProgress',
DoneEvent::class => 'onDone',
);
}

protected $progress_bar;

public function __construct() {
ProgressBar::setFormatDefinition( 'custom', ' [%bar%] %current%/%max% -- %message%' );

$this->progress_bar = ( new SymfonyStyle(
new StringInput( '' ),
new ConsoleOutput()
) )->createProgressBar( 100 );
$this->progress_bar->setFormat( 'custom' );
$this->progress_bar->setMessage( 'Start' );
$this->progress_bar->start();
}

public function onProgress( ProgressEvent $event ) {
$this->progress_bar->setMessage( $event->caption );
$this->progress_bar->setProgress( (int) $event->progress );
}

public function onDone( DoneEvent $event ) {
$this->progress_bar->finish();
}
};

$results = run_blueprint(
$blueprint,
array(
'environment' => ContainerBuilder::ENVIRONMENT_NATIVE,
'documentRoot' => __DIR__ . '/new-wp',
'progressSubscriber' => $subscriber,
)
);

0 comments on commit 1ec2e45

Please sign in to comment.