Topological Sort in PHP
Simple Topological Sorting (aka Dependency Resolution) algorithm implementation in PHP., (*1)
Install
composer require pustato/topsort
Example usage
Implement interface \Pustato\TopSort\Contracts\Sortable
:, (*2)
// in SiteAsset.php
class SiteAsset implements Pustato\TopSort\Contracts\Sortable
{
public function getId()
{
return static::class;
}
public function getDependencies()
{
return [
JQueryAsset::class, BootstrapAsset::class
];
}
...
// Asset implementation
}
// in BootstrapAsset.php
class BootstrapAsset implements Pustato\TopSort\Contracts\Sortable
{
public function getId()
{
return static::class;
}
public function getDependencies()
{
return [
JQueryAsset::class
];
}
...
// Asset implementation
}
// in JQueryAsset.php
class JQueryAsset implements Pustato\TopSort\Contracts\Sortable
{
public function getId()
{
return static::class;
}
public function getDependencies()
{
return [];
}
...
// Asset implementation
}
Add all sortable classes to \Pustato\TopSort\Collection
and sort them:, (*3)
$assetsCollection = new \Pustato\TopSort\Collection([
new SiteAsset(), new JQueryAsset(), new BootstrapAsset()
]);
$result = $assetsCollection->getSorted();
var_dump($result);
And you will get:, (*4)
array(3) {
[0] => class JQueryAsset#1 (0) {}
[1] => class BootstrapAsset#2 (0) {}
[2] => class SiteAsset#3 (0) {}
}