-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assets - Add bock asset features to compile dependencies and version …
…from a blocks index.asset.php file
- Loading branch information
1 parent
5c8f06c
commit 6c0ebc8
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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 GitHub Actions / phpstan
|
||
$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 ) ) { | ||
$block_dependencies = array_merge( $block_dependencies, $this->dependencies ); | ||
} | ||
|
||
$this->set_dependencies( ...$block_dependencies ); | ||
|
||
return $this; | ||
} | ||
} |