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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#!/usr/bin/env php
<?php
namespace Studip\Cli;
use Symfony\Component\Console\Application;
require __DIR__ . '/studip_cli_env.inc.php';
require __DIR__ . '/../composer/autoload.php';
\StudipAutoloader::addAutoloadPath('cli', 'Studip\\Cli');
$application = new Application();
$application->addCommands(loadCoreCommands());
$application->addCommands(loadPluginCommands($application));
$application->run();
function loadCoreCommands(): array
{
$commands = require __DIR__ . '/commands.php';
return array_map(fn($command) => app($command), $commands);
}
function loadPluginCommands(Application $application): array
{
$pluginCommands = [];
foreach (scanPluginDirectory() as $manifest) {
$pluginCommands = array_merge($pluginCommands, getPluginCommands($application, $manifest));
}
return $pluginCommands;
}
function scanPluginDirectory(): \Generator
{
$basepath = \Config::get()->PLUGINS_PATH;
$pluginManager = \PluginManager::getInstance();
$iterator = createPluginManifestIterator($basepath);
foreach ($iterator as $manifestFile) {
$manifest = $pluginManager->getPluginManifest($manifestFile->getPath());
if (isValidPluginManifest($manifest, $basepath, $manifestFile)) {
$manifest['path'] = $manifestFile->getPath();
yield $manifest;
}
}
}
function createPluginManifestIterator(string $basepath): \RegexIterator
{
return new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$basepath,
\FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::UNIX_PATHS
)
),
'/\/plugin\.manifest$/',
\RegexIterator::MATCH
);
}
function isValidPluginManifest(array $manifest, string $basepath, \SplFileInfo $manifestFile): bool
{
if (!isset($manifest['pluginclassname'], $manifest['cli'])) {
return false;
}
$pluginpath = $basepath . '/' . $manifest['origin'] . '/' . $manifest['pluginclassname'];
return $pluginpath === $manifestFile->getPath();
}
function getPluginCommands(Application $application, array $manifest): array
{
$cliFile = $manifest['path'] . '/' . $manifest['cli'];
$commandsFn = require_once $cliFile;
$commands = [];
foreach ($commandsFn($application) as $class => $path) {
require_once $path;
$commands[] = app($class);
}
return $commands;
}
|