Symfony 4 autowire DataTransformerInterface - symfony

I want to build a new form type to handle one of my problem.
But, using this code :
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Entity hidden custom type class definition
*/
class EntityHiddenType extends AbstractType
{
/**
* #var DataTransformerInterface $transformer
*/
private $transformer;
/**
* Constructor
*
* #param DataTransformerInterface $transformer
*/
public function __construct(DataTransformerInterface $transformer)
{
$this->transformer = $transformer;
}
I got this error :
Cannot autowire service "App\Form\Type\EntityHiddenType": argument
"$transformer" of method "__construct()" references interface
"Symfony\Component\Form\DataTransformerInterface" but no such service
exists. Did you create a class that implements this interface?
I tried to put autowire to off, but I can't inject an interface right ?
Why I can't autowire this Symfony interface ?

If you have your DataTransformer in a own class (which implements DataTransformerInterface) you have to inject this class (your implementation) - not the interface.
see https://symfony.com/doc/current/form/data_transformers.html#using-the-transformer
you could of course aliasing the interface for your specific implementation - but then you only could have this one DataTransformer when injecting the Interface.

Because DataTransformerInterface is not autowirable.
Try this, to see autowiring classes/interfaces
bin/console debug:autowiring
The only way to make it work is to create a service class which implements DataTransformerInterface
Inject your new service by configuring it in service.yml

Related

Sylius - Inject Services into Resource Repositories

I want to add a feature to AdminUser so that an admin can only see products / orders etc. that are available in specific channels. The aim is to have an admin interface and use sylius as a multishop platform for different clients with different shops.
What I did so far:
I created a custom resource repository for products that overrides the createListQueryBuilder method (and registered it in _sylius.yaml of course).
Since I need symfonys security service and autowiring seems not to work for additional parameters in the repository constructor, I created a CompilerPass that adds a method call to set the security service (code below).
Problem:
The service gets set in the repository, but when the createListQueryBuilder method gets called it is null again - so there is not way to filter user specific.
It seems that during the call another instance of the repository class is used (spl_object_hash returns different values when setSecurity is called than when createListQueryBuilder is called).
Am I something missing or is there any other more sylius-way to add this feature?
Sylius Version 1.8.0
Code:
ProductRepositoryCompilerPass
namespace App\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Security\Core\Security;
/**
* Class ProductRepositoryCompilerPass
*
* This class injects the security service into the product repository for filtering.
* #package App\DependencyInjection
*/
class ProductRepositoryCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if($container->hasDefinition('sylius.repository.product')) {
$definition = $container->getDefinition('sylius.repository.product');
$definition->addMethodCall('setSecurity', [new Reference(Security::class)]);
}
}
}
Custom ProductRepository
namespace App\Repository\Product;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Security;
class ProductRepository extends \Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository
{
private $security;
public function __construct(EntityManager $entityManager, Mapping\ClassMetadata $class)
{
parent::__construct($entityManager, $class);
}
public function setSecurity(Security $security) {
$this->security = $security; // this gets called properly
}
public function createListQueryBuilder(string $locale, $taxonId = null): QueryBuilder
{
// $this->security is null here
...
}
}
Or you could remove dependency on security at repository level and create a security layer above repository.

Symfony 3 service configuration providing dynamic configuration

I would like to know how I could avoid setter injection under the following use case.
I have a collection of tagged services:
interface EmailFormatter
class CvEmailFormatter implements EmailFormatter
class RegistrationEmailFormatter implements EmailFormatter
class LostPasswordEmailFormatter implements EmailFormatter
I use a CompilerPass to inject these into a mailer service by invoking its addEmailFormatter() method.
Each EmailFormatter instance requires some configuration that must be retrieved at run time from the current user:
/**
* #var FormatterConfig[]
* #ORM\OneToMany(targetEntity="AppBundle\Entity\FormatterConfig", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
*/
protected $formatterConfigs;
Right now I am checking to see if the user has created a configuration for a particular EmailFormatter inside the mailer service's addEmailFormatter() method. If they have then setConfig() injects the configuration object. If no config exists yet the EmailFormatter does not get added at all.
public function addEmailFormatter(EmailFormatter $formatter, $alias)
{
$formatterConfig = $this->user->getFormatterConfig($alias);
if (null !== $formatterConfig) {
$this->formatters[$alias] = $formatter;
$formatter->setConfig($formatterConfig);
}
}
Could this be a legitimate use case given I am working with a mutable collection of services?

Can't extend Doctrine's Repository

