| 1 |
<?php |
| 2 |
namespace Elementor\Core\Utils; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class Assets_Config_Provider extends Collection { |
| 9 |
/** |
| 10 |
* @var callable|null |
| 11 |
*/ |
| 12 |
private $path_resolver = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* @param callable $path_resolver |
| 16 |
* |
| 17 |
* @return $this |
| 18 |
*/ |
| 19 |
public function set_path_resolver( callable $path_resolver ) { |
| 20 |
$this->path_resolver = $path_resolver; |
| 21 |
|
| 22 |
return $this; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Load asset config from a file into the collection. |
| 27 |
* |
| 28 |
* @param $key |
| 29 |
* @param $path |
| 30 |
* |
| 31 |
* @return $this |
| 32 |
*/ |
| 33 |
public function load( $key, $path = null ) { |
| 34 |
if ( ! $path && $this->path_resolver ) { |
| 35 |
$path_resolver_callback = $this->path_resolver; |
| 36 |
|
| 37 |
$path = $path_resolver_callback( $key ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! $path || ! file_exists( $path ) ) { |
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
$config = require $path; |
| 45 |
|
| 46 |
if ( ! $this->is_valid_handle( $config ) ) { |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
$this->items[ $key ] = [ |
| 51 |
'handle' => $config['handle'], |
| 52 |
'deps' => $this->is_valid_deps( $config ) ? $config['deps'] : [], |
| 53 |
]; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check that the handle property in the config is a valid. |
| 60 |
* |
| 61 |
* @param $config |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
private function is_valid_handle( $config ) { |
| 66 |
return ! empty( $config['handle'] ) && is_string( $config['handle'] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Check that the deps property in the config is a valid. |
| 71 |
* |
| 72 |
* @param $config |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
private function is_valid_deps( $config ) { |
| 77 |
return isset( $config['deps'] ) && is_array( $config['deps'] ); |
| 78 |
} |
| 79 |
} |
| 80 |
|