Skip to content

Commit

Permalink
Assets - Add bock asset features to compile dependencies and version …
Browse files Browse the repository at this point in the history
…from a blocks index.asset.php file
  • Loading branch information
jesseeproductions committed Mar 6, 2024
1 parent 5c8f06c commit 6c0ebc8
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ class Asset {
*/
protected ?string $version = null;

/**
* The compiled data for all assets already loaded.
*
* @since TBD
*
* @var array
*/
protected array $compiled_data = [];


/**
* Constructor.
*
Expand Down Expand Up @@ -1258,4 +1268,101 @@ public function set_type( string $type ) {
public function should_print(): bool {
return $this->should_print;
}

/**
* Gets the data for a given compiled Asset.
*
* @since TBD
*
* @param string $src The partial path to the asset.
*
* @return ?array
*/
public function get_compiled_data( string $src ): ?array {
$file = $this->get_root_path() . $this->get_path() . $src . '.asset.php';

if ( isset( $this->compiled_data[ $src ] ) ) {
return $this->compiled_data[ $src ];
}

if ( ! file_exists( $file ) ) {
$this->compiled_data[ $src ] = null;
} else {
$this->compiled_data[ $src ] = include $file;
}

return $this->compiled_data[ $src ];
}

/**
* Gets the version for a given compiled Asset.
*
* @since TBD
*
* @param string $src The partial path to the asset.
*
* @return string|null
*/
public function get_compiled_version( string $src ): ?string {
$data = $this->get_compiled_data( $src );

return $data['version'] ?: null;
}

/**
* Set the version for a given compiled Asset.
*
* @since TBD
*
* @param string $src The partial path to the asset.
*
* @return static
*/
public function set_compiled_version( string $src ) {
$version = $this->get_compiled_version( $src );

$this->version = $version ?? Config::get_version();

return $this;
}

/**
* Gets the dependencies for a given compiled Asset.
*
* @since TBD
*
* @param string $src The partial path to the asset.
*
* @return string|null
*/
public function get_compiled_dependencies( string $src ): ?array {

Check failure on line 1338 in src/Assets/Asset.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @return with type string|null is not subtype of native type array|null.
$data = $this->get_compiled_data( $src );

return $data['dependencies'] ?: [];
}

/**
* Set block dependencies.
*
* @since TBD
*
* @param string $src The partial path to the asset.
*
* @return static
*/
public function set_block_dependencies( string $src ) {
$block_dependencies = $this->get_compiled_dependencies( $src );

if ( empty( $block_dependencies ) ) {
return $this;
}

if ( ! empty( $this->dependencies ) && is_array( $this->dependencies ) ) {

Check failure on line 1360 in src/Assets/Asset.php

View workflow job for this annotation

GitHub Actions / phpstan

Right side of && is always true.
$block_dependencies = array_merge( $block_dependencies, $this->dependencies );
}

$this->set_dependencies( ...$block_dependencies );

return $this;
}
}

0 comments on commit 6c0ebc8

Please sign in to comment.