Skip to content

Commit

Permalink
Merge pull request #350 from patchlevel/remove-deprecations
Browse files Browse the repository at this point in the history
remove deprecations
  • Loading branch information
DavidBadura authored Dec 31, 2022
2 parents 1201de1 + bab626d commit 4f74b47
Show file tree
Hide file tree
Showing 51 changed files with 238 additions and 1,628 deletions.
362 changes: 15 additions & 347 deletions baseline.xml

Large diffs are not rendered by default.

66 changes: 19 additions & 47 deletions src/Console/Command/ProjectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Patchlevel\EventSourcing\Console\Command;

use InvalidArgumentException;
use Patchlevel\EventSourcing\Console\InvalidArgumentGiven;
use Patchlevel\EventSourcing\Projection\MetadataAwareProjectionHandler;
use Patchlevel\EventSourcing\Projection\Projection;
use Patchlevel\EventSourcing\Projection\ProjectionHandler;
use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Symfony\Component\Console\Command\Command;

use function array_filter;
Expand All @@ -17,35 +15,39 @@
use function is_array;
use function is_string;
use function is_subclass_of;
use function sprintf;

abstract class ProjectionCommand extends Command
{
private ProjectionHandler $projectionHandler;
private readonly ProjectorRepository $projectorRepository;

public function __construct(ProjectionHandler $projectionHandler)
public function __construct(ProjectorRepository $projectorRepository)
{
parent::__construct();

$this->projectionHandler = $projectionHandler;
$this->projectorRepository = $projectorRepository;
}

protected function projectionHandler(mixed $projectionOption): ProjectionHandler
/**
* @return list<Projector>
*/
protected function projectors(mixed $projectionOption): array
{
$normalizedProjectionOption = $this->normalizeProjectionOption($projectionOption);

if (!$normalizedProjectionOption) {
return $this->projectionHandler;
return $this->projectorRepository->projectors();
}

return $this->filterProjectionInProjectionHandler(
$this->projectionHandler,
$normalizedProjectionOption
return array_values(
array_filter(
[...$this->projectorRepository->projectors()],
static fn (Projector $projection): bool => in_array($projection::class, $normalizedProjectionOption)
)
);
}

/**
* @return non-empty-array<class-string<Projection>>|null
* @return non-empty-array<class-string<Projector>>|null
*/
private function normalizeProjectionOption(mixed $option): ?array
{
Expand All @@ -54,14 +56,14 @@ private function normalizeProjectionOption(mixed $option): ?array
}

if (!is_array($option)) {
throw new InvalidArgumentGiven($option, 'class-string<' . Projection::class . '>[]');
throw new InvalidArgumentGiven($option, 'class-string<' . Projector::class . '>[]');
}

$result = [];

foreach ($option as $entry) {
if (!is_string($entry) || !is_subclass_of($entry, Projection::class)) {
throw new InvalidArgumentGiven($entry, 'class-string<' . Projection::class . '>');
if (!is_string($entry) || !is_subclass_of($entry, Projector::class)) {
throw new InvalidArgumentGiven($entry, 'class-string<' . Projector::class . '>');
}

$result[] = $entry;
Expand All @@ -73,34 +75,4 @@ private function normalizeProjectionOption(mixed $option): ?array

return $result;
}

/**
* @param non-empty-array<class-string<Projection>> $onlyProjections
*/
private function filterProjectionInProjectionHandler(
ProjectionHandler $projectionHandler,
array $onlyProjections
): MetadataAwareProjectionHandler {
if (!$projectionHandler instanceof MetadataAwareProjectionHandler) {
throw new InvalidArgumentException(
sprintf(
'Filtering projections is only supported with "%s", but "%s" was used.',
MetadataAwareProjectionHandler::class,
$projectionHandler::class
)
);
}

$projections = array_values(
array_filter(
[...$projectionHandler->projections()],
static fn (Projection $projection): bool => in_array($projection::class, $onlyProjections)
)
);

return new MetadataAwareProjectionHandler(
$projections,
$projectionHandler->metadataFactory()
);
}
}
21 changes: 20 additions & 1 deletion src/Console/Command/ProjectionCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Patchlevel\EventSourcing\Console\Command;

use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -16,6 +20,17 @@
)]
final class ProjectionCreateCommand extends ProjectionCommand
{
private readonly ProjectorResolver $projectorResolver;

public function __construct(
ProjectorRepository $projectorRepository,
ProjectorResolver $projectorResolver = new MetadataProjectorResolver()
) {
parent::__construct($projectorRepository);

$this->projectorResolver = $projectorResolver;
}

protected function configure(): void
{
$this
Expand All @@ -26,7 +41,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$console = new OutputStyle($input, $output);

$this->projectionHandler($input->getOption('projection'))->create();
(new ProjectorHelper($this->projectorResolver))->createProjection(
...$this->projectors(
$input->getOption('projection')
)
);

$console->success('projection created');

Expand Down
21 changes: 20 additions & 1 deletion src/Console/Command/ProjectionDropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Patchlevel\EventSourcing\Console\Command;

use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -16,6 +20,17 @@
)]
final class ProjectionDropCommand extends ProjectionCommand
{
private readonly ProjectorResolver $projectorResolver;

public function __construct(
ProjectorRepository $projectorRepository,
ProjectorResolver $projectorResolver = new MetadataProjectorResolver()
) {
parent::__construct($projectorRepository);

$this->projectorResolver = $projectorResolver;
}

protected function configure(): void
{
$this
Expand All @@ -26,7 +41,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$console = new OutputStyle($input, $output);

$this->projectionHandler($input->getOption('projection'))->drop();
(new ProjectorHelper($this->projectorResolver))->dropProjection(
...$this->projectors(
$input->getOption('projection')
)
);

$console->success('projection deleted');

Expand Down
67 changes: 43 additions & 24 deletions src/Console/Command/ProjectionRebuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use Patchlevel\EventSourcing\Pipeline\Middleware\UntilEventMiddleware;
use Patchlevel\EventSourcing\Pipeline\Pipeline;
use Patchlevel\EventSourcing\Pipeline\Source\StoreSource;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectionHandlerTarget;
use Patchlevel\EventSourcing\Projection\ProjectionHandler;
use Patchlevel\EventSourcing\Store\PipelineStore;
use Patchlevel\EventSourcing\Store\Store;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectorRepositoryTarget;
use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Patchlevel\EventSourcing\Store\StreamableStore;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -29,42 +32,55 @@
)]
final class ProjectionRebuildCommand extends ProjectionCommand
{
private Store $store;
private StreamableStore $store;
private ProjectorResolver $projectorResolver;

public function __construct(Store $store, ProjectionHandler $projectionHandler)
{
parent::__construct($projectionHandler);
public function __construct(
StreamableStore $store,
ProjectorRepository $projectorRepository,
ProjectorResolver $projectorResolver = new MetadataProjectorResolver()
) {
parent::__construct($projectorRepository);

$this->store = $store;
$this->projectorResolver = $projectorResolver;
}

protected function configure(): void
{
$this
->addOption('recreate', 'r', InputOption::VALUE_NONE, 'drop and create projections')
->addOption('until', 'u', InputOption::VALUE_REQUIRED, 'create the projection up to a point in time [2017-02-02 12:00]')
->addOption('projection', 'p', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'run only for specific projections [FQCN]');
->addOption(
'until',
'u',
InputOption::VALUE_REQUIRED,
'create the projection up to a point in time [2017-02-02 12:00]'
)
->addOption(
'projection',
'p',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'run only for specific projections [FQCN]'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$console = new OutputStyle($input, $output);

$store = $this->store;

if (!$store instanceof PipelineStore) {
$console->error('store is not supported');

return 1;
}

$projectionHandler = $this->projectionHandler($input->getOption('projection'));
$projectors = $this->projectors($input->getOption('projection'));

if (InputHelper::bool($input->getOption('recreate'))) {
$projectionHandler->drop();
(new ProjectorHelper($this->projectorResolver))->dropProjection(
...$projectors
);

$console->success('projection schema deleted');

$projectionHandler->create();
(new ProjectorHelper($this->projectorResolver))->createProjection(
...$projectors
);

$console->success('projection schema created');
}

Expand All @@ -75,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (is_string($until)) {
try {
$date = new DateTimeImmutable($until);
} catch (Throwable $exception) {
} catch (Throwable) {
$console->error(sprintf('date "%s" not supported. the format should be "2017-02-02 12:00"', $until));

return 1;
Expand All @@ -85,8 +101,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$pipeline = new Pipeline(
new StoreSource($store),
new ProjectionHandlerTarget($projectionHandler),
new StoreSource($this->store),
new ProjectorRepositoryTarget(
new InMemoryProjectorRepository($projectors),
$this->projectorResolver
),
$middlewares
);

Expand Down
15 changes: 5 additions & 10 deletions src/Console/Command/SchemaCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
use Patchlevel\EventSourcing\Console\InputHelper;
use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Schema\DryRunSchemaDirector;
use Patchlevel\EventSourcing\Schema\DryRunSchemaManager;
use Patchlevel\EventSourcing\Schema\SchemaDirector;
use Patchlevel\EventSourcing\Schema\SchemaManager;
use Patchlevel\EventSourcing\Store\Store;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -23,14 +20,12 @@
)]
final class SchemaCreateCommand extends Command
{
private Store $store;
private SchemaManager|SchemaDirector $schemaDirector;
private SchemaDirector $schemaDirector;

public function __construct(Store $store, SchemaManager|SchemaDirector $schemaDirector)
public function __construct(SchemaDirector $schemaDirector)
{
parent::__construct();

$this->store = $store;
$this->schemaDirector = $schemaDirector;
}

Expand All @@ -47,13 +42,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dryRun = InputHelper::bool($input->getOption('dry-run'));

if ($dryRun) {
if (!$this->schemaDirector instanceof DryRunSchemaManager && !$this->schemaDirector instanceof DryRunSchemaDirector) {
if (!$this->schemaDirector instanceof DryRunSchemaDirector) {
$console->error('SchemaDirector dont support dry-run');

return 1;
}

$actions = $this->schemaDirector->dryRunCreate($this->store);
$actions = $this->schemaDirector->dryRunCreate();

foreach ($actions as $action) {
$output->writeln($action);
Expand All @@ -62,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$this->schemaDirector->create($this->store);
$this->schemaDirector->create();

$console->success('schema created');

Expand Down
Loading

0 comments on commit 4f74b47

Please sign in to comment.