File size: 1,344 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

declare(strict_types=1);

namespace Plugin\Infrastructure;

use ArrayIterator;
use DirectoryIterator;
use Iterator;
use UnexpectedValueException;
use ValueError;

class PluginFinder
{
    /**
     * Find all plugins in the given directory sorted by creation time.
     * 
     * @param string $dir The directory to search for plugins.
     * @return Iterator<int,string>
     */
    public function findPlugins(string $dir): Iterator
    {
        $pluginDirs = new ArrayIterator();

        try {
            $di = new DirectoryIterator($dir);
        } catch (UnexpectedValueException | ValueError $th) {
            return $pluginDirs;
        }

        foreach ($di as $vendor) {
            if ($vendor->isDir() && !$vendor->isDot()) {
                $iterator = new DirectoryIterator($vendor->getPathname());

                foreach ($iterator as $package) {
                    $composerJsonFile = $package->getPathname()
                        . '/composer.json';

                    if (file_exists($composerJsonFile)) {
                        $pluginDirs->append($package->getPathname());
                    }
                }
            }
        }

        $pluginDirs->uasort(
            fn (string $a, string $b): int =>
            filectime($b) <=> filectime($a)
        );

        return $pluginDirs;
    }
}