Current Path : /storage/v11800/affypharma/public_html/wp-content/plugins/auxin-elements/includes/classes/ |
Linux v11800 5.3.0-1023-aws #25~18.04.1-Ubuntu SMP Fri Jun 5 15:19:18 UTC 2020 aarch64
|
Current File : /storage/v11800/affypharma/public_html/wp-content/plugins/auxin-elements/includes/classes/class-auxin-dependency-sorting.php |
<?php
/**
* Dependency sorting/resolution algorithm for PHP
*
*/
class Auxin_Dependency_Sorting {
/**
* The sorted list of dependency graph items
*
* @var array
*/
public $sorted = array();
/**
* Stores the list of checked nodes in dependency graph
*
* @var array
*/
protected $checked = array();
/**
* Stores the input dependency graph
*
* @var array
*/
protected $graph = array();
public function __construct(){}
/**
* Check and sort dependencies for each node
*
* @param string $name The node name
* @param array $ancestors The nodes that are already collected
* @return void
*/
private function node_check( $name, $ancestors = array() ){
$ancestors = (array) $ancestors;
$ancestors[] = $name;
$this->checked[ $name ] = true;
// sometimes a dependency is not in main nodes list
if( isset( $this->graph[ $name ] ) ){
foreach ( $this->graph[ $name ] as $dependency ) {
if( in_array( $dependency, $ancestors) ){
throw new Exception( "Circular dependency for following node detected: " . $name );
}
if( isset( $this->checked[ $dependency ] ) ){
continue;
}
$this->node_check( $dependency, $ancestors );
}
}
if( ! in_array( $name, $this->sorted ) ){
$this->sorted[] = $name;
}
}
/**
* Sorts and retrieves the list of sorted items based on defined dependencies
*
* @param array $graph The list of items and their dependencies
* @return array The sorted list of items based on defined dependencies
*/
public function resolve( $graph ) {
$this->graph = $graph;
foreach ( $this->graph as $main_node => $ancestors) {
$this->node_check( $main_node );
}
return $this->sorted;
}
}