| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AssetsManager; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Assets { |
| 10 |
private $assets; |
| 11 |
private $assets_map; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
$this->assets = []; |
| 15 |
$this->assets_map = []; |
| 16 |
} |
| 17 |
|
| 18 |
public function append( $handle, $uri, $dependencies = [], $version = '', $options = [] ) { |
| 19 |
if ( ! array_key_exists( $handle, $this->assets_map ) ) { |
| 20 |
$this->assets_map[ $handle ] = [ |
| 21 |
'uri' => $uri . ( $version ? '?ver=' . $version : '' ), |
| 22 |
'options' => $options, |
| 23 |
]; |
| 24 |
$this->assets[ $handle ] = $dependencies; |
| 25 |
} |
| 26 |
return $this; |
| 27 |
} |
| 28 |
|
| 29 |
public function assets_map() { |
| 30 |
return $this->assets_map; |
| 31 |
} |
| 32 |
|
| 33 |
public function priority_queue() { |
| 34 |
$graph = []; |
| 35 |
$in_degree = []; |
| 36 |
|
| 37 |
foreach ( $this->assets as $handle => $dependencies ) { |
| 38 |
if ( ! array_key_exists( $handle, $in_degree ) ) { |
| 39 |
$in_degree[ $handle ] = 0; |
| 40 |
} |
| 41 |
|
| 42 |
foreach ( $dependencies as $dependency ) { |
| 43 |
if ( ! array_key_exists( $dependency, $graph ) ) { |
| 44 |
$graph[ $dependency ] = []; |
| 45 |
} |
| 46 |
|
| 47 |
$graph[ $dependency ][] = $handle; |
| 48 |
|
| 49 |
$in_degree[ $handle ]++; |
| 50 |
|
| 51 |
if ( ! array_key_exists( $dependency, $in_degree ) ) { |
| 52 |
$in_degree[ $dependency ] = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$queue = new \SplQueue(); |
| 58 |
|
| 59 |
foreach ( $in_degree as $handle => $count ) { |
| 60 |
if ( 0 === $count ) { |
| 61 |
$queue->enqueue( $handle ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
$priority_queue = []; |
| 66 |
|
| 67 |
while ( ! $queue->isEmpty() ) { |
| 68 |
$current = $queue->dequeue(); |
| 69 |
|
| 70 |
$priority_queue[] = $current; |
| 71 |
|
| 72 |
if ( ! array_key_exists( $current, $graph ) ) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
foreach ( $graph[ $current ] as $next ) { |
| 77 |
$in_degree[ $next ]--; |
| 78 |
|
| 79 |
if ( 0 === $in_degree[ $next ] ) { |
| 80 |
$queue->enqueue( $next ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $priority_queue; |
| 86 |
} |
| 87 |
} |
| 88 |
|