PHP

浏览模式: 标准 | 列表 | 图文
我一直认为已经没用的laravel-ide-helper还在更新

我一直认为已经没用的laravel-ide-helper还在更新

如题,真没想到barryvdh/laravel-ide-helper还在更新。毕竟现在phpStorm已经功能这么强大了,我想它应该是没有用了吧!结果在看到LaravelQuery的插件时,提到了它,同时提到的还有LaravelIdea。毕竟 LaravelIdea是要钱的。还挺贵。所以又composer require了一下barryvdh/laravel-ide-helper。

然后php artisan ide-helper:model,发现他把scopeFilter这样的方法加进注释啦,这个确实挺方便。不然每次我都 要自己在写了scope方法后,还得主动到注释里加上。否则 IDE会认不出 filter()后的方法。
例如:
function scopeIsActive($query ){
$query->where('status','=','active');
}
在用的时候 Model::query()->isActive()->get(); 写到 isActive()后,后面的方法都认不出来。
因此,在这时候 只能在Model类里加入
/**
* @method static \Illuminate\Database\Eloquent\Builder|ModelName isActive()
*/
当然不是不能写,只是每次这么写就有点累罢了
其他的生成对 phpstorm来说感觉用处没有那么大,当然如果你象官方那样进行扩展的话,倒也不是不可以。
belongsToMany的备份

belongsToMany的备份

纯备份,长时间不用的时候 直接就忘了

