0
votes

I migrate symfony 3 to symfony 4.3 and my custom bundle moved to: src/Bundles/FooBundle.

Strucutre FooBundle: - DependencyInjection - Configuration.php - FooExtension.php - Event - Model - Resources - config - services.yml - Service - Twig - Exception - FooBundle.php

And files Bundles/FooBundle/Resources/config/servies.yaml

services:
  foo:
    class: App\Bundles\FooBundle\Service\Foo
    arguments:
      - '%foo.argument1%'
      - '%foo.argument2%'
      - '%foo.argument3%'
  foo.listener.example:
    class: App\Bundles\FooBundle\Event\Listener\ExampleListener
    arguments: ['@annotations.reader']
    tags:
      - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
  foo.filter:
    class: App\Bundles\FooBundle\Filter\FilterConverter
    tags:
      - { name: request.param_converter }

Bundles/FooBundle/DependencyInjection/Configuration.php

<?php

namespace App\Bundles\FooBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('foo');
        $treeBuilder->getRootNode()
            ->children()
            ->scalarNode('argument1')->isRequired()->end()
            ->scalarNode('argument2')->isRequired()->end()
            ->scalarNode('argument3')->isRequired()->end()
            ->end();

        return $treeBuilder;
    }
}

Bundles/FooBundle/DependencyInjection/FooExtension.php

<?php

namespace App\Bundles\FooBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration.
 *
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
 */
class FooExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('foo.argument1', $config['page_field_name']);
        $container->setParameter('foo.argument2', $config['per_page_field_name']);
        $container->setParameter('foo.argument3', $config['sort_field_name']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

And bundle registration: config/bundles.php

App\Bundles\FooBundle\FooBundle::class => ['all' => true],

configuration package:

config/packages/foo.yam

foo:
  argument1: test1
  argument2: test2
  argument3: test3

Done... Run app and error: Cannot autowire service "App\Bundles\FooBundle\Service\Foo": argument "$argument1" of method "__construct()" has no type-hint, you should configure its value explicitly.

But, when I add conf in config/services.yaml:

App\Bundles\FooBundle\Service\Foo:
  arguments:
    - '%foo.argument1%'
    - '%foo.argument2%'
    - '%foo.argument3%'

That working...

Question: why is the bundle service not working?

1

1 Answers

1
votes

I guess the problem is that the default configuration in symfony add autowireing to all classes under the App namespace.

Check the configuration under config/services.yml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

You can remove this lines:

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

And the problem should disappear.