Skip to content

Commit

Permalink
Split and move around exceptions, make MigrationException an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkl578 committed May 6, 2018
1 parent e95dacb commit 3e40819
Show file tree
Hide file tree
Showing 38 changed files with 370 additions and 215 deletions.
9 changes: 0 additions & 9 deletions lib/Doctrine/Migrations/AbortMigrationException.php

This file was deleted.

13 changes: 8 additions & 5 deletions lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\SkipMigration;
use function sprintf;

abstract class AbstractMigration
Expand Down Expand Up @@ -73,22 +76,22 @@ public function warnIf(bool $condition, string $message = '') : void
}

/**
* @throws AbortMigrationException
* @throws AbortMigration
*/
public function abortIf(bool $condition, string $message = '') : void
{
if ($condition) {
throw new AbortMigrationException($message ?: 'Unknown Reason');
throw new AbortMigration($message ?: 'Unknown Reason');
}
}

/**
* @throws SkipMigrationException
* @throws SkipMigration
*/
public function skipIf(bool $condition, string $message = '') : void
{
if ($condition) {
throw new SkipMigrationException($message ?: 'Unknown Reason');
throw new SkipMigration($message ?: 'Unknown Reason');
}
}

Expand Down Expand Up @@ -134,6 +137,6 @@ protected function throwIrreversibleMigrationException(?string $message = null)
$message = 'This migration is irreversible and cannot be reverted.';
}

throw new IrreversibleMigrationException($message);
throw new IrreversibleMigration($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\FileAlreadyLoaded;
use Doctrine\Migrations\Configuration\Exception\NotValid;
use Doctrine\Migrations\Exception\MigrationException;
use InvalidArgumentException;
use function dirname;
use function file_exists;
Expand Down Expand Up @@ -44,7 +46,7 @@ protected function setConfiguration(array $config) : void
if (! in_array($configurationKey, self::ALLOWED_CONFIGURATION_KEYS, true)) {
$msg = sprintf('Migrations configuration key "%s" does not exist.', $configurationKey);

throw MigrationException::configurationNotValid($msg);
throw NotValid::new($msg);
}
}

Expand Down Expand Up @@ -108,15 +110,15 @@ private function setMigrationOrganization(string $migrationOrganization) : void
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
$msg = 'Unknown ' . var_export($migrationOrganization, true) . ' for configuration "organize_migrations".';
throw MigrationException::configurationNotValid($msg);
throw NotValid::new($msg);
}
}

