-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPlease.php
80 lines (66 loc) · 1.7 KB
/
Please.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
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Statamic\Cli;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class Please
{
protected $output;
protected $cwd;
/**
* Instantiate Statamic `please` command wrapper.
*/
public function __construct(OutputInterface $output)
{
$this->output = $output;
}
/**
* Get or set current working directory.
*
* @param mixed $cwd
* @return mixed
*/
public function cwd($cwd = null)
{
if (func_num_args() === 0) {
return $this->cwd ?? getcwd();
}
$this->cwd = $cwd;
return $this;
}
/**
* Check if Statamic instance is v2.
*
* @return bool
*/
public function isV2()
{
return is_dir($this->cwd().'/statamic') && is_file($this->cwd().'/please');
}
/**
* Run please command.
*
* @param mixed $commandParts
* @return int
*/
public function run(...$commandParts)
{
if (! is_file($this->cwd().'/please')) {
throw new \RuntimeException('This does not appear to be a Statamic project.');
}
$process = (new Process(array_merge([PHP_BINARY, 'please'], $commandParts)))
->setTimeout(null);
if ($this->cwd) {
$process->setWorkingDirectory($this->cwd);
}
try {
$process->setTty(true);
} catch (RuntimeException $e) {
// TTY not supported. Move along.
}
$process->run(function ($type, $line) {
$this->output->write($line);
});
return $process->getExitCode();
}
}