From 6c0ebc823be44104229d3f10ec83b6353870c8ed Mon Sep 17 00:00:00 2001 From: jesseeproductions Date: Wed, 6 Mar 2024 11:23:36 -0500 Subject: [PATCH] Assets - Add bock asset features to compile dependencies and version from a blocks index.asset.php file --- src/Assets/Asset.php | 107 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index ab82dcb..fca20cf 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -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 { + $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; + } }