Skip to content

Commit

Permalink
Handle PHP version incompatibility issues, test UnzipStepRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 5, 2024
1 parent 85c67fa commit 30b3644
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 110 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"symfony/http-kernel": "*",
"pimple/pimple": "*",
"psr/simple-cache": "*",
"opis/json-schema": "*"
"opis/json-schema": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand Down
7 changes: 3 additions & 4 deletions src/WordPress/Blueprints/BlueprintParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public function parse($rawBlueprint)
'Unsupported $rawBlueprint type. Use a JSON string, a parsed JSON object, or a BlueprintBuilder instance.'
);
}
// TODO Evaluate waring: missing function's return type
public function fromJson($json)
{
// TODO Evaluate warning: 'ext-json' is missing in composer.json

public function fromJson($json): ?Blueprint
{
return $this->fromObject(json_decode($json, false));
}

Expand Down
48 changes: 26 additions & 22 deletions src/WordPress/Blueprints/Model/DataClass/UnzipStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,64 @@

namespace WordPress\Blueprints\Model\DataClass;

class UnzipStep implements StepDefinitionInterface
{
class UnzipStep implements StepDefinitionInterface {

public const DISCRIMINATOR = 'unzip';

/** @var Progress */
public $progress;
public Progress $progress;

/** @var bool */
public $continueOnError;
public bool $continue_on_error;

/** @var string */
public $step = 'unzip';
/**
* Step
*
* @var string
*/
public string $step = 'unzip';

/** @var string|ResourceDefinitionInterface */
public $zipFile;
/**
* The file to extract
*
* @var string|ResourceDefinitionInterface
*/
public $zip_file;

/**
* The path to extract the zip file to
*
* @var string
*/
public $extractToPath;
public string $extract_to_path;


public function setProgress(Progress $progress)
{
public function setProgress( Progress $progress ) {
$this->progress = $progress;
return $this;
}


public function setContinueOnError(bool $continueOnError)
{
$this->continueOnError = $continueOnError;
public function setContinueOnError( bool $continue_on_error ) {
$this->continue_on_error = $continue_on_error;
return $this;
}


public function setStep(string $step)
{
public function setStep( string $step ) {
$this->step = $step;
return $this;
}


public function setZipFile($zipFile)
{
$this->zipFile = $zipFile;
public function setZipFile( $zip_file ) {
$this->zip_file = $zip_file;
return $this;
}


public function setExtractToPath(string $extractToPath)
{
$this->extractToPath = $extractToPath;
public function setExtractToPath( string $extract_to_path ) {
$this->extract_to_path = $extract_to_path;
return $this;
}
}
22 changes: 18 additions & 4 deletions src/WordPress/Blueprints/Progress/ProgressEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@
* Custom event providing progress details.
*/
class ProgressEvent extends Event {
/**
* The progress percentage as a number between 0 and 100.
*
* @var float $progress
*/
public float $progress;

/**
* The caption to display during progress, a string.
*
* @var ?string $caption
*/
public ?string $caption;

public function __construct(
/** The progress percentage as a number between 0 and 100. */
public float $progress,
/** The caption to display during progress, a string. */
public ?string $caption,
float $progress,
?string $caption
) {
$this->caption = $caption;
$this->progress = $progress;
}
}
2 changes: 1 addition & 1 deletion src/WordPress/Blueprints/Progress/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Tracker {
private $weight;
private $subTrackers = [];

public readonly EventDispatcher $events;
public EventDispatcher $events;

public function __construct( $options = [] ) {
$this->weight = $options['weight'] ?? 1;
Expand Down
11 changes: 5 additions & 6 deletions src/WordPress/Blueprints/Resources/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

use Symfony\Component\Filesystem\Filesystem;
use WordPress\Blueprints\Compile\CompiledResource;
use WordPress\Blueprints\Model\DataClass\ResourceDefinitionInterface;
use WordPress\Blueprints\Resource\Resolver\ResourceResolverInterface;
use WordPress\Blueprints\Resources\Resolver\ResourceResolverInterface;

class ResourceManager {

protected Filesystem $fs;
protected ResourceMap $map;
protected ResourceResolverInterface $resourceResolver;

public function __construct(
protected ResourceResolverInterface $resourceResolver
ResourceResolverInterface $resourceResolver
) {
$this->fs = new Filesystem();
$this->resourceResolver = $resourceResolver;
$this->fs = new Filesystem();
$this->map = new ResourceMap();
}

Expand All @@ -33,7 +34,6 @@ public function getStream( $key ) {
return $this->map[ $key ];
}


public function bufferToTemporaryFile( $resource, $callback, $suffix = null ) {
$fp = $this->getStream( $resource );
$path = $this->fs->tempnam( sys_get_temp_dir(), 'resource', $suffix );
Expand All @@ -45,5 +45,4 @@ public function bufferToTemporaryFile( $resource, $callback, $suffix = null ) {
$this->fs->remove( $path );
}
}

}
23 changes: 17 additions & 6 deletions src/WordPress/Blueprints/Runner/Step/UnzipStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@

namespace WordPress\Blueprints\Runner\Step;

use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\UnzipStep;
use WordPress\Blueprints\Progress\Tracker;
use function WordPress\Zip\zip_extract_to;

class UnzipStepRunner extends BaseStepRunner {

/**
* Runs the Unzip Step
*
* @param UnzipStep $input Step.
* @param Tracker $tracker Tracker.
* @return void
* @throws BlueprintException
*/
public function run(
UnzipStep $input,
Tracker $progress = null
Tracker $tracker
) {
$progress?->set( 10, 'Unzipping...' );
$tracker->set( 10, 'Unzipping...' );

// @TODO: Expose a generic helper method for this, e.g. $this->getExecutionContext()->resolvePath($input->extractToPath);
$toPath = $this->getRuntime()->getDocumentRoot() . '/' . $input->extractToPath;
zip_extract_to( $this->getResource( $input->zipFile ), $toPath );
$resolved_to_path = $this->getRuntime()->resolvePath( $input->extract_to_path );
try {
zip_extract_to( $this->getResource( $input->zip_file ), $resolved_to_path );
} catch ( \Throwable $exception ) {
throw new BlueprintException( "Failed to unzip file \"$input->zip_file\".", 0, $exception );
}
}

}
72 changes: 53 additions & 19 deletions src/WordPress/Zip/ZipCentralDirectoryEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,62 @@

class ZipCentralDirectoryEntry {

public readonly bool $isDirectory;
public bool $isDirectory;
public int $firstByteAt;
public int $versionCreated;
public int $versionNeeded;
public int $generalPurpose;
public int $compressionMethod;
public int $lastModifiedTime;
public int $lastModifiedDate;
public int $crc;
public int $compressedSize;
public int $uncompressedSize;
public int $diskNumber;
public int $internalAttributes;
public int $externalAttributes;
public int $lastByteAt;
public string $path;
public string $extra;
public string $fileComment;

public function __construct(
public readonly int $versionCreated,
public readonly int $versionNeeded,
public readonly int $generalPurpose,
public readonly int $compressionMethod,
public readonly int $lastModifiedTime,
public readonly int $lastModifiedDate,
public readonly int $crc,
public readonly int $compressedSize,
public readonly int $uncompressedSize,
public readonly int $diskNumber,
public readonly int $internalAttributes,
public readonly int $externalAttributes,
public readonly int $firstByteAt,
public readonly int $lastByteAt,
public readonly string $path,
public readonly string $extra,
public readonly string $fileComment,
int $versionCreated,
int $versionNeeded,
int $generalPurpose,
int $compressionMethod,
int $lastModifiedTime,
int $lastModifiedDate,
int $crc,
int $compressedSize,
int $uncompressedSize,
int $diskNumber,
int $internalAttributes,
int $externalAttributes,
int $firstByteAt,
int $lastByteAt,
string $path,
string $extra,
string $fileComment
) {
$this->isDirectory = $this->path[ - 1 ] === '/';
$this->fileComment = $fileComment;
$this->extra = $extra;
$this->path = $path;
$this->lastByteAt = $lastByteAt;
$this->externalAttributes = $externalAttributes;
$this->internalAttributes = $internalAttributes;
$this->diskNumber = $diskNumber;
$this->uncompressedSize = $uncompressedSize;
$this->compressedSize = $compressedSize;
$this->crc = $crc;
$this->lastModifiedDate = $lastModifiedDate;
$this->lastModifiedTime = $lastModifiedTime;
$this->compressionMethod = $compressionMethod;
$this->generalPurpose = $generalPurpose;
$this->versionNeeded = $versionNeeded;
$this->versionCreated = $versionCreated;
$this->firstByteAt = $firstByteAt;
$this->isDirectory = $this->path[- 1] === '/';
}

public function isFileEntry() {
Expand Down
31 changes: 23 additions & 8 deletions src/WordPress/Zip/ZipEndCentralDirectoryEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@

class ZipEndCentralDirectoryEntry {

public int $diskNumber;
public int $centralDirectoryStartDisk;
public int $numberCentralDirectoryRecordsOnThisDisk;
public int $numberCentralDirectoryRecords;
public int $centralDirectorySize;
public int $centralDirectoryOffset;
public string $comment;

public function __construct(
public readonly int $diskNumber,
public readonly int $centralDirectoryStartDisk,
public readonly int $numberCentralDirectoryRecordsOnThisDisk,
public readonly int $numberCentralDirectoryRecords,
public readonly int $centralDirectorySize,
public readonly int $centralDirectoryOffset,
public readonly string $comment
int $diskNumber,
int $centralDirectoryStartDisk,
int $numberCentralDirectoryRecordsOnThisDisk,
int $numberCentralDirectoryRecords,
int $centralDirectorySize,
int $centralDirectoryOffset,
string $comment
) {
$this->comment = $comment;
$this->centralDirectoryOffset = $centralDirectoryOffset;
$this->centralDirectorySize = $centralDirectorySize;
$this->numberCentralDirectoryRecords = $numberCentralDirectoryRecords;
$this->numberCentralDirectoryRecordsOnThisDisk = $numberCentralDirectoryRecordsOnThisDisk;
$this->centralDirectoryStartDisk = $centralDirectoryStartDisk;
$this->diskNumber = $diskNumber;
}

public function isFileEntry() {
return false;
}
Expand Down
Loading

0 comments on commit 30b3644

Please sign in to comment.