mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
move RepositoryForDoctrineProvider logic to abstract class
This commit is contained in:
parent
74b2dbbe7b
commit
e69b5b8701
@ -0,0 +1,35 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Contract\Bridge;
|
||||
|
||||
abstract class AbstractRepositoryForDoctrineEntityProvider implements RepositoryForDoctrineEntityProviderInterface
|
||||
{
|
||||
final public function provideRepositoryForEntity(string $name): ?string
|
||||
{
|
||||
if ($this->isAlias($name)) {
|
||||
return $this->resoleFromAlias($name);
|
||||
}
|
||||
|
||||
return $this->provideRepositoriesForEntities()[$name] ?? null;
|
||||
}
|
||||
|
||||
private function isAlias(string $name): bool
|
||||
{
|
||||
return strpos($name, ':') !== false;
|
||||
}
|
||||
|
||||
private function resoleFromAlias(string $name): ?string
|
||||
{
|
||||
[$namespaceAlias, $simpleClassName] = explode(':', $name, 2);
|
||||
|
||||
$pattern = sprintf('/(%s{1}.*%s)/', $namespaceAlias, $simpleClassName);
|
||||
|
||||
foreach ($this->provideRepositoriesForEntities() as $key => $value) {
|
||||
if (preg_match($pattern, $key)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -5,4 +5,9 @@ namespace Rector\Contract\Bridge;
|
||||
interface RepositoryForDoctrineEntityProviderInterface
|
||||
{
|
||||
public function provideRepositoryForEntity(string $name): ?string;
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function provideRepositoriesForEntities(): array;
|
||||
}
|
||||
|
@ -2,43 +2,17 @@
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\DoctrineRepositoryAsService\Source;
|
||||
|
||||
use Rector\Contract\Bridge\RepositoryForDoctrineEntityProviderInterface;
|
||||
use Rector\Contract\Bridge\AbstractRepositoryForDoctrineEntityProvider;
|
||||
|
||||
final class RepositoryForDoctrineEntityProvider implements RepositoryForDoctrineEntityProviderInterface
|
||||
final class RepositoryForDoctrineEntityProvider extends AbstractRepositoryForDoctrineEntityProvider
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
* @inheritdoc
|
||||
*/
|
||||
private $map = [
|
||||
'AppBundle\Entity\Post' => 'AppBundle\Repository\PostRepository',
|
||||
];
|
||||
|
||||
public function provideRepositoryForEntity(string $name): ?string
|
||||
public function provideRepositoriesForEntities(): array
|
||||
{
|
||||
if ($this->isAlias($name)) {
|
||||
return $this->resoleFromAlias($name);
|
||||
}
|
||||
|
||||
return $this->map[$name] ?? null;
|
||||
}
|
||||
|
||||
private function isAlias(string $name): bool
|
||||
{
|
||||
return strpos($name, ':') !== false;
|
||||
}
|
||||
|
||||
private function resoleFromAlias(string $name): ?string
|
||||
{
|
||||
[$namespaceAlias, $simpleClassName] = explode(':', $name, 2);
|
||||
|
||||
$pattern = sprintf('/(%s{1}.*%s)/', $namespaceAlias, $simpleClassName);
|
||||
|
||||
foreach ($this->map as $key => $value) {
|
||||
if (preg_match($pattern, $key)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return [
|
||||
'AppBundle\Entity\Post' => 'AppBundle\Repository\PostRepository',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user