class Shop{
/**
* 员工
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function staffs()
{
// 1、目标类 UnionUser,
// 2、中间表的表名 pivot: union_user_store,
// 3、中间表对应当前表的字段 :shopcode ,
// 4、中间表对应目标类的字段 usercode
// 当关联不是ID时,需要有5、6参数
// 5、当前类对应 3的字段 (code -> 3:shopcode)
// 6、目标类对应4的字段 (usercode -> 4:usercode )
return $this->belongsToMany(UnionUser::class, UnionUserStore::getTable(), 'shopcode', 'usercode', 'code', UnionUser::USERCODE);
}
}
---EOF--
laravel debug模式下抛出异常时隐藏关键字段

laravel debug模式下抛出异常时隐藏关键字段

laravel在debug模式下抛出异常时,会把当前的环境变量等打印出来,即使是开发机、测试机,其实有时候也不希望将一些关键的变量公开给所有人,比如密码、APPKEY(主要是不少人在上传的时候APPKEY本地和服务器一致),还有各类OSS、微信的appId,secret,token等,所以就需要在抛出异常时隐藏掉。

网上查到的是在config/app.php中增加:debug_hide 的数组下标,对应的数据是_ENV,_SERVER,其实就是把遍历这些全局变量,对指定的KEY进行隐藏。但偶尔也查到有debug_blacklist。于是全文搜索了一下,发现框架里是:foreach([config('app.debug_hide'),config('app.debug_blacklist')]),看来这是一个历史 遗留问题。
知道怎么写之后就方便了。例如:
'debug_hide'=>[
'_ENV' => ['APP_KEY']
]
,随便抛一个异常可以看到 ,输出的APP_KEY的地方,已经不再是标准值了,而是一堆***,关键变量其实有很多,如果一个个写,感觉有点累,再加上,它不支持 * 的模拟判断,比如*KEY*,则完全无效。于是偷了个懒:
'debug_hide'=>[
'_ENV' => array_filter(array_keys($_ENV),fn($v)=> Str::contains(strtolower($v), ['oss_', 'db_', 'cookie', 'key','secret', 'password', 'aliyun']) ),
]
既然都在debug_mode下了。也不在乎这点性能了。。
tips:
1、不能用str($v)->contains,因为str这个helper函数,居然还调用了env的class,而在刚加载config文件时。env的类还没有加载进来。
2、strtolower,是因为Str::contains不能忽略大小写。虽然.env都是标准的大写,但你不能保证其他人有没有写小写(又不能抽他们)。所以先tolower一下
解决!
Sablog-x 1.6升级支持PHP8成功

Sablog-x 1.6升级支持PHP8成功

发现从PHP7.4升到PHP8后,就一个地方,那就是 get_magic_quotes_gpc 这个函数。报错信息是:PHP message: PHP Fatal error: Uncaught Error: Call to undefined function get_magic_quotes_gpc()。

方法未定义?看了下文档,原来从7.4.1开始就已经不再支持了,而且永远返回False,在PHP8中彻底移除,仅此而已,于是乎。。。。
在 include/common.php 文件中,在调用 get_magic_quotes_gpc上面加了一段 :
if(!function_exists('get_magic_quotes_gpc')){
function get_magic_quotes_gpc(){
return false;
}
}
然后,一切正常,所以我就不再上传新包了。(页面详情可能有不正常的情况,是因为我在上一页下一页的时候。$article[articleid]上没有加单引号,进入后台在模板管理 default/show.php 搜到 57行,下一篇的链接就是 ,调整一下即可)
----
改完后才发现,问题很多啊。。。比如 global.php的296行,$article[image] ,这种代码太多了。。。。估计得一点点改了,php4的后遗症啊。
----
只能说解决了95%的问题,其它的只能遇到后慢慢改了。。。我也不可能一个个的去定义:define('image','image');。当然 这样就彻底解决了,只是有点2
Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."

Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."

在使用Ziggy的时候,偶尔会报类似的错误:

Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."。
仔细看出错的路由,往往都是有必要参数的,比如Route::any('/test/{user:uuid}',[UserController::class,'test'])->name('test')
象这种路由,如果在JS中直接获取route().current()来获取当前路径并显示,那肯定就会报错。因为current()方法是将参数和路由一并显示出来。
route(route().current())这样的报错怎么处理呢?
其实,只要加上params参数就行了,例如:route(route().current(),route().params).toString(),为什么会有这么妖的写法?是因为在公众号的网页里,如果你要用jssdk,就必须是当前URL,如果不正确,获取的签名也就可能是失败的,因此,才想到用这个恶心的方法。当然你也可以用location.href...
aliyun composer 镜像可能有问题

aliyun composer 镜像可能有问题

好几次遇到阿里云的镜像更新有问题了,明明某个库有了最新版,但阿里云就是报版本不存在。今天又出现了这样的情况,安装laravel-zero的时候,require-dev中有个laravel/pint ^1.2,直接就报了

Problem 1
- Root composer.json requires laravel/pint ^1.2, found laravel/pint[dev-main, v0.1.0, ..., v0.2.4, v1.0.0, ..., v1.1.3] but it does not match the constraint.
问题是这个laravel/pint 的github上,1.2的release是 9月13日啊。(laravel/pint: Laravel Pint is an opinionated PHP code style fixer for minimalists. (github.com))。
由于之前也遇到过这个问题,当时是unset掉 aliyun镜像,换成了官方镜像,但确实是慢,所以我就看了一眼其他镜像,比如以前常用的https://packagist.phpcomposer.com,在国内没有其他镜像的时候,它做了很大的贡献,然而也太不稳定了。直接报:

Problem 1 - Root composer.json requires laravel-zero/framework, it could not be found in any version, there may be a typo in the package name. Problem 2 - Root composer.json requires nunomaduro/termwind, it could not be found in any version, there may be a typo in the package name. Problem 3 - Root composer.json requires laravel/pint, it could not be found in any version, there may be a typo in the package name. Problem 4 - Root composer.json requires mockery/mockery, it could not be found in any version, there may be a typo in the package name. Problem 5 - Root composer.json requires pestphp/pest, it could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details. - It's a private package and you forgot to add a custom repository to find it Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

看来,已经多年没有更新过了,真可惜了。
最后在这里找到了:国内 PHP Composer 镜像列表 - 腾讯云开发者社区-腾讯云 (tencent.com),列表中的那些镜像,象最后的安畅网络的,网址都崩了。所幸,腾讯的还能用。
打开:腾讯软件源 (tencent.com),搜到composer,看一下帮助(真简单就两句话),一句教你配置,一句教你下载。
配好之后,composer u,完成。记录一下
1、阿里:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
2、腾讯:composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
3、清除:composer config -g --unset repos.packagist
带 -g 的是全局的,不带 -g,就是当前项目了。

predis扩展数组和对象的存入

predis扩展数组和对象的存入

PHP有不少Redis库,比如pecl的Redis库(phpredis),就是直接自带了数组的存取和读出,因为他在存储和读出的时候自动序列化了。象是象predis就不行,如果你直接存一个数组去,那么它就会报warning,同时存一个Array到指定的KEY上。

但是predis允许你封装自定义的redis方法。比如jsonset/jsonget,这时候你利用这些自定义的方法来获取或写入数组即可。(基于Laravel,其他的也一样,其他参考:Setting arrays · Issue #136 · predis/predis (github.com))

只是官方的Issue中 【Predis\Profile\ServerProfile:】已经不存在了,要换成【Predis\Profile\Factory】,其余可以复制

----

基于Laravel的话,上述的数组可以放到config里,就啥出不用配置了~~

if (!function_exists('yredis')) {
class StringSetJson extends Predis\Command\StringSet
{
protected function filterArguments(array $arguments)
{
$arguments[1] = json_encode($arguments[1]);
return $arguments;
}
}

class StringGetJson extends Predis\Command\StringGet
{
public function parseResponse($data)
{
return json_decode($data, true);
}
}

class StringSetPhp extends Predis\Command\StringSet
{
protected function filterArguments(array $arguments)
{
$arguments[1] = serialize($arguments[1]);
return $arguments;
}
}

class StringGetPhp extends Predis\Command\StringGet
{
public function parseResponse($data)
{
return unserialize($data);
}
}

/**
* 这个方法是为了读redis,但是不含prefix
* @param string $connection
* @return RedisManager
*/
function yredis()
{
try {
$redis = app('yredis');
} catch (Exception $e) {
app()->singleton('yredis', function ($app) {
$config = $app->make('config')->get('database.redis', []);
unset($config['options']['prefix']);
if(env('REDIS_CLIENT') == 'predis'){
$config['options']['profile'] = function ($options, $option) {
$profile = \Predis\Profile\Factory::getDefault();
$profile->defineCommand('jsonset', 'StringSetJson');
$profile->defineCommand('jsonget', 'StringGetJson');
$profile->defineCommand('phpset', 'StringSetPhp');
$profile->defineCommand('phpget', 'StringGetPhp');
return $profile;
};
}
return new \Illuminate\Redis\RedisManager($app, \Illuminate\Support\Arr::pull($config, 'client', 'phpredis'), $config);
});
app()->bind('yredis.connection', function ($app) {
return $app['yredis']->connection();
});
$redis = app('yredis');
}
return $redis;
}
}
因为laravel的默认config会带 prefix,所以写一个简单的,其实就是为了在写数据的时候不加 prefix 。。。
PHP 8.1 features a new type for return values: never.

