Skip to content

Commit

Permalink
Adding groups of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Oct 22, 2024
1 parent 2ba83d5 commit 3877966
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 22 deletions.
45 changes: 42 additions & 3 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ class Asset {
*/
protected string $root_path = '';

/**
* The path group name for this asset.
*
* A path group is a group of assets that share the same path which could be different that the root path or the asset's path.
*
* The order of priority goes like this:
*
* 1. If a specific root path is set, that will be used.
* 2. If a path group is set, that will be used.
* 3. Otherwise, the root path will be used.
*
* @var string
*/
protected string $group_path_name = '';

/**
* Content or callable that should be printed after the asset.
*
Expand Down Expand Up @@ -285,6 +300,19 @@ public function __construct( string $slug, string $file, string $version = null,
$this->infer_type();
}

/**
* Adds the asset to a group path.
*
* @since TBD
*
* @return static
*/
public function add_to_group_path( string $group_path_name ) {
$this->group_path_name = $group_path_name;

return $this;
}

/**
* Registers an asset.
*
Expand Down Expand Up @@ -630,7 +658,7 @@ public function clone_to( string $clone_type, ...$dependencies ) {
$slug,
str_replace( ".{$source_type}", ".{$clone_type}", $this->file ),
$this->version,
$this->root_path
$this->get_root_path()
);

$condition = $this->get_condition();
Expand Down Expand Up @@ -910,7 +938,8 @@ public function get_min_url(): string {
*/
public function get_path(): string {
if ( $this->path === null ) {
return Config::get_relative_asset_path();
$group_relative = $this->group_path_name ? Config::get_relative_path_of_group_path( $this->group_path_name ) : '';
return $group_relative ? $group_relative : Config::get_relative_asset_path();
}

return $this->path;
Expand All @@ -922,7 +951,17 @@ public function get_path(): string {
* @return ?string
*/
public function get_root_path(): ?string {
return $this->root_path;
if ( ! $this->group_path_name ) {
return $this->root_path;
}

if ( $this->root_path !== Config::get_path() ) {
return $this->root_path;
}

$group_path = Config::get_path_of_group_path( $this->group_path_name );

return $group_path ? $group_path : $this->root_path;
}

/**
Expand Down
100 changes: 81 additions & 19 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Config {
*/
protected static string $root_path = '';

/**
* @var array<string, array<string, string>>
*/
protected static array $group_paths = [];

/**
* @var string
*/
Expand All @@ -42,6 +47,55 @@ public static function get_hook_prefix(): string {
return static::$hook_prefix;
}

/**
* Gets the root path of a group.
*
* @since TBD
*
* @return string
*/
public static function get_path_of_group_path( string $group ): string {
return ( static::$group_paths[ $group ] ?? [] )['root'] ?? '';
}

/**
* Gets the relative path of a group.
*
* @since TBD
*
* @return string
*/
public static function get_relative_path_of_group_path( string $group ): string {
return ( static::$group_paths[ $group ] ?? [] )['relative'] ?? '';
}

/**
* Adds a group path.
*
* @since TBD
*
* @throws RuntimeException If the root or relative path is not specified.
*
* @param string $group_path_slug The slug of the group path.
* @param array<string, string> $paths The paths of the group path.
*
* @return void
*/
public static function add_group_path( string $group_path_slug, array $paths ): void {
if ( empty( $paths['root'] ) || ! is_string( $paths['root'] ) ) {

Check failure on line 85 in src/Assets/Config.php

View workflow job for this annotation

GitHub Actions / phpstan

Negated boolean expression is always false.
throw new RuntimeException( 'You must specify a root path for the group path' );
}

if ( empty( $paths['relative'] ) || ! is_string( $paths['relative'] ) ) {

Check failure on line 89 in src/Assets/Config.php

View workflow job for this annotation

GitHub Actions / phpstan

Negated boolean expression is always false.
throw new RuntimeException( 'You must specify a relative path for the group path' );
}

$paths['root'] = self::normalize_path( $paths['root'] );
$paths['relative'] = trailingslashit( $paths['relative'] );

static::$group_paths[ $group_path_slug ] = $paths;
}

/**
* Gets the root path of the project.
*
Expand Down Expand Up @@ -131,16 +185,35 @@ public static function set_relative_asset_path( string $path ) {
* @return void
*/
public static function set_path( string $path ) {
$plugin_dir = WP_PLUGIN_DIR;
static::$root_path = self::normalize_path( $path );
}

if ( DIRECTORY_SEPARATOR !== '/' ) {
$plugin_dir = str_replace( DIRECTORY_SEPARATOR, '/', $plugin_dir );
// Because the $path passed can be a constant like plugin_dir_path( __FILE__ ), we should check and replace the slash in the $path too.
$path = str_replace( DIRECTORY_SEPARATOR, '/', $path );
}
/**
* Sets the version of the project.
*
* @param string $version The version of the project.
*
* @return void
*/
public static function set_version( string $version ) {
static::$version = $version;
}

/**
* Normalizes a path.
*
* @since TBD
*
* @param string $path The path to normalize.
*
* @return string
*/
protected static function normalize_path( string $path ): string {
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
$path = wp_normalize_path( $path );

$plugins_content_dir_position = strpos( $path, $plugin_dir );
$themes_content_dir_position = strpos( $path, get_theme_root() );
$themes_content_dir_position = strpos( $path, wp_normalize_path( get_theme_root() ) );

if (
$plugins_content_dir_position === false
Expand All @@ -154,17 +227,6 @@ public static function set_path( string $path ) {
$path = substr( $path, $themes_content_dir_position );
}

static::$root_path = trailingslashit( $path );
}

/**
* Sets the version of the project.
*
* @param string $version The version of the project.
*
* @return void
*/
public static function set_version( string $version ) {
static::$version = $version;
return trailingslashit( $path );
}
}

0 comments on commit 3877966

Please sign in to comment.