-
-
Notifications
You must be signed in to change notification settings - Fork 61
allow extending DIRepositoryFinder with custom filtering #741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b6e32ee
2dd6a17
312cc11
5858e8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Bridges\NetteDI; | ||
|
||
|
||
use Nette\DI\ContainerBuilder; | ||
use Nette\DI\Definitions\FactoryDefinition; | ||
use Nette\DI\Definitions\ServiceDefinition; | ||
use Nextras\Orm\Entity\IEntity; | ||
use Nextras\Orm\Exception\InvalidStateException; | ||
use Nextras\Orm\Model\IModel; | ||
use Nextras\Orm\Model\Model; | ||
use Nextras\Orm\Repository\IRepository; | ||
use ReflectionClass; | ||
|
||
|
||
class FilteredDIRepositoryFinder extends DIRepositoryFinder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced this should be in Orm, it is a highly subjective use case. It would be ok to have it locally in your project, wouldn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, yes/maybe and no. FilteredDIRepositoryFinder may be subjective and I don't insist this particular solution should be in Orm. I think however, that if Orm supports multiple solutions then it should also support auto-registering repositories that work with multiple solutions. FilteredDIRepositoryFinder is just a proposal for a solution that works with minimal configuration because it reuses the model namespace. |
||
{ | ||
|
||
/** | ||
* @param class-string<IModel> $modelClass | ||
*/ | ||
public function __construct( | ||
private readonly string $modelClass, | ||
private readonly ContainerBuilder $builder, | ||
OrmExtension $extension | ||
) | ||
{ | ||
if ($this->modelClass === Model::class) { | ||
throw new InvalidStateException('Your model has to inherit from ' . Model::class . '. Use compiler extension configuration - model key.'); | ||
} | ||
|
||
parent::__construct($this->modelClass, $builder, $extension); | ||
} | ||
|
||
|
||
protected function findRepositories(): array | ||
{ | ||
$reflection = new ReflectionClass($this->modelClass); | ||
$modelClassNamespace = $reflection->getNamespaceName(); | ||
|
||
$types = $this->builder->findByType(IRepository::class); | ||
$filteredTypes = []; | ||
|
||
foreach ($types as $serviceName => $serviceDefinition) { | ||
$serviceName = (string) $serviceName; | ||
|
||
/** @var class-string<IRepository<IEntity>> $type */ | ||
$type = $serviceDefinition->getType(); | ||
|
||
$isSupportedDefinition = | ||
$serviceDefinition instanceof FactoryDefinition || | ||
$serviceDefinition instanceof ServiceDefinition || | ||
$serviceDefinition instanceof \Nette\DI\ServiceDefinition; | ||
|
||
if ($isSupportedDefinition === false) { | ||
throw new InvalidStateException( | ||
"It seems DI defined repository of type '$type' is not defined as one of supported DI services. | ||
Orm can only work with ServiceDefinition or FactoryDefinition services.", | ||
); | ||
} | ||
|
||
if ($type === null) { | ||
continue; | ||
} | ||
|
||
if (str_starts_with($type, $modelClassNamespace)) { | ||
$filteredTypes[$serviceName] = $serviceDefinition; | ||
} | ||
} | ||
|
||
return $filteredTypes; | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.