Skip to content

Commit

Permalink
Merge pull request #2 from netlogix/feature/console-output
Browse files Browse the repository at this point in the history
Feature/console output
  • Loading branch information
twojtylak authored Oct 7, 2019
2 parents 8355930 + f658fdd commit b369b56
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Classes/Command/MigrationsCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function migrateCommand(bool $quiet = false)

foreach ($unexecutedMigrations as $version => $migration) {
try {
$this->migrationExecutor->execute($migration);
$this->migrationExecutor->execute($migration, 'up', $this->output);
if (false === $quiet) {
$this->outputLine('Executed Migration "' . $version . '".');
}
Expand All @@ -86,7 +86,7 @@ public function executeCommand(string $version, string $direction = 'up')
{
try {
$migration = $this->migrationService->getMigrationByVersion($version);
$this->migrationExecutor->execute($migration, $direction);
$this->migrationExecutor->execute($migration, $direction, $this->output);
} catch (\Exception $exception) {
$this->handleException($exception);
}
Expand Down
22 changes: 22 additions & 0 deletions Classes/Domain/Handler/DefaultMigrationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

namespace Netlogix\Migrations\Domain\Handler;

use Neos\Flow\Cli\ConsoleOutput;
use Netlogix\Migrations\Domain\Model\DefaultMigration;
use Netlogix\Migrations\Domain\Model\Migration;

class DefaultMigrationHandler implements MigrationHandler
{

/**
* @var ConsoleOutput
*/
protected $output;

public function canExecute(Migration $migration): bool
{
return $migration instanceof DefaultMigration;
Expand All @@ -22,4 +29,19 @@ public function down(Migration $migration): void
{
$migration->down();
}

public function setConsoleOutput(?ConsoleOutput $consoleOutput = null): void
{
$this->output = $consoleOutput;
}

protected function outputLine(string $text, array $arguments = []): void
{
if (!$this->output) {
return;
}

$this->output->outputLine($text, $arguments);
}

}
4 changes: 3 additions & 1 deletion Classes/Domain/Handler/MigrationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Netlogix\Migrations\Domain\Handler;

use Neos\Flow\Cli\ConsoleOutput;
use Netlogix\Migrations\Domain\Model\Migration;
use Netlogix\Migrations\Domain\Model\MigrationInterface;

interface MigrationHandler
{
Expand All @@ -13,4 +13,6 @@ public function canExecute(Migration $migration): bool;
public function up(Migration $migration): void;

public function down(Migration $migration): void;

public function setConsoleOutput(?ConsoleOutput $consoleOutput = null): void;
}
7 changes: 4 additions & 3 deletions Classes/Domain/Service/FileSystemMigrationsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Netlogix\Migrations\Domain\Service;

use Doctrine\DBAL\Migrations\Finder\GlobFinder;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Package\PackageInterface;
use Neos\Flow\Package\PackageManager;
Expand Down Expand Up @@ -36,8 +35,10 @@ public function __construct(PackageManager $packageManager)
public function findMigrationFiles(): array
{
$classNames = [];
/** @var PackageInterface $package */
foreach ($this->packageManager->getAvailablePackages() as $package) {
/** @var PackageInterface[] $packages */
$packages = $this->packageManager->getFilteredPackages('available', 'Application');

foreach ($packages as $package) {
$path = Files::concatenatePaths([
$package->getPackagePath(),
'Migrations',
Expand Down
20 changes: 20 additions & 0 deletions Classes/Domain/Service/GlobFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Netlogix\Migrations\Domain\Service;

use Doctrine\DBAL\Migrations\Finder\AbstractFinder;

final class GlobFinder extends AbstractFinder
{

public function findMigrations($directory, $namespace = null)
{
$dir = $this->getRealPath($directory);

$files = glob(rtrim($dir, '/') . '/**/Version*.php');

return $this->loadMigrations($files, $namespace);
}

}
4 changes: 3 additions & 1 deletion Classes/Domain/Service/MigrationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Netlogix\Migrations\Domain\Service;

use Neos\Flow\Cli\ConsoleOutput;
use Neos\Flow\ObjectManagement\ObjectManager;
use Neos\Flow\Reflection\ReflectionService;
use Netlogix\Migrations\Domain\Handler\MigrationHandler;
Expand Down Expand Up @@ -40,14 +41,15 @@ public function __construct(
$this->versionLogger = $versionLogger;
}

public function execute(Migration $migration, $direction = 'up')
public function execute(Migration $migration, $direction = 'up', ?ConsoleOutput $consoleOutput = null)
{
foreach ($this->reflectionService->getAllImplementationClassNamesForInterface(MigrationHandler::class) as $handlerClassName) {

/** @var MigrationHandler $handler */
$handler = $this->objectManager->get($handlerClassName);

if ($handler->canExecute($migration)) {
$handler->setConsoleOutput($consoleOutput);
$result = $handler->{$direction}($migration);
$this->versionLogger->logMigration($migration, $direction);
return $result;
Expand Down
1 change: 1 addition & 0 deletions Classes/Domain/Service/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Netlogix\Migrations\Domain\Model\Migration;
use Netlogix\Migrations\Domain\Model\MigrationStatus;
use Netlogix\Migrations\Domain\Repository\MigrationStatusRepository;
use Netlogix\Migrations\Error\UnknownMigration;

Expand Down
2 changes: 0 additions & 2 deletions Classes/Domain/Service/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Netlogix\Migrations\Domain\Service;

use Netlogix\Migrations\Domain\Model\Migration;

class VersionResolver
{
public function extractVersion(string $migrationClassName): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declare(strict_types=1);

namespace Netlogix\Migrations\Tests\Unit\Domain\Service;
use Doctrine\DBAL\Migrations\Finder\GlobFinder;

use Neos\Flow\Package;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Tests\UnitTestCase;
Expand Down Expand Up @@ -32,17 +32,19 @@ protected function setUp()
->method('getPackagePath')
->willReturn('dummy/package/path/');

$packageManager->method('getAvailablePackages')
$packageManager->method('getFilteredPackages')
->willReturn([$dummyPackage]);
assert($packageManager instanceof PackageManager);

$this->fileSystemMigrationsResolver = new FileSystemMigrationsResolver($packageManager, new GlobFinder());
$this->fileSystemMigrationsResolver = new FileSystemMigrationsResolver($packageManager);
}

/**
* @test
*/
public function Can_return_empty_array()
{
xdebug_break();
$files = $this->fileSystemMigrationsResolver->findMigrationFiles();
$this->assertCount(0, $files);
}
Expand Down

0 comments on commit b369b56

Please sign in to comment.