PHP 8.1 features a new type for return values: never.

如题

比如,你定义了一个方法,
function test():never {
exit(1);
}

在其他方法使用的时候,
doSomething(){
test() ;
echo 123;
}

在IDE里,会提示这个echo 123;的错误是unreachable

原来phpstorm支持:#[NoReturn] 这个注释,但现在有语法级的支持,则变得更舒服。
----
然而,现在线上用的还是PHP7.3。连fn=> 这个功能都用不了。还有就是对于对象的操作$a?->b?->c也用不了。。。
只能接着忍,Laravel 9最低要8.0了,估计可能会强制更新了
fpdf: This PDF document probably uses a compression technique

fpdf: This PDF document probably uses a compression technique

使用fpdf读取PDF文件时,报错:This PDF document probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)

提示这个文件是采用了一个压缩格式,到官网上查看之后,发现他们提供了fpdi-pdf-paser2,但是这是收费的。。。啊啊啊,短时间内先忽略吧。毕竟这样的PDF不多。
后面再关注一下有没有类似的东西。
1、安装pdf-parser2 : FPDI PDF-Parser 2.x (setasign.com)
2、查看这里的测试代码和示例:https://www.setasign.com/products/fpdi-pdf-parser/details/
目前我是放弃了,因为,他develop version,要100欧,然而这只是我自己用来测试,没必要。
------
后记:
1、读了PDF的第一行,发现这个PDF版本是PDF1.5,尝试用chrome打开,另存为,无效。选择打印->另存为PDF,成功
// USE GHOSTSCRIPT IF PDF VERSION ABOVE 1.4 AND SAVE ANY PDF TO VERSION 1.4 , SAVE NEW PDF OF 1.4 VERSION TO NEW LOCATION
<?php
exec('gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="'.$srcfile_new.'" "'.$srcfile.'"');
?>
结果,官网上没有mac的可执行文件,又不想编译,所以纯记录一下吧
-----
0227更新:
GPL Ghostscript 9.50 (2019-10-15)
Copyright (C) 2019 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
GPL Ghostscript 9.50: Cannot open X display `(null)'.
**** Unable to open the initial device, quitting.
纠结。
解决Chrome的ERR_EMPTY_RESPONSE问题

解决Chrome的ERR_EMPTY_RESPONSE问题

在使用本地valet进行开发的时候,打开刚指定的域名,出现:ERR_EMPTY_RESPONSE

命令行下访问一切正常:curl http://xxx.dev 都能够正常访问,但就是网页不行。因此问题不是出在了所谓的防火墙上(网上很多搜到的资料都是说防火墙)
试着清除浏览器缓存之类的,发现没正常,因为我换了chrome/edge/safari,都是同样的问题。
这时候开始怀疑网络问题了。因为在访问前正好将 global 模式换成了 pac mode,于是关闭APP,访问恢复。
这个问题应该是在切换网络的时候,系统缓存下来了。但命令行里却是正常。。。
做了几次测试
1、打开PAC模式,断开网络,本地网站恢复,连接后继续不正常
2、关闭PAC模式,恢复,打开后不正常
3、因为valet的其他站点正常,尝试unlink再link ,并没有效果
4、valet secure后,https访问,正常。关闭后继续无法访问 【这里还很妖,safari在https下可以访问,edge/chrome不行】
5、sudo brew services restart dnsmasq ,无效。
6、。。。。。没重启,毕竟机器 已经运行60多天了(12:13 up 68 days, 20:44, 6 users, load averages: 2.22 2.96 3.27),先缓缓
---暂先使用valet secure进行访问吧
---updated 01-03 16:00
这个缓存不知道怎么处理了,换了一个link,恢复了。valet link othername。。。正常,难道blog这个域名有问题??