Skip to content

Commit d13b583

Browse files
committed
Fix for output since L5.7+
1 parent fc9328e commit d13b583

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

src/Console/Commands/MigrateDropSingle.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function handle()
8181
}
8282

8383
$this->checkRequirements();
84+
$this->setMigratorOutput();
8485

8586
$this->migrator->rollback(
8687
$this->getMigrationFile(),
@@ -90,10 +91,8 @@ public function handle()
9091
]
9192
);
9293

93-
foreach ($this->migrator->getNotes() as $note) {
94-
if (strpos($note, 'Migration not found') === false) {
95-
$this->output->writeln($note);
96-
}
97-
}
94+
$this->writeMigratorNotes(static function ($note) {
95+
return strpos($note, 'Migration not found') === false;
96+
});
9897
}
9998
}

src/Console/Commands/MigrateSingle.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ public function handle()
6969

7070
$this->checkRequirements();
7171
$this->prepareDatabase();
72+
$this->setMigratorOutput();
7273

7374
$file = $this->getMigrationFile();
7475

7576
$this->migrator->run($file, ['pretend' => $this->option('pretend')]);
7677

77-
foreach ($this->migrator->getNotes() as $note) {
78-
$this->output->writeln($note);
79-
}
78+
$this->writeMigratorNotes();
8079
}
8180
}

src/Console/Commands/Traits/MigrateTrait.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Support\Str;
1010
use RuntimeException;
1111

12-
use function glob, file_exists;
12+
use function glob, file_exists, method_exists;
1313

1414
use const null;
1515

@@ -27,10 +27,10 @@ trait MigrateTrait
2727
*/
2828
protected function getMigrationFile(): string
2929
{
30-
if ($this->hasOption('class')) {
31-
$file = $this->getFileByClass();
30+
if ($class = $this->option('class')) {
31+
$file = $this->getFileByClass($class);
3232
} else {
33-
$file = $this->getMigrationPath().'/'.$this->argument('file');
33+
$file = $this->getMigrationPath().'/'.$this->option('file');
3434
}
3535

3636
if (!$file || !file_exists($file)) {
@@ -41,12 +41,12 @@ protected function getMigrationFile(): string
4141
}
4242

4343
/**
44+
* @param string|null $class
45+
*
4446
* @return string|null
4547
*/
46-
protected function getFileByClass()
48+
protected function getFileByClass(string $class = null)
4749
{
48-
$class = $this->option('class');
49-
5050
if (!$class) {
5151
return null;
5252
}
@@ -69,4 +69,32 @@ protected function checkRequirements()
6969
throw new RuntimeException('You must pass at least one argument "file" or "class"');
7070
}
7171
}
72+
73+
/**
74+
* @return void
75+
*/
76+
protected function setMigratorOutput()
77+
{
78+
// Works only for 5.7+
79+
if (method_exists($this->migrator, 'setOutput')) {
80+
$this->migrator->setOutput($this->getOutput());
81+
}
82+
}
83+
84+
/**
85+
* @param callable|null $when
86+
*
87+
* @return void
88+
*/
89+
protected function writeMigratorNotes(callable $when = null)
90+
{
91+
// Support for 5.2-5.6
92+
if (method_exists($this->migrator, 'getNotes')) {
93+
foreach ($this->migrator->getNotes() as $note) {
94+
if (null === $when || $when($note)) {
95+
$this->output->writeln($note);
96+
}
97+
}
98+
}
99+
}
72100
}

0 commit comments

Comments
 (0)