Как внедрить репозиторий в сервис в Symfony?
Мне нужно ввести два объекта в ImageService
. Одним из них является экземпляр Repository/ImageRepository
, который я получаю следующим образом:
$image_repository = $container->get('doctrine.odm.mongodb')
->getRepository('MycompanyMainBundle:Image');
Итак, как мне объявить об этом в файле services.yml? Вот эта услуга:
namespace Mycompany\MainBundle\Service\Image;
use Doctrine\ODM\MongoDB\DocumentRepository;
class ImageManager {
private $manipulator;
private $repository;
public function __construct(ImageManipulatorInterface $manipulator, DocumentRepository $repository) {
$this->manipulator = $manipulator;
$this->repository = $repository;
}
public function findAll() {
return $this->repository->findAll();
}
public function createThumbnail(ImageInterface $image) {
return $this->manipulator->resize($image->source(), 300, 200);
}
}
5 answers
Вот очищенное решение для тех, кто приходит из Google, как я:
Обновление: вот решение Symfony 2.6 (и выше):
services:
myrepository:
class: Doctrine\ORM\EntityRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- MyBundle\Entity\MyClass
myservice:
class: MyBundle\Service\MyService
arguments:
- "@myrepository"
Устаревшее решение (Symfony 2.5 и менее):
services:
myrepository:
class: Doctrine\ORM\EntityRepository
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- MyBundle\Entity\MyClass
myservice:
class: MyBundle\Service\MyService
arguments:
- "@myrepository"
Я нашел эту ссылку , и это сработало для меня:
parameters:
image_repository.class: Mycompany\MainBundle\Repository\ImageRepository
image_repository.factory_argument: 'MycompanyMainBundle:Image'
image_manager.class: Mycompany\MainBundle\Service\Image\ImageManager
image_manipulator.class: Mycompany\MainBundle\Service\Image\ImageManipulator
services:
image_manager:
class: %image_manager.class%
arguments:
- @image_manipulator
- @image_repository
image_repository:
class: %image_repository.class%
factory_service: doctrine.odm.mongodb
factory_method: getRepository
arguments:
- %image_repository.factory_argument%
image_manipulator:
class: %image_manipulator.class%
В случае, если вы не хотите определять каждый репозиторий как службу, начиная с версии 2.4
вы можете сделать следующее (default
- это имя менеджера сущностей):
@=service('doctrine.orm.default_entity_manager').getRepository('MycompanyMainBundle:Image')
2017 и Symfony 3.3+ сделал это намного проще.
Проверьте мой пост Как использовать репозиторий с Доктриной в качестве службы в Symfony для более общего описания.
Для вашего кода все, что вам нужно сделать, это использовать композицию над наследованием - один из ТВЕРДЫХ шаблонов.
1. Создайте собственный репозиторий без прямой зависимости от Доктрины
<?php
namespace MycompanyMainBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
use MycompanyMainBundle\Entity\Image;
class ImageRepository
{
private $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(Image::class);
}
// add desired methods here
public function findAll()
{
return $this->repository->findAll();
}
}
2. Добавьте регистрацию конфигурации с помощью PSR-4 на основе авторегистрация
# app/config/services.yml
services:
_defaults:
autowire: true
MycompanyMainBundle\:
resource: ../../src/MycompanyMainBundle
3. Теперь вы можете добавить любую зависимость в любом месте с помощью инъекции конструкции
use MycompanyMainBundle\Repository\ImageRepository;
class ImageService
{
public function __construct(ImageRepository $imageRepository)
{
$this->imageRepository = $imageRepository;
}
}
В моем случае основывается на ответе @Tomáš Votruba и этом вопросе Я предлагаю следующие подходы:
Без наследования
-
Создайте универсальный класс адаптера:
namespace AppBundle\Services; use Doctrine\ORM\EntityManagerInterface; class RepositoryServiceAdapter { private $repository=null; /** * @param EntityManagerInterface the Doctrine entity Manager * @param String $entityName The name of the entity that we will retrieve the repository */ public function __construct(EntityManagerInterface $entityManager,$entityName) { $this->repository=$entityManager->getRepository($entityName) } public function __call($name,$arguments) { if(empty($arrguments)){ //No arguments has been passed $this->repository->$name(); } else { //@todo: figure out how to pass the parameters $this->repository->$name(...$argument); } } }
-
Затем для каждой сущности Определите службу, например, в моем случае, чтобы определить (я использую php для определения служб symfony):
$container->register('ellakcy.db.contact_email',AppBundle\Services\Adapters\RepositoryServiceAdapter::class) ->serArguments([new Reference('doctrine'),AppBundle\Entity\ContactEmail::class]);
С наследованием
Тот же шаг 1, упомянутый выше
-
Расширить класс
RepositoryServiceAdapter
, например:namespace AppBundle\Service\Adapters; use Doctrine\ORM\EntityManagerInterface; use AppBundle\Entity\ContactEmail; class ContactEmailRepositoryServiceAdapter extends RepositoryServiceAdapter { public function __construct(EntityManagerInterface $entityManager) { parent::__construct($entityManager,ContactEmail::class); } }
-
Служба регистрации:
$container->register('ellakcy.db.contact_email',AppBundle\Services\Adapters\RepositoryServiceAdapter::class) ->serArguments([new Reference('doctrine')]);
В любом случае у вас есть хороший проверяемый способ функционального тестирования вашей базы данных, а также он помогает вам издеваться, если вы хотите провести модульное тестирование своей службы без необходимости слишком беспокоиться о том, как это сделать. Например, предположим, что у нас есть следующая служба:
//Namespace definitions etc etc
class MyDummyService
{
public function __construct(RepositoryServiceAdapter $adapter)
{
//Do stuff
}
}
И адаптер RepositoryServiceAdapter адаптирует следующий репозиторий:
//Namespace definitions etc etc
class SomeRepository extends \Doctrine\ORM\EntityRepository
{
public function search($params)
{
//Search Logic
}
}
Таким образом, вы можете легко имитировать/жестко кодировать/эмулировать поведение метода search
, определенного в SomeRepository
, высмеивая RepositoryServiceAdapter
в подходе без наследования или ContactEmailRepositoryServiceAdapter
в подходе наследования.