pustato / topsort
Topological sorting implementation in PHP
Installs: 44
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/pustato/topsort
Requires
- php: >=7.0
Requires (Dev)
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2025-10-12 06:42:50 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) {}
}