pustato / topsort
Topological sorting implementation in PHP
v1.0.1
2017-09-08 13:54 UTC
Requires
- php: >=7.0
Requires (Dev)
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2025-02-16 03:42:07 UTC
README
Simple Topological Sorting (aka Dependency Resolution) algorithm implementation in PHP.
Install
composer require pustato/topsort
Example usage
Implement interface \Pustato\TopSort\Contracts\Sortable
:
// 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:
$assetsCollection = new \Pustato\TopSort\Collection([ new SiteAsset(), new JQueryAsset(), new BootstrapAsset() ]); $result = $assetsCollection->getSorted(); var_dump($result);
And you will get:
array(3) {
[0] => class JQueryAsset#1 (0) {}
[1] => class BootstrapAsset#2 (0) {}
[2] => class SiteAsset#3 (0) {}
}