-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
schema manager - schema browser, transaction support, conditions
- Loading branch information
Showing
57 changed files
with
1,797 additions
and
694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\QueryBuilder\Schema\Diff\Browser; | ||
|
||
use MakinaCorpus\QueryBuilder\Schema\Diff\Condition\AbstractCondition; | ||
use MakinaCorpus\QueryBuilder\Schema\Diff\SchemaTransaction; | ||
use MakinaCorpus\QueryBuilder\Schema\Diff\Transaction\AbstractNestedSchemaTransaction; | ||
|
||
class ChangeLogBrowser | ||
{ | ||
/** @var ChangeLogVisitor[] */ | ||
private array $visitors = []; | ||
|
||
public function addVisitor(ChangeLogVisitor $visitor): void | ||
{ | ||
$this->visitors[] = $visitor; | ||
} | ||
|
||
public function browse(SchemaTransaction $transaction): void | ||
{ | ||
try { | ||
foreach ($this->visitors as $visitor) { | ||
$visitor->start($transaction); | ||
} | ||
|
||
foreach ($transaction->getChangeLog()->getAll() as $change) { | ||
if ($change instanceof AbstractNestedSchemaTransaction) { | ||
$this->reduceNested($change, 1, $this->visitors); | ||
} else { | ||
foreach ($this->visitors as $visitor) { | ||
$visitor->apply($change); | ||
} | ||
} | ||
} | ||
|
||
foreach ($this->visitors as $visitor) { | ||
$visitor->stop($transaction); | ||
} | ||
} catch (\Throwable $error) { | ||
foreach ($this->visitors as $visitor) { | ||
$visitor->error($transaction, $error); | ||
} | ||
|
||
throw $error; | ||
} | ||
} | ||
|
||
/** | ||
* @param AbstractCondition[] $conditions | ||
*/ | ||
protected function evaluateConditions(array $conditions, ChangeLogVisitor $visitor): bool | ||
{ | ||
foreach ($conditions as $condition) { | ||
if (!$visitor->evaluate($condition)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param ChangeLogVisitor[] $visitors | ||
*/ | ||
protected function reduceNested(AbstractNestedSchemaTransaction $new, int $depth, array $visitors) | ||
{ | ||
if ($conditions = $new->getConditions()) { | ||
$candidates = []; | ||
|
||
foreach ($visitors as $visitor) { | ||
\assert($visitor instanceof ChangeLogVisitor); | ||
|
||
if ($this->evaluateConditions($conditions, $visitor)) { | ||
$candidates[] = $visitor; | ||
} else { | ||
$visitor->skip($new, $depth); | ||
} | ||
} | ||
|
||
if ($candidates) { | ||
$this->browseNested($new, $depth, $candidates); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param ChangeLogVisitor[] $visitors | ||
*/ | ||
protected function browseNested(AbstractNestedSchemaTransaction $new, int $depth, array $visitors): void | ||
{ | ||
foreach ($visitors as $visitor) { | ||
$visitor->enter($new, $depth); | ||
} | ||
|
||
foreach ($new->getChangeLog()->getAll() as $change) { | ||
if ($change instanceof AbstractNestedSchemaTransaction) { | ||
$this->reduceNested($change, $depth + 1, $visitors); | ||
} else { | ||
foreach ($visitors as $visitor) { | ||
$visitor->apply($change); | ||
} | ||
} | ||
} | ||
|
||
foreach ($visitors as $visitor) { | ||
$visitor->leave($new, $depth); | ||
} | ||
} | ||
} |
Oops, something went wrong.