作曲家自动加载始终是第一位的 [英] Composer autoload always comes first

查看:56
本文介绍了作曲家自动加载始终是第一位的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个遗留项目中使用composer,该遗留项目具有许多没有命名空间的类.重构不是一个选择(这是一个非常庞大的应用程序),但是所有新模块都完全兼容psr-4.遗留代码具有自己的自动加载方法(使用类映射,非常有效).

I'm using composer in a legacy project that have lots of classes with no namespace. Refactoring is not an option (it's a very huge application), but all new modules are fully psr-4 compliant. The legacy code has it's own autoload method (using a class map, pretty efficient).

我的问题是:无论我以哪种顺序添加自动加载器方法,作曲家自动加载器总是首当其冲!这正在减慢每个类的负载:每次我从旧版代码中调用一个类时,它都会首先尝试将其与所有composer自动加载选项(包括findFileWithExtension())进行匹配,然后才调用备用自动加载器.

My problem is: no matter in which order I add the autoloader methods, composer autoloader ALWAYS COMES FIRST! This is slowing down the load of each single class: every time I call a class from the legacy code, it first tries to match it against all composer autoload options (including the findFileWithExtension() ), and only then it calls the alternative autoloader.

我已经使用PHPStorm + XDebug对其进行了检查,无论我首先包括哪个自动加载器,Composer自动加载总是在旧版本之前被调用.

I've inspected it using PHPStorm+XDebug, and no matter which autoloader I include first, Composer autoload is always called before the legacy one.

有没有办法改变这种行为?

Is there a way to change this behaviour??

提前谢谢!

推荐答案

好的,伙计们,我找到了解决方案并想与您分享:spl_autoload_register()函数具有第三个参数:$ prepend.设置为true时,它将在自动加载队列之前添加自动加载功能,而不是将其附加(实际上已在官方 PHP文档中进行了记录). Composer始终将其设置为true,因此始终首先调用其自动加载器.为了解决这个问题,我更改了旧式自动加载器,将$ prepend设置为true,并在包括作曲家的自动加载之后将其命名.

Ok, guys, I found the solution and want to share it with you: the spl_autoload_register() function has a third parameter: $prepend. When set to true, it will prepend the autoload function to the autoload queue, instead of appending it (it's actually documented at the official PHP Documentation). Composer always sets it to true, so that its autoloader is always called first. To fix it, I changed the legacy autoloader, setting $prepend to true, and called it AFTER including composer's autoload.

希望它可以帮助某人! :)

Hope it helps someone! :)

这篇关于作曲家自动加载始终是第一位的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