Skip to content

Commit

Permalink
- ability to pass dependencies into Cycle repositories and mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Sep 7, 2019
1 parent c58a590 commit 8269900
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
18 changes: 6 additions & 12 deletions src/Bootloader/Cycle/CycleBootloader.php
Expand Up @@ -54,15 +54,17 @@ public function boot(Container $container, FinalizerInterface $finalizer, Schema
$container->bindInjector(Select::class, SelectInjector::class);

if ($schema !== null) {
$this->bootRepositories($container, $schema);
$this->bindRepositories($container, $schema);
}
}

/**
* Create container bindings to resolve repositories by they class names.
*
* @param Container $container
* @param SchemaInterface $schema
*/
public function bootRepositories(Container $container, SchemaInterface $schema)
public function bindRepositories(Container $container, SchemaInterface $schema)
{
foreach ($schema->getRoles() as $role) {
$repository = $schema->define($role, SchemaInterface::REPOSITORY);
Expand All @@ -72,16 +74,8 @@ public function bootRepositories(Container $container, SchemaInterface $schema)
}

// initiate all repository dependencies using factory method forwarded to ORM
$container->bindSingleton($repository, function (ORMInterface $orm) use ($role, $container, $repository) {
// to avoid cyclic dependency since ORM use factory to resolve needed dependencies
$binding = $container->getBindings()[$repository];
$container->removeBinding($repository);

try {
return $orm->getRepository($role);
} finally {
$container->bind($repository, $binding);
}
$container->bindSingleton($repository, function (ORMInterface $orm) use ($role) {
return $orm->getRepository($role);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Cycle/MigrateCommand.php
Expand Up @@ -83,6 +83,6 @@ public function perform(
}
}

$cycleBootloader->bootRepositories($container, new Schema($schema));
$cycleBootloader->bindRepositories($container, new Schema($schema));
}
}
2 changes: 1 addition & 1 deletion src/Command/Cycle/SyncCommand.php
Expand Up @@ -51,6 +51,6 @@ public function perform(
$this->writeln("\n<info>ORM Schema has been synchronized</info>");
}

$cycleBootloader->bootRepositories($container, new Schema($schema));
$cycleBootloader->bindRepositories($container, new Schema($schema));
}
}
2 changes: 1 addition & 1 deletion src/Command/Cycle/UpdateCommand.php
Expand Up @@ -43,6 +43,6 @@ public function perform(

$this->writeln("<info>done</info>");

$cycleBootloader->bootRepositories($container, new Schema($schema));
$cycleBootloader->bindRepositories($container, new Schema($schema));
}
}

0 comments on commit 8269900

Please sign in to comment.