-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQLite attempts to load non-class files, resulting in weird errors #659
Comments
What if |
I've taken a look at how other frameworks do it: Symfony loads controller routes in two ways:
Spiral also does some weird stuff by tokenizing PHP files and extracting all defined classes, like Symfony does in the first case: https://github.com/spiral/framework/blob/master/src/Tokenizer/src/ClassLocator.php#L55 None of these support a non auto-loaded use case though, but we can do an argument no problem. As for the skipping files, it's not trivial to check whether code contains GraphQLite attributes as that requires the class to be already loaded to access All I can think of is the following:
|
Seems reasonable to me |
Ha @oojacoboo. I was working on a pull request and writing tests and it turns out the previous "fix" that doesn't use autoloading doesn't actually work as I expected 😆 What I expected was that it allowed a class to have an incorrect namespace in two scenarios:
That's not the case though 🥲 The way it currently works is // Result of GlobClassExplorer::getClassMap()
array:1 [
"TheCodingMachine\GraphQLite\Fixtures\BadNamespace\BadNamespaceClass" => "/opt/project/tests/Fixtures/BadNamespace/BadNamespaceClass.php"
]
// BadNamespaceClass.php
<?php
namespace OtherNamespace;
class BadNamespaceClass {} In the first case, where a class has a namespace that doesn't match PSR-0 or PSR-4, the file would indeed be loaded. The problem is that although a class was loaded, we don't know it's full class name :) In the example above, the one Because there's still no class with class name In the second case, All this to say even the current behaviour essentially silently skips all files with incorrect namespaces, and it was specifically made not to get an error on an invalid namespace in a file. |
Oh, and for some reason this was only a fix for |
Please also see this ticket, see if there is some overlap: #657 |
Not much of an overlap. But I've searched through packagist and it seems there's one up-to-date package that does correctly parse all namespaces, including those from |
@oprypkhantc I think it will relate unfortunately, lib you suggested is great, thx for that, but it does exactly the same what you mention (require_once everything it can), but I have an easier solution for you: I'm leaving possibility to provide |
@fogrye Not sure what you mean. The lib I suggested only globs classes, the "load" part is on the graphqlite side. There's no need for blacklists if we just allow using proper autoloading as it should've been in the first place. |
@oprypkhantc as you can see here files will be autoloaded so, unfortunately, if I understand your issue correctly, the only way left is to blacklist. |
Sorry, didn't notice that. That's unfortunate, but we can still use that package's idea to support vendor loading - instead of parsing the composer.json, we could pull namespace mapping from Composer's AutoLoader class. I don't believe excludes/blacklist is a good idea as it feels like a step backwards :( |
@oprypkhantc I copied the test you have created and it passes with mine changes #664, I will try to figure out how to test |
No, you placed them in best way possible, you can check this commit, I hope this will solve your issue too. Thanks a lot. |
Assuming that we will resolve this after migration to |
@oprypkhantc was this resolved by alekitto/class-finder@4187ae1? If so, a PR will need to be submitted to update the version, after it's tagged. |
@oojacoboo Unfortunately not, as it still uses |
@oprypkhantc you can't rely on Composer's autoload file? We actually ran into a similar issue with some |
What do you mean by relying on Composer's autoload file? The problem with loading arbitrary .php files is now fixed, thanks to |
Hey.
We have a
modules/
folder that maps toModules\
PSR-4 namespace. This namespace was also added to be globbed by graphqlite using->addTypeNamespace('Modules\\')
and-> addControllerNamespace('Modules\\')
. That namespace also contains non-class files (such as lang files and others), as well as pre-8.0 enums that require autoloading before they can be used.If you try to blindly require these files, you'll get errors as those non-class files are meant to be loaded from a specific context, and those enums require a special autoloader to call
::initialize()
static method on them.The problem is that GraphQLite does exactly that - blindly load all files using
require_once
that appear as classes to it. Obviously, this is not desired, as it forces you to list every class separately in calls to->addTypeNamespace()
and->addControllerNamespace()
to avoid loading what it can't load.This is happening because of this fix: https://github.com/thecodingmachine/graphqlite/blob/master/src/Utils/Namespaces/NS.php#L55. It was done specifically to avoid issues with autoloading, and to allow autoloading incorrectly namespaced classes.
Instead of doing that, GraphQLite can instead attempt to load a class through autoloading and simply skip it if it fails. This will break loading of classes that have incorrect namespaces.
What do you think @oojacoboo? I'll draft a PR if it's an acceptable BC.
The text was updated successfully, but these errors were encountered: