Closed gquemener closed 8 years ago
this is not my project, but I think you should try with
use Transistor;
because its full name was already define at the top of the file
Never mind, my bad :-) I use composer dump-autoload -o
, so I didn't get this problem
@bjori was receptive to PRs
I've honestly no idea. Not a composer user actually. I can't even get that composer.json you pasted to do anything meaningful. It either fails with
Script Incenteev\ParameterHandler\ScriptHandler::buildParameters handling the post-install-cmd event terminated with an exception
or
Could not scan for classes inside "app/AppKernel.php" which does not appear to be a file nor a folder
One thing I notice is that the mongo-php-library doesn't have "src/MongoDB" folder, only "src/". Meaning the "MongoDB\Client" class is in "src/Client.php" -- while the transistor interface is "src/MongoDB/Transistor.php"
@gquemener, you are right, the problem is the "autoload" entry in the composer.json
file.
It's related to the what you @bjori noticed.
I saw some alternatives to fix it. I'm sharing my experiment with you all.
Run
composer create-project mongodb/transistor:dev-master /tmp/autoload-fix
If you run the file below, you'll get the error…
<?php
require '/tmp/autoload-fix/vendor/autoload.php';
class Tmp { use MongoDB\Transistor; }
new Tmp;
Then change the autoload
entry into /tmp/autoload-fix/composer.json
to one the of these values:
"autoload": {
"psr-4": {
"": "src/"
}
}
or
"autoload": {
"psr-4": {
"MongoDB\\": "src/MongoDB/"
}
}
or
"autoload": {
"classmap": ["src/MongoDB/Transistor.php"]
}
or
"autoload": {
"psr-0": {
"MongoDB\\": "src/"
}
}
Then run:
composer dump-autoload -d /tmp/autoload-fix
If you run the php file again, the error disappears :pray: I hope
@bjori: Confirming that "psr-4": { "MongoDB\\": "src/" }
should resolve this, as @renanbr suggested. We have the same in the PHP library. Your current composer.json
autoload definition has "src\/"
, but you probably shouldn't be escaping that forward slash.
Additionally, you should be able to remove the MongoDB/
directory from the src/
path in the repository.
Thanks @renanbr and @jmikola !
@gquemener I've move the Transistor.php file into more conventional location so your standard composer autoloaders should work fine now.
Thanks for the report! :D
:+1: Great news!
I'll give it a try when I got time this week (I hope).
Cheers
Hello,
I've been struggling for the past hour trying to use the trait provided by your library...
Here's my document:
And here's the error I don't know how to fix:
"Attempted to load trait "Transistor" from namespace "App\Document\MongoDB".Did you forget a "use" statement for "MongoDB\Transistor"?"
I've tried adding a
\
before the trait name (use \MongoDB\Transistor
) but then I get the following error:"Attempted to load trait "Transistor" from namespace "MongoDB".Did you forget a "use" statement for "MongoDB\Transistor"?"
I'm suspecting an autoloading problem, but I can't find where it is. Here's my composer.json in case of it can help: