Composer expected class(but not found) when trying to autoload PHP file

I am trying to autoload just a PHP file without any class in it but with helper functions instead. Something like Laravel helpers so:
<?php
if (!function_exists('fixture')) {
function fixture()
{
// some code here
}
}
According to the docs it's quite easy, I am doing something like that:
"autoload": {
"files": [
"src/Helpers/helpers.php"
],
"psr-4": {
"App\\": "src/"
}
},
After that, I am doingcomposer dumpautoload
to regenerate the whole thing. Then the strange thing happens.
If that matters I am using Symfony 5. For as long as I keep my helper file insrc
directory I get this error:
Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\InvalidArgumentException: Expected to find class "App\Helpers\helpers" in file "/srv/app/src/Helpers/helpers.php" while importing services from resource "../src/*", but it was not found! Check the namespace prefix used with the resource. in /srv/app/vendor/symfony/dependency-injection/Loader/FileLoader.php:206
When I move it outside the src directory it seems to be working fine. It looks to me like composer decides that psr-4 is more important than the file key.
What is interesting Laravel does the same thing and it apparently works just fine. Example:
"autoload": {
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
"Illuminate\\": "src/Illuminate/"
}
},
Does anyone have any idea why this could be happening? I am sure it's something silly, I just can't see what it is...
Answer
Solution:
Your composer settings looks fine, it's just that the Symfony autoloader with service manager (di-container) is loading first.
You are calling the function from within a namespace, the function itself is not namespaced.
Try using\fixture();
instead of justfixture();
to explicitly use root namespace.
I'm not sure if there is a workaround, I just assume Symfony didn't setup the di-container to handle a function instead of a class.
A good approach would be to wire the class properly and dont use it from outside the project workflow.
Considering tests, there are easy ways to configure, and load from data providers.
To load something for all tests, use the--bootstrap
flag:
phpunit --bootstrap test/bootstrap.php
Thenbootstrap.php
:
<?php
require_once 'vendor/autoload.php';
function fixture() {
// ...
}
Answer
Solution:
in config/services.yaml
App\:
resource: '../src/'
exclude:
- '../src/helpers.php'
or
App\:
resource: '../src/*'
exclude: '../src/{helpers.php}'
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.