Skip to content

Commit

Permalink
Fix types and PHP version-related issues (#62)
Browse files Browse the repository at this point in the history
- Fixes an error where the Resource Resolver... Resolving process was
erroneously typed and caused PHP to confuse itself.
- Fixes types to match PHP 7.0 coding standards.
- Fixed some other small issues in affected classes that would cause
coding standards to signal warnings.
- Fixed a member access issue caused by the Compiler.
  • Loading branch information
reimic authored Mar 5, 2024
1 parent 85c67fa commit 4f3104c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

class FilesystemResourceResolver implements ResourceResolverInterface {

public function parseUrl( string $url ): ResourceDefinitionInterface|false {
public function parseUrl( string $url ): ?ResourceDefinitionInterface {
if ( ! str_starts_with( $url, 'file://' ) ) {
return false;
return null;
}

return ( new FilesystemResource() )->setPath( $url );
Expand All @@ -24,12 +24,12 @@ public function supports( ResourceDefinitionInterface $resource ): bool {
return $resource instanceof FilesystemResource;
}

public function stream( ResourceDefinitionInterface $resource, Tracker $progressTracker ) {
public function stream( ResourceDefinitionInterface $resource, Tracker $progress_tracker ) {
if ( ! $this->supports( $resource ) ) {
throw new \InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
}

$progressTracker->finish();
$progress_tracker->finish();

/** @var $resource FilesystemResource */
return fopen( $resource->path, 'r' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class InlineResourceResolver implements ResourceResolverInterface {

public function parseUrl( string $url ): ResourceDefinitionInterface|false {
public function parseUrl( string $url ): ?ResourceDefinitionInterface {
// If url starts with "protocol://" then we assume it's not inline raw data
if ( 0 !== preg_match( '#^[a-z_+]+://#', $url ) ) {
return false;
return null;
}

return ( new InlineResource() )->setContents( $url );
Expand All @@ -26,11 +26,11 @@ public function supports( ResourceDefinitionInterface $resource ): bool {
return $resource instanceof InlineResource;
}

public function stream( ResourceDefinitionInterface $resource, Tracker $progressTracker ) {
public function stream( ResourceDefinitionInterface $resource, Tracker $progress_tracker ) {
if ( ! $this->supports( $resource ) ) {
throw new \InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
}
$progressTracker->finish();
$progress_tracker->finish();

/** @var $resource InlineResource */
$fp = fopen( "php://temp", 'r+' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@

namespace WordPress\Blueprints\Resources\Resolver;

use InvalidArgumentException;
use RuntimeException;
use WordPress\Blueprints\Model\DataClass\ResourceDefinitionInterface;
use WordPress\Blueprints\Progress\Tracker;

class ResourceResolverCollection implements ResourceResolverInterface {

/** @var ResourceResolverInterface[] */
protected array $resource_resolvers;

public function __construct(
/** @var ResourceResolverInterface[] */
protected $ResourceResolvers
array $resource_resolvers
) {
$this->resource_resolvers = $resource_resolvers;
}

static public function getResourceClass(): string {
throw new \RuntimeException( 'Not implemented' );
public static function getResourceClass(): string {
throw new RuntimeException( 'Not implemented' );
}

public function parseUrl( string $url ): ResourceDefinitionInterface|false {
foreach ( $this->ResourceResolvers as $handler ) {
$resource = $handler->parseUrl( $url );
public function parseUrl( string $url ): ?ResourceDefinitionInterface {
foreach ( $this->resource_resolvers as $resolver ) {
/** @var ResourceResolverInterface $resolver */
$resource = $resolver->parseUrl( $url );
if ( $resource ) {
return $resource;
}
}

return false;
return null;
}

public function supports( ResourceDefinitionInterface $resource ): bool {
foreach ( $this->ResourceResolvers as $handler ) {
if ( $handler->supports( $resource ) ) {
foreach ( $this->resource_resolvers as $resolver ) {
/** @var ResourceResolverInterface $resolver */
if ( $resolver->supports( $resource ) ) {
return true;
}
}
Expand All @@ -39,13 +46,13 @@ public function supports( ResourceDefinitionInterface $resource ): bool {
}

public function stream( ResourceDefinitionInterface $resource, Tracker $progressTracker ) {
foreach ( $this->ResourceResolvers as $handler ) {
if ( $handler->supports( $resource ) ) {
return $handler->stream( $resource, $progressTracker );
foreach ( $this->resource_resolvers as $resolver ) {
/** @var ResourceResolverInterface $resolver */
if ( $resolver->supports( $resource ) ) {
return $resolver->stream( $resource, $progressTracker );
}
}

throw new \InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
throw new InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use WordPress\Blueprints\Progress\Tracker;

interface ResourceResolverInterface {
public function parseUrl( string $url ): ResourceDefinitionInterface|false;
public function parseUrl( string $url ): ?ResourceDefinitionInterface;

public function supports( ResourceDefinitionInterface $resource ): bool;

static public function getResourceClass(): string;

public function stream( ResourceDefinitionInterface $resource, Tracker $progressTracker );
public function stream( ResourceDefinitionInterface $resource, Tracker $progress_tracker );
}
30 changes: 17 additions & 13 deletions src/WordPress/Blueprints/Resources/Resolver/UrlResourceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WordPress\Blueprints\Resources\Resolver;

use WordPress\Blueprints\Model\Builder\UrlResourceBuilder;
use InvalidArgumentException;
use WordPress\Blueprints\Model\DataClass\ResourceDefinitionInterface;
use WordPress\Blueprints\Model\DataClass\UrlResource;
use WordPress\Blueprints\Progress\Tracker;
Expand All @@ -11,45 +11,49 @@

class UrlResourceResolver implements ResourceResolverInterface {

public function __construct( protected DataSourceInterface $dataSource ) {
protected DataSourceInterface $data_source;

public function __construct( DataSourceInterface $data_source ) {
$this->data_source = $data_source;
}

public function parseUrl( string $url ): ResourceDefinitionInterface|false {
public function parseUrl( string $url ): ?ResourceDefinitionInterface {
if ( ! str_starts_with( $url, 'http://' ) && ! str_starts_with( $url, 'https://' ) ) {
return false;
return null;
}

return ( new UrlResource() )->setUrl( $url );
}


static public function getResourceClass(): string {
public static function getResourceClass(): string {
return UrlResource::class;
}

public function supports( ResourceDefinitionInterface $resource ): bool {
return $resource instanceof UrlResource;
}

public function stream( ResourceDefinitionInterface $resource, Tracker $progressTracker ) {
public function stream( ResourceDefinitionInterface $resource, Tracker $progress_tracker ) {
if ( ! $this->supports( $resource ) ) {
throw new \InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
throw new InvalidArgumentException( 'Resource ' . get_class( $resource ) . ' unsupported' );
}

$this->dataSource->events->addListener( DataSourceProgressEvent::class,
function ( DataSourceProgressEvent $progress ) use ( $progressTracker, $resource ) {
$this->data_source->events->addListener(
DataSourceProgressEvent::class,
function ( DataSourceProgressEvent $progress ) use ( $progress_tracker, $resource ) {
if ( $resource->url === $progress->url ) {
// If we don't have totalBytes, we assume 5MB
$totalBytes = $progress->totalBytes ?: 5 * 1024 * 1024;

$progressTracker->set(
$progress_tracker->set(
100 * $progress->downloadedBytes / $totalBytes
);
}
} );
}
);

/** @var $resource UrlResource */
return $this->dataSource->stream( $resource->url );
return $this->data_source->stream( $resource->url );
}

}
10 changes: 6 additions & 4 deletions src/WordPress/Blueprints/Resources/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

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\ResourceResolverCollection;

class ResourceManager {

protected Filesystem $fs;
protected ResourceMap $map;
protected ResourceResolverCollection $resource_resolvers;

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

public function enqueue( array $compiledResources ) {
foreach ( $compiledResources as $compiled ) {
/** @var CompiledResource $compiled */
$this->map[ $compiled->declaration ] = $this->resourceResolver->stream(

$this->map[ $compiled->declaration ] = $this->resource_resolvers->stream(
$compiled->resource,
$compiled->progressTracker
);
Expand Down
11 changes: 7 additions & 4 deletions src/WordPress/Blueprints/Runner/Blueprint/BlueprintRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
use WordPress\Blueprints\Compile\CompiledBlueprint;
use WordPress\Blueprints\Compile\CompiledStep;
use WordPress\Blueprints\Compile\StepSuccess;
use WordPress\Blueprints\Progress\Tracker;
use WordPress\Blueprints\Runtime\RuntimeInterface;

class BlueprintRunner {

public readonly EventDispatcher $events;
public EventDispatcher $events;
protected RuntimeInterface $runtime;
protected $resourceManagerFactory;

public function __construct(
protected RuntimeInterface $runtime,
protected $resourceManagerFactory,
RuntimeInterface $runtime,
$resourceManagerFactory
) {
$this->resourceManagerFactory = $resourceManagerFactory;
$this->runtime = $runtime;
$this->events = new EventDispatcher();
}

Expand Down
2 changes: 1 addition & 1 deletion src/WordPress/Blueprints/Runner/Step/BaseStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function getRuntime(): RuntimeInterface
return $this->runtime;
}

protected function getDefaultCaption($input): ?string
public function getDefaultCaption($input): ?string
{
return null;
}
Expand Down

0 comments on commit 4f3104c

Please sign in to comment.