Select Git revision
plugin.manifest
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
studip 2.31 KiB
#!/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->run();
function loadCoreCommands(): array
{
$commands = require __DIR__ . '/commands.php';
return array_map(fn($command) => app($command), $commands);
}
function loadPluginCommands(): array
{
$pluginCommands = [];
foreach (scanPluginDirectory() as $manifest) {
$pluginCommands = array_merge($pluginCommands, getPluginCommands($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(array $manifest): array
{
$cliFile = $manifest['path'] . '/' . $manifest['cli'];
$commandsFn = require_once $cliFile;
$commands = [];
foreach ($commandsFn() as $class => $path) {
require_once $path;
$commands[] = app($class);
}
return $commands;
}