forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HasCommandInterface.php
69 lines (60 loc) · 1.82 KB
/
HasCommandInterface.php
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
<?php
/**
* @file classes/cliTool/traits/HasCommandInterface.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @trait HasCommandInterface
*
* @ingroup tools
*
* @brief A helper trait for CLI tools that provide functionality to read/write on CLI interface
*/
namespace PKP\cliTool\traits;
use PKP\cliTool\CommandInterface;
use Symfony\Component\Console\Helper\Helper;
trait HasCommandInterface
{
/**
* CLI interface, this object should extends InteractsWithIO
*/
protected ?CommandInterface $commandInterface = null;
/**
* Set the command interface
*/
public function setCommandInterface(?CommandInterface $commandInterface = null): self
{
$this->commandInterface = $commandInterface ?? new CommandInterface;
return $this;
}
/**
* Get the command interface
*/
public function getCommandInterface(): CommandInterface
{
return $this->commandInterface;
}
/**
* Print given options in a pretty way.
*/
protected function printCommandList(array $options, bool $shouldTranslate = true): void
{
$width = (int)collect(array_keys($options))
->map(fn($command) => Helper::width($command))
->sort()
->last() + 2;
foreach ($options as $commandName => $description) {
$spacingWidth = $width - Helper::width($commandName);
$this->getCommandInterface()->line(
sprintf(
' <info>%s</info>%s%s',
$commandName,
str_repeat(' ', $spacingWidth),
$shouldTranslate ? __($description) : $description
)
);
}
}
}