Skip to content

Commit 894f47a

Browse files
committed
support module cli integration with tutorial example
1 parent 19b212c commit 894f47a

2 files changed

Lines changed: 71 additions & 12 deletions

File tree

cli/ob.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public function run()
6666
// Find the most specific CLI class based on the commands provided.
6767
$commands = array_slice($this->argv, 1);
6868
$cliInstance = null;
69+
$cliMap = $this->getCliMap();
6970
$commandLength = count($this->argv);
7071
do {
71-
$className = implode('', array_map(fn ($x) => ucwords($x), $commands));
72+
$command = implode(' ', $commands);
7273

73-
if (file_exists(OB_LOCAL . '/core/cli/' . $className . '.php')) {
74-
require_once(OB_LOCAL . '/core/cli/' . $className . '.php');
74+
if (isset($cliMap[$command])) {
75+
require_once($cliMap[$command]['file']);
7576

76-
$fullClassName = 'OpenBroadcaster\\CLI\\' . $className;
77-
$cliInstance = new $fullClassName();
77+
$cliInstance = new ($cliMap[$command]['class'])();
7878

7979
break;
8080
}
@@ -98,15 +98,12 @@ public function help()
9898
';
9999

100100
$rows = [];
101-
$cliList = array_filter(scandir(OB_LOCAL . '/core/cli/'), fn($f) => $f[0] !== '.');
102-
foreach($cliList as $cliPath) {
103-
$cliFileName = pathinfo($cliPath, PATHINFO_FILENAME);
104-
$cliCommand = strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $cliFileName));
105-
$cliClassName = "OpenBroadcaster\\CLI\\" . $cliFileName;
106101

107-
require_once(OB_LOCAL . '/core/cli/' . $cliPath);
102+
$cliMap = $this->getCliMap();
103+
foreach ($cliMap as $cliCommand => $cliItem) {
104+
require_once($cliItem['file']);
108105

109-
$cliClass = new $cliClassName();
106+
$cliClass = new ($cliItem['class'])();
110107
$help = $cliClass->help();
111108

112109
if (is_array($help)) {
@@ -123,6 +120,44 @@ public function help()
123120

124121
echo Helpers::table(spacing: 5, rows: $rows);
125122
}
123+
124+
private function getCliMap(): array
125+
{
126+
$cliMap = [];
127+
$coreDir = OB_LOCAL . '/core';
128+
$subDirs = [$coreDir, ...glob(OB_LOCAL . '/modules/*')];
129+
130+
foreach ($subDirs as $subDir) {
131+
$dir = $subDir . '/cli/';
132+
if (! file_exists($dir) || ! is_dir($dir)) {
133+
continue;
134+
}
135+
136+
$files = array_filter(scandir($dir), fn($f) => $f[0] !== '.');
137+
138+
foreach ($files as $file) {
139+
$cliFileName = pathinfo($file, PATHINFO_FILENAME);
140+
$cliCommand = strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $cliFileName));
141+
$cliClassName = "OpenBroadcaster\\CLI\\" . $cliFileName;
142+
143+
if (isset($cliMap[$cliCommand])) {
144+
echo "Duplicate CLI command found: '{$cliCommand}'. Perhaps one or more models define conflicting commands? Quitting." . PHP_EOL;
145+
146+
exit(2);
147+
}
148+
149+
if ($subDir === $coreDir) {
150+
}
151+
$cliMap[$cliCommand] = [
152+
'type' => ($subDir === $coreDir) ? 'core' : 'module',
153+
'file' => $dir . $file,
154+
'class' => $cliClassName,
155+
];
156+
}
157+
}
158+
159+
return $cliMap;
160+
}
126161
}
127162

128163
(new OBCLI($argv))->run();

modules/Tutorial/cli/Tutorial.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace OpenBroadcaster\CLI;
4+
5+
use OpenBroadcaster\Base\CLI;
6+
7+
class Tutorial extends CLI
8+
{
9+
protected array|string $help = [
10+
['<args>', 'module example, echoes arguments back to user as string'],
11+
];
12+
13+
public function run(array $args): bool
14+
{
15+
if (count($args) < 1) {
16+
(new OBCLI())->help();
17+
return false;
18+
}
19+
20+
echo implode(' ', $args) . PHP_EOL;
21+
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)