/** @throws MigrationException */
public function load(string $file) : void
{
if ($this->loaded) {
throw MigrationException::configurationFileAlreadyLoaded();
throw FileAlreadyLoaded::new();
}

$path = getcwd() . '/' . $file;
Expand Down
22 changes: 14 additions & 8 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired;
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder;
use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
use Doctrine\Migrations\Exception\MigrationClassNotFound;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\MigrationsDirectoryRequired;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\FileQueryWriter;
use Doctrine\Migrations\Finder\MigrationDeepFinder;
use Doctrine\Migrations\Finder\MigrationFinder;
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\OutputWriter;
use Doctrine\Migrations\QueryWriter;
use Doctrine\Migrations\Version;
Expand Down Expand Up @@ -119,11 +125,11 @@ public function areMigrationsOrganizedByYearAndMonth() : bool
public function validate() : void
{
if (! $this->migrationsNamespace) {
throw MigrationException::migrationsNamespaceRequired();
throw MigrationsNamespaceRequired::new();
}

if (! $this->migrationsDirectory) {
throw MigrationException::migrationsDirectoryRequired();
throw MigrationsDirectoryRequired::new();
}
}

Expand Down Expand Up @@ -224,7 +230,7 @@ public function setMigrationsFinder(MigrationFinder $finder) : void
{
if (($this->migrationsAreOrganizedByYear || $this->migrationsAreOrganizedByYearAndMonth)
&& ! ($finder instanceof MigrationDeepFinder)) {
throw MigrationException::configurationIncompatibleWithFinder(
throw ParameterIncompatibleWithFinder::new(
'organize-migrations',
$finder
);
Expand All @@ -247,7 +253,7 @@ public function registerMigration(string $version, string $class) : Version
$this->ensureMigrationClassExists($class);

if (isset($this->migrations[$version])) {
throw MigrationException::duplicateMigrationVersion(
throw DuplicateMigrationVersion::new(
$version,
get_class($this->migrations[$version])
);
Expand Down Expand Up @@ -291,7 +297,7 @@ public function getVersion(string $version) : Version
$this->loadMigrationsFromDirectory();

if (! isset($this->migrations[$version])) {
throw MigrationException::unknownMigrationVersion($version);
throw UnknownMigrationVersion::new($version);
}

return $this->migrations[$version];
Expand Down Expand Up @@ -644,7 +650,7 @@ protected function connect() : bool
private function ensureOrganizeMigrationsIsCompatibleWithFinder() : void
{
if (! ($this->migrationFinder instanceof MigrationDeepFinder)) {
throw MigrationException::configurationIncompatibleWithFinder(
throw ParameterIncompatibleWithFinder::new(
'organize-migrations',
$this->migrationFinder
);
Expand Down Expand Up @@ -683,7 +689,7 @@ private function shouldExecuteMigration(
private function ensureMigrationClassExists(string $class) : void
{
if (! class_exists($class)) {
throw MigrationException::migrationClassNotFound(
throw MigrationClassNotFound::new(
$class,
$this->getMigrationsNamespace()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use Doctrine\Migrations\Exception\MigrationException;

interface ConfigurationException extends MigrationException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;
use function sprintf;

final class FileAlreadyLoaded extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self(
sprintf(
'Migrations configuration file already loaded'
),
8
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class MigrationsNamespaceRequired extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self(
'Migrations namespace must be configured in order to use Doctrine migrations.',
2
);
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Exception/NotValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class NotValid extends LogicException implements ConfigurationException
{
public static function new(string $message) : self
{
return new self($message, 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use Doctrine\Migrations\Finder\MigrationFinder;
use LogicException;
use function get_class;
use function sprintf;

final class ParameterIncompatibleWithFinder extends LogicException implements ConfigurationException
{
public static function new(string $configurationParameterName, MigrationFinder $finder) : self
{
return new self(
sprintf(
'Configuration-parameter "%s" cannot be used with finder of type "%s"',
$configurationParameterName,
get_class($finder)
),
9
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class YamlNotAvailable extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self(
'Unable to load yaml configuration files, please run '
. '`composer require symfony/yaml` to load yaml configuration files.'
);
}
}
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Configuration/JsonConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\NotValid;
use function file_get_contents;
use function json_decode;

Expand All @@ -16,7 +16,7 @@ protected function doLoad(string $file) : void
$config = json_decode(file_get_contents($file), true);

if ($config === false) {
throw MigrationException::configurationNotValid('Configuration is not valid JSON.');
throw NotValid::new('Configuration is not valid JSON.');
}

if (isset($config['migrations_directory'])) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Configuration/XmlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\NotValid;
use DOMDocument;
use const DIRECTORY_SEPARATOR;
use const LIBXML_NOCDATA;
Expand All @@ -27,7 +27,7 @@ protected function doLoad(string $file) : void
if (! $xml->schemaValidate($xsdPath)) {
libxml_clear_errors();

throw MigrationException::configurationNotValid('XML configuration did not pass the validation test.');
throw NotValid::new('XML configuration did not pass the validation test.');
}

$xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/Migrations/Configuration/YamlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\NotValid;
use Doctrine\Migrations\Configuration\Exception\YamlNotAvailable;
use Symfony\Component\Yaml\Yaml;
use function class_exists;
use function file_get_contents;
Expand All @@ -18,13 +19,13 @@ class YamlConfiguration extends AbstractFileConfiguration
protected function doLoad(string $file) : void
{
if (! class_exists(Yaml::class)) {
throw MigrationException::yamlConfigurationNotAvailable();
throw YamlNotAvailable::new();
}

$config = Yaml::parse(file_get_contents($file));

if (! is_array($config)) {
throw MigrationException::configurationNotValid('Configuration is not valid YAML.');
throw NotValid::new('Configuration is not valid YAML.');
}

if (isset($config['migrations_directory'])) {
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/Migrations/Exception/AbortMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use RuntimeException;

final class AbortMigration extends RuntimeException implements ControlException
{
}
22 changes: 22 additions & 0 deletions lib/Doctrine/Migrations/Exception/AlreadyAtVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use RuntimeException;
use function sprintf;

final class AlreadyAtVersion extends RuntimeException implements MigrationException
{
public static function new(string $version) : self
{
return new self(
sprintf(
'Database is already at version %s',
$version
),
6
);
}
}
9 changes: 9 additions & 0 deletions lib/Doctrine/Migrations/Exception/ControlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

interface ControlException extends MigrationException
{
}
Loading

0 comments on commit 3e40819

Please sign in to comment.