2017-06-28 1 views
1

Vous essayez de mettre à niveau un projet Symfony 3.3.2 existant pour utiliser l'autowiring. Suivez les instructions ici: https://symfony.com/doc/current/service_container/3.3-di-changes.html#content_wrapper.Symfony autowiring ne collecte pas les services dans un chemin spécifié

Ce qui ne fonctionne pas pour moi est de pouvoir mettre tous les services dans un répertoire public.

Important: Mon fichier services.yml se trouve dans src/SiteBundle/Resources/config, et non dans app/config.

# src/SiteBundle/Resources/config/services.yml 
services: 
    _defaults: 
    autowire: true 
    autoconfigure: true 
    public: false 

    SiteBundle\Service: 
    resource: '../../Service' 
    public: true 

    CustomerOrderMasterRepository: 
    class: Doctrine\ORM\EntityRepository 
    factory: ["@doctrine.orm.entity_manager", get_repository] 
    arguments: 
     - SiteBundle\Entity\CustomerMaster 

    SiteBundle\Service\CustomersService: 
    arguments: 
     - '@doctrine.orm.entity_manager' 
     - '@CustomerMasterRepository' 

Si je fais ensuite une mise au point de la console: container | grep -i service, il affiche uniquement l'en-tête de sortie et le conteneur de service Symfony. Aucune des classes du répertoire src/SiteBundle/Service n'est détectée. Si j'ajoute --show-private, alors ils sont affichés (avec beaucoup d'autres choses).

Symfony Container Public and Private Services Service ID
Class name
1_bcf140bb848ef41617942628c8525b4872574e826d00fee6aaabbf2ede89fbb8
Symfony\Component\DependencyInjection\ServiceLocator
Psr\Container\ContainerInterface
alias for "service_container"
SiteBundle\Service\CustomerOrdersService
SiteBundle\Service\CustomerOrdersService
SiteBundle\Service\CustomersService
SiteBundle\Service\CustomersService
SiteBundle\Service\InventoryItemsService
SiteBundle\Service\InventoryItemsService
SiteBundle\Service\VendorOrdersService
SiteBundle\Service\VendorOrdersService
SiteBundle\Service\VendorsService
SiteBundle\Service\VendorsService
SiteBundle\Service\WorkOrdersService
SiteBundle\Service\WorkOrdersService
Symfony\Component\DependencyInjection\ContainerInterface
alias for "service_container" argument_resolver.service
Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver routing.loader.service
Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader security.authentication.rememberme.services.abstract
security.authentication.rememberme.services.persistent
Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices security.authentication.rememberme.services.simplehash
Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices service_container
Symfony\Component\DependencyInjection\ContainerInterface
service_locator.1712c3a50d1ec2c742b2ead0f03bb76c
Symfony\Component\DependencyInjection\ServiceLocator
service_locator.26ac001b3ede28481ac0de703666b4d7
Symfony\Component\DependencyInjection\ServiceLocator
service_locator.39e66930232432ca5ba91e98fdd8a17b
Symfony\Component\DependencyInjection\ServiceLocator
service_locator.6f24348b77840ec12a20c22a3f985cf7
Symfony\Component\DependencyInjection\ServiceLocator
service_locator.8925f20c49cbd61fcb37adf8c595459e
Symfony\Component\DependencyInjection\ServiceLocator
service_locator.b8d2046fb854cde05549fb309e1a80d2
alias for "1_bcf140bb848ef41617942628c8525b4872574e826d00fee6aaabbf2ede89fbb8"
service_locator.ceb8bbb9f48e8bfd1c8ec2d209eabdca
Symfony\Component\DependencyInjection\ServiceLocator

Si je commente le SiteBundle \ Service \ CustomerService définition, la mise au point: commande conteneur déclenche cette exception:

PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException: Cannot autowire service "SiteBundle\Entity\CustomerMasterRepository": argument "$em" of method "Doctrine\ORM\EntityRepository::__construct()" must have a type-hint or be given a value explicitly. Which, frankly, I can't make heads nor tails of.

Suggestions? Merci.

+0

Vérifiez la réponse de l'auteur ici: https://stackoverflow.com/questions/44616346/symfony-3-3-services-autoconfiguration#comment76382803_44616346 Peut-être le même problème. Je n'ai pas beaucoup abusé avec autowire. En attendant qu'il se stabilise. Mais il semble que vous ayez besoin de changer la façon dont le fichier est chargé. – Cerad

+0

Ce que Cerad a dit. Déplacez simplement votre fichier services.yml dans le dossier 'app/config'. –

+0

Juste un offtopic aux dépôts, vous pouvez [les enregistrer en tant que services aussi] (https://www.tomasvotruba.cz/blog/2017/10/16/how-to-use-repository-with-doctrine-as- service-dans-symfony /) –

Répondre

1

Pour faire un travail de référentiel personnalisé que je devais passer outre la méthode __construct pour fournir le EntityManager et le ClassMetadata comme indiqué ci-dessous:

app/config/services.yml

services: 
    _defaults: 
     autowire: true 
     autoconfigure: true 
     public: false 

    app.repository.channel: 
     class: \AppBundle\EntityRepository\ChannelRepository 

src/AppBundle/EntityRepository/ChannelRepository.php:

<?php 

namespace AppBundle\EntityRepository; 

use AppBundle\Entity\Channel; 
use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Mapping\ClassMetadata; 

class ChannelRepository extends RepositoryFactory 
{ 
    public function __construct(EntityManager $entityManager) 
    { 
     parent::__construct($entityManager, new ClassMetadata(Channel::class)); 
    } 

    public function getOneByName(string $name) 
    { 
     return $this->findOneBy(['name' => $name]); 
    } 
} 

src/AppBundle/Entité/Channel.php

<?php 

namespace AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="channel") 
* @ORM\Entity(repositoryClass="\AppBundle\EntityRepository\ChannelRepository") 
* 
*/ 
class Channel 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
}