12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace OC;
- use Psr\Log\LoggerInterface;
- class AppScriptSort {
-
- private $logger;
- public function __construct(LoggerInterface $logger) {
- $this->logger = $logger;
- }
-
- private function topSortVisit(
- AppScriptDependency $app,
- array &$parents,
- array &$scriptDeps,
- array &$sortedScriptDeps): void {
-
- if (isset($parents[$app->getId()])) {
- $this->logger->error('Circular dependency in app scripts at app ' . $app->getId());
- }
-
- if (!$app->isVisited()) {
- $parents[$app->getId()] = true;
- $app->setVisited(true);
- foreach ($app->getDeps() as $dep) {
- if ($app->getId() === $dep) {
-
- continue;
- }
- if (isset($scriptDeps[$dep])) {
- $newParents = $parents;
- $this->topSortVisit($scriptDeps[$dep], $newParents, $scriptDeps, $sortedScriptDeps);
- }
- }
- $sortedScriptDeps[] = $app->getId();
- }
- }
-
- public function sort(array $scripts, array $scriptDeps): array {
-
- $sortedScriptDeps = [];
- foreach ($scriptDeps as $app) {
- $parents = [];
- $this->topSortVisit($app, $parents, $scriptDeps, $sortedScriptDeps);
- }
-
- $sortedScripts = [];
- foreach ($sortedScriptDeps as $app) {
- $sortedScripts[$app] = $scripts[$app] ?? [];
- }
-
- foreach (array_keys($scripts) as $app) {
- if (!isset($sortedScripts[$app])) {
- $sortedScripts[$app] = $scripts[$app];
- }
- }
- return $sortedScripts;
- }
- }
|