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 6, 2024
1 parent 4517a17 commit 2e0d591
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 96 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
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
2 changes: 0 additions & 2 deletions src/WordPress/Blueprints/Resources/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -47,5 +46,4 @@ public function bufferToTemporaryFile( $resource, $callback, $suffix = null ) {
$this->fs->remove( $path );
}
}

}
17 changes: 11 additions & 6 deletions src/WordPress/Blueprints/Runner/Step/UnzipStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@

class UnzipStepRunner extends BaseStepRunner {

/**
* Runs the Unzip Step
*
* @param UnzipStep $input Step.
* @param Tracker $progress_tracker Tracker.
* @return void
*/
public function run(
UnzipStep $input,
Tracker $progress = null
Tracker $progress_tracker
) {
$progress?->set( 10, 'Unzipping...' );
$progress_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->extractToPath );
zip_extract_to( $this->getResource( $input->zipFile ), $resolved_to_path );
}

}
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
51 changes: 36 additions & 15 deletions src/WordPress/Zip/ZipFileEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,47 @@
namespace WordPress\Zip;

class ZipFileEntry {
public readonly bool $isDirectory;
public bool $isDirectory;
public int $version;
public int $generalPurpose;
public int $compressionMethod;
public int $lastModifiedTime;
public int $lastModifiedDate;
public int $crc;
public int $compressedSize;
public int $uncompressedSize;
public string $path;
public string $extra;
public string $bytes;

public function __construct(
public readonly int $version,
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 string $path,
public readonly string $extra,
public readonly string $bytes
int $version,
int $generalPurpose,
int $compressionMethod,
int $lastModifiedTime,
int $lastModifiedDate,
int $crc,
int $compressedSize,
int $uncompressedSize,
string $path,
string $extra,
string $bytes
) {
$this->isDirectory = $this->path[ - 1 ] === '/';
$this->bytes = $bytes;
$this->extra = $extra;
$this->path = $path;
$this->uncompressedSize = $uncompressedSize;
$this->compressedSize = $compressedSize;
$this->crc = $crc;
$this->lastModifiedDate = $lastModifiedDate;
$this->lastModifiedTime = $lastModifiedTime;
$this->compressionMethod = $compressionMethod;
$this->generalPurpose = $generalPurpose;
$this->version = $version;
$this->isDirectory = $this->path[- 1] === '/';
}

public function isFileEntry() {
return true;
}

}
2 changes: 1 addition & 1 deletion src/WordPress/Zip/ZipStreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static protected function readEndCentralDirectoryEntry( $stream ): ZipEndCentral
*
* @return false|string
*/
static protected function read_bytes( $stream, $length ): string|bool {
static protected function read_bytes( $stream, $length ) {
if ( $length === 0 ) {
return '';
}
Expand Down
18 changes: 6 additions & 12 deletions src/WordPress/Zip/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace WordPress\Zip;

use Symfony\Component\Filesystem\Path;

function zip_read_entry( $fp ) {
return ZipStreamReader::readEntry( $fp );
}

function zip_extract_to( $fp, $toPath ) {
function zip_extract_to( $fp, $to_path ) {
while ( $entry = zip_read_entry( $fp ) ) {
if ( ! $entry->isFileEntry() ) {
continue;
}
$path = $toPath . '/' . sanitize_path( $entry->path );
$parent = dirname( $path );

$path = Path::canonicalize( $to_path . '/' . $entry->path );
$parent = Path::getDirectory( $path );
if ( ! is_dir( $parent ) ) {
mkdir( $parent, 0777, true );
}
Expand All @@ -28,12 +31,3 @@ function zip_extract_to( $fp, $toPath ) {

return feof( $fp ) ? 1 : 0;
}

// @TODO: Find a more reliable technique
function sanitize_path( $path ) {
if ( empty( $path ) ) {
return '';
}

return preg_replace( '#/\.+(/|$)#', '/', $path );
}
Loading

0 comments on commit 2e0d591

Please sign in to comment.