Laravel collect() returns the wrong class when using tightenco/collect #92
Comments
Confirmed the issue with the loading sequence (vendor/composer/autoload_static.php):
|
I'm also experiencing this. |
I opened an issue on the Laravel repository too: laravel/framework#23420 |
Wouldn't it be sufficient to just check if the Illuminate Collection class exists in the helpers.php file? So basically just wrap everything inside:
We're already aliasing the tighten classes in case the illuminate classes do not exist - so if we do the same thing for the helper methods, we're good. At least that's easier than namespacing / prefixing all existing helper functions - which would break a lot :) |
According to this issue in Composer: composer/composer#956 and this PR which fixed it: https://github.com/composer/composer/pull/1051/files#diff-33037407f7f293f37df2258567b56598R238
I agree this is a big issue, but I could not reproduce the bug, because, unfortunately (or luckily) it looks like the order on But this should be an issue only for those requiring Collect before the |
@antonioribeiro I'm able to reproduce it with this: laravel new testapp
cd testapp
composer require spatie/crawler
open http://testapp.test |
Since we have no control on the loading sequence of the files, this package should be Laravel aware by like @mpociot ‘s proposal e.g. |
Hey @mattstauffer , |
@mpociot Thanks for asking! We still haven't found a good solution, to be honest. Wouldn't your proposed changes suffer from the same problem: That if they run before Laravel is loaded, it wont' detect that the class exists? I think any such conditional would have to be inside the definition, changing which class it returns, rather than outside of it. Thoughts? |
@mattstauffer No that is not a problem. The You can manually test this by doing: Reproduce bug
Open the Apply fixEdit the Reload page - the error is gone 💥 Edit: |
@mpociot Ahhh... we're having two issues here. One has to do with the load order (which you are NOT seeing), and another has to do with the collect helper. Your proposed solution fixes the second one: In a laravel project which includes collect or includes a package which includes collect So go ahead with it. Sadly it doesn't fix the issue here (right?) In a Laravel project which includes Collect or includes a package which includes Collect ... regardless, yes, please PR it! And remember to PR it to the stub as well, not just the helper in src. Thanks! |
@mattstauffer To be honest, I’m having a hard time wrapping my head around the load order issue, as I do not really see the difference between these two issues. Isn’t the original issue posted by @tomcoonen only related to the helpers being defined by collect instead of Laravel? That’s the exact same issue that you can reproduce with my example. Let me know if I get something wrong 😅 Regarding the PR: yup, I’ll prepare it. |
What about removing the type hint from the Changing it to array is also a possibility, since the I'm not able to test right now, but I think it would solve the problem. |
@rodrigobendia That would just move the problem to the next method that type-hints Laravel's Collection class. |
You just have to change the stub and run the script |
We have some issues here: The autoloading order of vendors filesThis is the original subject of this issue and this matters because My opinion is that this will work and should be enough to fix it all. The autoloading order of files of a single vendorHere's the related part of the
This is a problem for Collect because the
May return This should not happen, exactly the way loading vendors files required after laravel/framework should not load before it. But it's happening on @mpociot and @tomcoonen's end, but not on my end. This is something discussed on the Composer project and should not behave the way it is. So, should we rely on one order but not on the other? My opinion is that this should not be a problem, because in @tomcoonen's comment, even though vendor files are loading in the wrong order, helpers.php are being loaded before alias.php, as it is supposed to, but, again, as it is behaving erratically in some environments, this may also fail with someone else. Upgrade.sh must changeIf we do not change it, the fix proposed by @mpociot, to be made only on |
I was under the impression that it was the entire loading that was out of order. If it's just that the autoloading of the files that's out of order, but not the PSR-4 classes, then yah, I think that would solve quite a few of the issues we're having. Is that definitively true? The only issue here is that our files are loading out of order, but by the time our files have loaded, the classes have all been autoloaded?? |
The last thing the autoloader does, after loading PSR4, PSR0, ClassMap is to load "files", so classes should have been all loaded at this point:
|
Geez. Then yes. This Pr of marcel’s Work with antonio’s Suggested alterations of the upgrade script sound like exactly what we need. Thanks! |
The only issue I'm having is the loading order of the files, since its sort of random (depends on the packages you require). I didn't test the solution from @mpociot yet, but looking at it I think it solves the problem. |
I was having this problem too, changing the file order worked. But after I changed them back (and it broke again) and applied @mpociot fix and its all good now! 👍 |
@tomcoonen @kre8or69 and anyone else who had this issue: I've merged a fix to master but not tagged yet. Could you try pulling this in dev-master (if that's possible and easy) to see if it fixes it for you? If that's too hard, no worries. |
@mattstauffer I can't require the package on a current project since dependency restrictions won't allow me. |
Yeah i'm the same sorry! |
OK, I've tagged it with 5.6.12 and I think we're good. Here's hoping! |
It seems to be related to the loading sequence I guess, when installing the dev dependencies the issue is gone. So in dev I have no issues, but on non-dev environments the issue is 100% persistent.
How can we prevent the error?
PHP Version: 7.1.12
Laravel: 5.6.8
tightenco/collect: 5.6.7
Type error: Argument 1 passed to Illuminate\Routing\Router::sortMiddleware() must be an instance of Illuminate\Support\Collection, instance of Tightenco\Collect\Support\Collection given, called in xxx/vendor/laravel/framework/src/Illuminate/Routing/Router.php on line 676
The text was updated successfully, but these errors were encountered: