2
votes

So, I created new Bundle in Symfony and now, I want to have a config for that bundle. It looks something like this:

App\DireSolutions\TestUI\Resources\config\services.yaml

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

  dire_solutions.ui_test.simple_service:
    public: true
    class: App\DireSolutions\TestUI\Service\SimpleService
    arguments:
      $client_id: '123'
      $client_secret: 'secret'

App\DireSolutions\TestUI\DependencyInjection\DireSolutionsTestUIConfiguration.php

class DireSolutionsTestUIConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('dire_solutions_test_ui');

        $rootNode
            ->children()
            ->arrayNode('twitter')
            ->children()
            ->integerNode('client_id')->end()
            ->scalarNode('client_secret')->end()
            ->end()
            ->end()// twitter
            ->end();

        return $treeBuilder;
    }
}

App\DireSolutions\TestUI\DependencyInjection\DireSolutionsTestUIExtension.php

class DireSolutionsTestUIExtension extends Extension
{
    /**
     * @param array $configs
     * @param ContainerBuilder $container
     * @throws \Exception
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../Resources/config')
        );
        $loader->load('services.yaml');

        $configuration = new DireSolutionsTestUIConfiguration();

        $config = $this->processConfiguration($configuration, $configs);

        /** @var Definition $definition */
        $definition = $container->getDefinition('dire_solutions.ui_test.simple_service');

        $definition->setArgument('$client_id', $config['twitter']['client_id']);
        $definition->setArgument('$client_secret', $config['twitter']['client_secret']);
    }

}

App\DireSolutions\TestUI\Service\SimpleService.php

class SimpleService
{

    /**
     * SimpleService constructor.
     * @param ParameterBagInterface $params
     * @param string $client_id
     * @param string $client_secret
     */
    public function __construct($client_id, $client_secret)
    {
        dump($client_id);
        dump($client_secret);
        exit;
    }
}

With this configuration I get Cannot autowire service "App\DireSolutions\TestUI\Service\SimpleService": argument "$client_id" of method "__construct()" has no type-hint, you should configure its value explicitly.

Whatever I change, I cannot manually wire these arguments into the service.

1
Is your bundle under the src directory? If so then your main application is trying to autowire it. You will need to exclude it or move the source somewhere else. Also, real bundles will have the prefix Bundle somewhere i.e. MyBundle\Service\SomeService. App does not make sense as a bundle name. Doubtful your extension is even being called. - Cerad
@Cerad Yes, it is a Bundle and it has its own services.yaml, when I change something, it gets called, but then it fails over and over again when I try to manually wire the arguments that are strings. - Vulovic Vukasin
Seems very unlikely. What does your entry in config/bundles.php look like? Where does the bundle source code live? And why are you trying to autowire a bundle's services when the recommendation is to use manual wiring? - Cerad

1 Answers

0
votes

Change your services.yaml to

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

  App\DireSolutions\TestUI\Service\SimpleService:
    arguments:
      $client_id: "123"
      $client_secret: "secret"

If that doesn't work you can try to explicitly bind arguments with

services:
      _defaults:
        autowire: true
        autoconfigure: true
        public: false
        bind: 
          $client_id: "123"
          $client_secret: "secret"

bind allows even to use php built-in types like int, string etc.

The last option is to explicitly define service with an alias which may be helpful in some cases.

also change your

$definition = $container>getDefinition('dire_solutions.ui_test.simple_service');

to

$definition = $container>getDefinition('App\DireSolutions\TestUI\Service\SimpleService');

or create an alias as I mentioned above.