-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
48 lines (41 loc) · 1.1 KB
/
helpers.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
<?php
use LukasKleinschmidt\Terminal\Script;
use LukasKleinschmidt\Terminal\Terminal;
/**
* Creates a new Scripts instance
*
* @param mixed $script
* @param mixed $model
* @throws Exception
* @return Terminal
*/
function terminal($script, $model = null): Terminal
{
// Try to find a registered script by name
$scripts = kirby()->option('lukaskleinschmidt.terminal.scripts');
$script = $scripts[$script] ?? null;
// Create a script with a closure
if (is_callable($script) === true) {
$script = $script->call($model);
}
// Create a new script object from string
if (is_string($script) === true) {
$script = script($script);
}
// Create a new Terminal
if (is_a($script, 'LukasKleinschmidt\Terminal\Script') === true) {
return new Terminal($script);
}
throw new Exception('Terminal could not be created');
}
/**
* Creates a new Script instance
*
* @param string $cmd
* @param string $cwd
* @return Script
*/
function script(string $cmd, string $cwd = null): Script
{
return new Script($cmd, $cwd ?? kirby()->root('index'));
}