Skip to content

Commit b1faa93

Browse files
Run migrations using php artisan (#102)
1 parent 2156131 commit b1faa93

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/Artisan.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Statamic\Cli;
4+
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
use Symfony\Component\Process\Exception\RuntimeException;
7+
use Symfony\Component\Process\Process;
8+
9+
class Artisan
10+
{
11+
protected $output;
12+
protected $cwd;
13+
14+
/**
15+
* Instantiate Laravel `artisan` command wrapper.
16+
*/
17+
public function __construct(OutputInterface $output)
18+
{
19+
$this->output = $output;
20+
}
21+
22+
/**
23+
* Get or set current working directory.
24+
*
25+
* @param mixed $cwd
26+
* @return mixed
27+
*/
28+
public function cwd($cwd = null)
29+
{
30+
if (func_num_args() === 0) {
31+
return $this->cwd ?? getcwd();
32+
}
33+
34+
$this->cwd = $cwd;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* Run artisan command.
41+
*
42+
* @param mixed $commandParts
43+
* @return int
44+
*/
45+
public function run(...$commandParts)
46+
{
47+
if (! is_file($this->cwd().'/artisan')) {
48+
throw new \RuntimeException('This does not appear to be a Laravel project.');
49+
}
50+
51+
$process = (new Process(array_merge([PHP_BINARY, 'artisan'], $commandParts)))
52+
->setTimeout(null);
53+
54+
if ($this->cwd) {
55+
$process->setWorkingDirectory($this->cwd);
56+
}
57+
58+
try {
59+
$process->setTty(true);
60+
} catch (RuntimeException $e) {
61+
// TTY not supported. Move along.
62+
}
63+
64+
$process->run(function ($type, $line) {
65+
$this->output->write($line);
66+
});
67+
68+
return $process->getExitCode();
69+
}
70+
}

src/NewCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ protected function configureDatabaseConnection()
610610
$command[] = '--no-interaction';
611611
}
612612

613-
$migrate = (new Please($this->output))
613+
$migrate = (new Artisan($this->output))
614614
->cwd($this->absolutePath)
615615
->run(...$command);
616616

0 commit comments

Comments
 (0)