首页 技术 正文
技术 2022年11月16日
0 收藏 391 点赞 2,584 浏览 2176 个字

PHP的Composer工具规范了我们对系统各种资源库的加载格式,借助于PHP的自动加载机制,可以很大程度上简化在应用开发过程中的类库文件引用场景。但到目前为止,它有个不是问题的问题,就是文件后缀名只支持.php,而基于某些框架开发的旧资产,类文件的后缀名是.class.php,想使用Composer的自动加载规范,就不太纯粹了,一般要两者混着用,或者修改其他框架下的加载规则。

有没有省事点的解决办法呢?

首先只要能产生这么一个疑问,就赢了。而答案呢,多半能找到的。

Composer实现自动加载机制的代码非常简练,稍微看一下就能看懂。

当看到ClassLoader.php文件中的findFileWithExtension方法时参数里出现了一个$ext,也就看到希望。只要在适当的时机,能覆盖这个$ext参数就搞定。

其原始代码如下:

private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
} // PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
} // PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {

稍微修改一下:

autload_psr4.php 配置文件中,对应的格式变化:

return array(
'Qiniu\\' => array($vendorDir . '/qiniu/php-sdk/src/Qiniu’),
// 字符串格式改为二维数组格式
‘Liniu\\' => array([$vendorDir . ‘/Liniu/php-sdk/src/Liniu’, ‘.class.php']),
);

贴出代码:

private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR); $first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
$_ext = $ext;
$_dir = $dir;
if (is_array($dir) && count($dir) == 2) {
$_ext = $dir[1];
$_dir = $dir[0];
}
if (file_exists($file = $_dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4 . $_ext, $length))) {
return $file;
}
}
}
}
} // PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4 . $ext)) {
return $file;
}
} // PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4 . $ext, 0, $pos + 1)
. strtr(substr($logicalPathPsr4 . $ext, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {

编码,有一种纯粹的乐趣。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902