Skip to content
Snippets Groups Projects
Select Git revision
  • 1e8bc6cb2b527cde3ae5444fd03c0ee33fca5e2f
  • main default protected
  • 5.5 protected
  • atlantis
  • 5.3 protected
  • 5.0 protected
  • issue-23
  • issue8-seat-logging-and-export
  • ticket-216
  • tickets-215-216-241-242
10 results

Navigation.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    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;
    }