I'm trying to make a custom Doctrine's ORM Repository and extend it but I can't find a way to make it work. So far this is what i have:
The original Repository
//AppBundle\Repository\LocaleRepository.php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation as DI;
class LocaleRepository extends EntityRepository
{
protected myCustomFunction(){
}
}
The extended Repository
//OfficeBundle\Repository\OfficeRepository.php
namespace OfficeBundle\Repository;
use AppBundle\Repository\LocaleRepository;
class OfficeRepository extends LocaleRepository
{
//Empty class
}
My entiy:
namespace OfficeBundle\Entity;
// some calls to traits
use Doctrine\ORM\Mapping as ORM;
/**
* Office
*
* #ORM\Table(name="office__office")
* #ORM\Entity(repositoryClass="OfficeBundle\Repository\OfficeRepository")
*/
class Office implements TranslatableInterface{
//...
}
And Finally the call:
$em = $this->getDoctrine()->getManager();
$this->getEntityManager();
$office=$em->getRepository('OfficeBundle:Office')->myCustomeFunction($slug);
This trows the exception:
Undefined method 'myCustomFunction'. The method name must start with either findBy or findOneBy!
If I place myCustomeFunction inside the OfficeRepository it works fine but it brings down the purpose of extendind the repository. Also, the repository loaded by the controller is the correct one, vardumping the class shows: 'OfficeBundle\Repository\OfficeRepository'.
Finally I'm using KNP DoctrineBehaviors(translatable) on the office entity.
You must make your method public if you are going to use it outside the repository class.
class LocaleRepository extends EntityRepository
{
public function myCustomFunction()
{
....
}
}

Creating Doctrine objects from a Behat Context class

Situation
I have a Symfony2 project. I want to create Doctrine objects during a Behat test. To that effect, I need to access the EntityManager from within my FeatureContext class.
My attempt
I have my FeatureContext class extend RawMinkContext, which in turn extends BehatContext.
I then try, as per the documentation to access the container, or the entitymanager.
class FeatureContext extends RawMinkContext
{
/**
* #Given /^I have some disciplines$/
*/
public function iHaveSomeDisciplines()
{
$em = $this->getEntityManager();
$container = $this->getContainer();
}
But neither of these work, because none of the classes FeatureContext inherits from have access to this. As far as I know, only Controller does.
Question
How can I get access to Doctrine from within my FooContext classes?
Inject the kernel into your context by:
Behat2
class FeatureContext extends RawMinkContext implements KernelAwareContext
Behat3
class FeatureContext extends RawMinkContext
{
use KernelDictionary
Then you can get the entity manager as follows:
$this->getKernel()->getContainer()->get('doctrine.orm.entity_manager');
Not sure how Behat tests work but is it possible to turn your class into a service and inject entity manager?

Injecting #session into EntityRepository

I wanted to override the default Doctrine\ORM\EntityRepository class in my Symfony2 project so that I can have access to the #session service so that all of my repositories have access to a certain session variable if it is set.
On investigation it appeared to be less simple than I had hoped, as the EntityRepository is instantiated from within the Doctrine\ORM\EntityManager, and this class is instantiated itself using a static "create" method.
I followed the answer in Injecting dependency into entity repository but have hit a roadblock in actually implementing the custom manager class (specifically where the answer's author says "But since you're making a custom entity manager, you can wire it up to the service container and inject whatever dependencies you need").
I have defined my overridden EntityManager class, with an overridden "create" function and have also overridden the "getRepository" function. It is in this function that I believe I need to add the session to the Repository as it is created using a "setSession" method on my overridden EntityRepository class, but I am unsure as to how to actually get the session into the manager in the first place, as the other constructor arguments for the EntityManager class (Connection $conn, Configuration $config, EventManager $eventManager) are supplied in the Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension "ormLoad" method.
I have also specified
doctrine.orm.entity_manager.class: Me\MyBundle\Doctrine\ORM\EntityManager
in my config.yml file.
How can I have Symfony use my custom EntityManager class when creating repositories, and inject the session into it as well?
Florian, here, explained how to create repository via service:
my_service:
class: Doctrine\Common\Persistence\ObjectRepository
factory_service: doctrine # this is an instance of Registry
factory_method: getRepository
arguments: [ %mytest.entity% ]
You could add calls to invoke setSession (as deferred DI):
my_service:
...
calls:
- [setSession, ["#session"]]
Is this you're trying to do?
I ended up going with something slightly different:
I overrode the doctrine.orm.entity_manager.class parameter with my custom class which simple extended the default class with an additional $session parameter (complete with getter and setter), along with overridden create and getRepository functions (to return instances of my class instead of the default.
I then overrode the EntityRepository class and implemented a "getSession" method which returned
$this->_em->getSession();
and finally, in a custom event listener which has access to the entity manager, I called
$this->entityManager->setSession($session);
which gave me access to the session from every repository.
In Symfony 4+ you can just make it a ServiceEntityRepository and with autowiring there's no need for any services.yaml changes.
namespace App\Repository;
use App\Entity\YourEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class YourRepository extends ServiceEntityRepository
{
private $session;
public function __construct(ManagerRegistry $registry, SessionInterface $session)
{
$this->session = $session;
parent::__construct($registry, YourEntity::class);
}
public function findSomethingUsingSession()
{
$someValue = $this->session->get('some_index');
// ...
}
}
Then in your controller (for example)
$repository = $this->getDoctrine()->getRepository(YourEntity::class);
$result = $repository->findSomethingUsingSession();
Or use dependency injection (recommended)
public function someAction(YourRepository $repository)
{
$result = $repository->findSomethingUsingSession();
// ...
}

Resources