pepper
pepper

Reputation: 1772

Importing files when using PHP configuration files in Symfony

I have a Symfony's services.yaml which defines a "basic" container configuration and import di.php files to configure some specific services.

imports:
  - { resource: '../src/**/di.php' }

services:
  _defaults:
    autowire: true
    autoconfigure: true

App\:
  resource: '../src/'
    exclude:
      - '../src/**/di.php'
      - '../src/**/Entity/'
      - '../src/**/Model/'
      - '../src/Authorization/AuthorizationProcessor/'
      - '../src/Kernel.php'

For instance, here, I explicitly exclude src/Authorization/Processor folder because it has a di.php file that holds service definitions for this specific namespace.

This approach works.

But when I replace the services.yaml with services.php

<?php
return static function (ContainerConfigurator $di): void {
    $di->import('../src/**/di.php');

    $services = $di->services()
                   ->defaults()
                   ->autowire()
                   ->autoconfigure();

    $services->load('App\\', '../src/')
             ->exclude([
                 '../src/**/di.php',
                 '../src/**/Entity/',
                 '../src/**/Model/',
                 '../src/Authorization/AuthorizationProcessor/',
                 '../src/Kernel.php',
             ]);

To me, it looks the same but with services.php I get an error

The file "../src" does not exist (in: "/var/www/app/src/Authorization/AuthorizationProcessor") in /var/www/app/config/services.php (which is being imported from "/var/www/app/src/Kernel.php").

Upvotes: 0

Views: 422

Answers (2)

pepper
pepper

Reputation: 1772

I've figured it out. It has nothing to do with path. What is important - the order.

When it is YAML than you could have first imports, then services section. But when it is PHP - the import has to be the last instruction.

As soon as I've moved the import after the $services->load it starts working as expected.

Upvotes: 1

Kolyunya
Kolyunya

Reputation: 6240

I'd try replacing all the paths with this format:

$di->import(__DIR__.'/../src/**/di.php');

Note the added / in the beginning of the string.

Upvotes: -1

Related Questions