aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Plugins/I18N/I18NCommand.php
blob: 6fa9f77c9cab3807509e3e14b6085ac48bcf9bad (plain)
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
<?php
namespace Studip\Cli\Commands\Plugins\I18N;

use Exception;
use Studip\Cli\Commands\AbstractPluginCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

abstract class I18NCommand extends AbstractPluginCommand
{
    private $plugin_manager = null;

    protected function configure(): void
    {
        $this->addOption('pluginname', 'p', InputArgument::OPTIONAL, 'name of the plugin');
        $this->addOption('folder', 'f', InputArgument::OPTIONAL, 'folder to scan (overrides pluginname)');
    }

    protected function getPluginFolder(InputInterface $input): string
    {
        $pluginname = $input->getOption('pluginname');
        $folder     = $input->getOption('folder');

        if (!$pluginname && !$folder) {
            throw new Exception('You must specify either pluginname or folder.');
        }

        if (!$folder && $pluginname) {
            $plugin = $this->findPluginByName($this->getPluginManager(), $pluginname);
            if ($plugin === null) {
                throw new Exception('Could not find plugin of that name.');
            }

            $folder = "{$GLOBALS['PLUGINS_PATH']}/{$plugin['path']}";
        }

        if (!$folder || !file_exists($folder) || !is_readable($folder)) {
            throw new Exception('Could not access folder.');
        }

        return $folder;
    }

    protected function getPluginManifest(string $folder): array
    {
        return $this->getPluginManager()->getPluginManifest($folder);
    }

    protected function getPluginLocaleDomainByFolder(string $folder): string
    {
        $manifest = $this->getPluginManifest($folder);
        if (!$manifest) {
            throw new Exception("Could not detect plugin manifest in folder {$folder}");
        }

        if (!isset($manifest['localedomain'])) {
            throw new Exception('Manifest has no defined localedomain');
        }

        return $manifest['localedomain'];
    }

    protected function getPluginManager(): \PluginManager
    {
        if ($this->plugin_manager === null) {
            $this->plugin_manager = \PluginManager::getInstance();
        }
        return $this->plugin_manager;
    }

    protected function getSystemLanguages(): array
    {
        return array_map(function ($lang) {
            return explode('_', $lang)[0];
        }, array_keys($GLOBALS['INSTALLED_LANGUAGES']));
    }
}