手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

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里,就啥出不用配置了~~

PHP代码
  1. if (!function_exists('yredis')) {  
  2.     class StringSetJson extends Predis\Command\StringSet  
  3.     {  
  4.         protected function filterArguments(array $arguments)  
  5.         {  
  6.             $arguments[1] = json_encode($arguments[1]);  
  7.             return $arguments;  
  8.         }  
  9.     }  
  10.   
  11.     class StringGetJson extends Predis\Command\StringGet  
  12.     {  
  13.         public function parseResponse($data)  
  14.         {  
  15.             return json_decode($data, true);  
  16.         }  
  17.     }  
  18.   
  19.     class StringSetPhp extends Predis\Command\StringSet  
  20.     {  
  21.         protected function filterArguments(array $arguments)  
  22.         {  
  23.             $arguments[1] = serialize($arguments[1]);  
  24.             return $arguments;  
  25.         }  
  26.     }  
  27.   
  28.     class StringGetPhp extends Predis\Command\StringGet  
  29.     {  
  30.         public function parseResponse($data)  
  31.         {  
  32.             return unserialize($data);  
  33.         }  
  34.     }  
  35.   
  36.     /** 
  37.      * 这个方法是为了读redis,但是不含prefix 
  38.      * @param  string  $connection 
  39.      * @return RedisManager 
  40.      */  
  41.     function yredis()  
  42.     {  
  43.         try {  
  44.             $redis = app('yredis');  
  45.         } catch (Exception $e) {  
  46.             app()->singleton('yredis'function ($app) {  
  47.                 $config = $app->make('config')->get('database.redis', []);  
  48.                 unset($config['options']['prefix']);  
  49.                 if(env('REDIS_CLIENT') == 'predis'){  
  50.                     $config['options']['profile'] = function ($options$option) {  
  51.                         $profile = \Predis\Profile\Factory::getDefault();  
  52.                         $profile->defineCommand('jsonset''StringSetJson');  
  53.                         $profile->defineCommand('jsonget''StringGetJson');  
  54.                         $profile->defineCommand('phpset''StringSetPhp');  
  55.                         $profile->defineCommand('phpget''StringGetPhp');  
  56.                         return $profile;  
  57.                     };  
  58.                 }  
  59.                 return new \Illuminate\Redis\RedisManager($app, \Illuminate\Support\Arr::pull($config'client''phpredis'), $config);  
  60.             });  
  61.             app()->bind('yredis.connection'function ($app) {  
  62.                 return $app['yredis']->connection();  
  63.             });  
  64.             $redis = app('yredis');  
  65.         }  
  66.         return $redis;  
  67.     }  
  68. }  
因为laravel的默认config会带 prefix,所以写一个简单的,其实就是为了在写数据的时候不加     prefix 。。。

 

 

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

如题

 
比如,你定义了一个方法,
PHP代码
  1. function test():never {  
  2.      exit(1);  
  3. }  

在其他方法使用的时候, 
PHP代码
  1. doSomething(){ 
  2.     test() ; 
  3.     echo 123;
  4. }  

在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读取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,成功
2、网上也有教程,建议是使用ghostscript来处理,比如:php - FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI - Stack Overflow,代码比较简单:
PHP代码
  1. // 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   
  2. <?php   
  3. exec('gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="'.$srcfile_new.'" "'.$srcfile.'"');  
  4.  ?>  
结果,官网上没有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问题

在使用本地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这个域名有问题??

COPY:Supporting multiple TLDs in Laravel Valet

 因为最近在用valet,然后把MAMP PRO都干掉了(不能怪我啊,实在是MAMP PRO6的一些使用太反人类了。。。不知道作者是怎么想的),但现在想想反正也就是这样简单的使用,干脆就用valet了算了。其实也可以用laravel sail的。但因为4月份之前,M1版的mac上的docker都不正常。所以,就暂时放弃了,现在用用valet也算OK。80%的情况下都OK

创建个目录,valet link一下就完事了。因为现在都是laravel的项目,用起来也算方便,毕竟也是官方的工具。但因为我用frp,所以我设置valet tld 真实的域名(比如:xxx.com),这就造成,我访问demo.xxx.com,访问的是127.0.0.1,然后我映射在外面的server也是demo.xxx.com,其实这一切都OK了。然而问题是,有时候我要测试真实域名,比如就是 demo.xxx.com ,我就只能打开全局梯子。这样我访问demo.xxx.com才对应了 frp的域名。

听起来有点绕口令,说白了就是,因为用了真实域名,所以我默认访问都是本地,但外网访问我都是真实的。而我如果要测试真实的网址,反而还要爬个梯子。虽然影响不大,但还是想换换,于是我就想,valet 是不是可以允许绑定两个域名?这样的话,是不是就全解决了?本地测试一个域名,一个域名公开对外。

找了一些文章,发现默认是不支持,但有人通过hack的方法(说白了就是改模板,改配置改代码。。。。)支持了。原文在这里:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613

我简单的COPY了一下,没排版,要看详细的,还是直接看原版吧:

XML/HTML代码
  1. Valet is great.  
  2. Occasionally though I need an app to respond on a different TLD to the default one. And there are even times when it makes sense to have an app respond on multiple TLDs (e.g. to prove some multi-domain functionality).  
  3. You may have some secure assets hosted by a third-party that requires domain verification (FontAwesome Pro). In development, that may mean it’s easier to be using the same domain as other devs on the team than add loads of different dev domains to the service’s whitelist.  
  4. In the current release of Valet, this kind of functionality isn’t natively available. Even in third-party packages like Valet+, this isn’t supported. But with a few quick changes it’s easily possible. Let me show you how.  
  5. Dnsmasq  
  6. First we’ll need to update the Dnsmasq config so that it will handle multiple TLDs.  
  7. Turns out this is really easy. Valet holds some config in ~/.config/valet/dnsmasq.conf.  
  8. In there we just need to add some config for each TLD we want to support. Add the following two lines for every TLD you want to support. Replace {tld} with the TLD, unsurprisingly.  

  9. address=/{tld}/127.0.0.1  
  10. listen-address=127.0.0.1  

  11. Then restart Dnsmasq:  

  12. sudo brew services restart dnsmasq  

  13. Valet  
  14. Now we just need to update the Valet code to support multiple domains.  
  15. Firstly, we’re going to add some values to our ~/.config/valet/config.json. I’ve opted for simply adding keys rather than changing any existing one. I’ve added a tlds array. This will be used later.  

  16. {  
  17.     ...  
  18.     "tlds": [  
  19.          "tld1",  
  20.          "tld2"  
  21.     ]  
  22. }  

  23. Then we just need to edit some Valet files and run some commands.  
  24. All of these edits I’m making directly in my globally-installed Valet (~/.composer/vendor/laravel/valet/).  

  25. // server.php, line 70  
  26. $siteName = basename(  
  27.     // Filter host to support wildcard dns feature  
  28.     valet_support_wildcard_dns($_SERVER['HTTP_HOST']),  
  29.     '.'.$tld  
  30. );  
  31. // change to:  
  32. foreach ($valetConfig['tlds'] as $tld) {  
  33.     if (strpos($_SERVER['HTTP_HOST'], '.'.$tld) === false) {  
  34.         continue;  
  35.     }  
  36.     $siteName = basename(  
  37.         // Filter host to support wildcard dns feature  
  38.         valet_support_wildcard_dns($_SERVER['HTTP_HOST']),  
  39.         '.'.$tld  
  40.     );  
  41. }  

  42. This just allows the server to respond to whichever one of the TLDs we want to support.  
  43. Now let’s update the CLI so we can generate and remove SSL certs for all of the TLDs (valet secure and valet unsecure commands). Firstly, secure:  

  44. // cli/valet.php, line 145, secure command  
  45. $app->command('secure [domain]', function ($domain = null) {  
  46.     $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];  
  47.     Site::secure($url);  
  48.     Nginx::restart();  
  49.     info('The ['.$url.'] site has been secured with a fresh TLS certificate.');  
  50. })->descriptions('Secure the given domain with a trusted TLS certificate');  
  51. // change to:  
  52. $app->command('secure [domain]', function ($domain = null) {  
  53.     $urls = [];  
  54.     foreach (Configuration::read()['tlds'] as $tld) {  
  55.         $url = ($domain ?: Site::host(getcwd())).'.'.$tld;  
  56.         Site::secure($url);  
  57.         $urls[] = $url;  
  58.     }  
  59.     Nginx::restart();  
  60.     $urls = implode(', ', $urls);  
  61.     info('The ['.$urls.'] site has been secured with a fresh TLS certificate.');  
  62. })->descriptions('Secure the given domain with a trusted TLS certificate');  

  63. And now the unsecure command:  
  64. // cli/valet.php, line 158, unsecure command  
  65. $app->command('unsecure [domain]', function ($domain = null) {  
  66.     $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];  
  67. Site::unsecure($url);  
  68. Nginx::restart();  
  69. info('The ['.$url.'] site  will now serve traffic over HTTP.');  
  70. })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');  
  71. // change to:  
  72. $app->command('secure [domain]', function ($domain = null) {  
  73.     $urls = [];  
  74.     foreach (Configuration::read()['tlds'] as $tld) {  
  75.         $url = ($domain ?: Site::host(getcwd())).'.'.$tld;  
  76.         Site::secure($url);  
  77.         $urls[] = $url;  
  78.     }  
  79.     Nginx::restart();  
  80.     $urls = implode(', ', $urls);  
  81.     info('The ['.$urls.'] site will now serve traffic over HTTP.');  
  82. })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');  

  83. Finally, run the following command:  

  84. valet restart 

  85. Enjoy

 

以上是仅复制未排版,嫌丑的看原文:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613

如果不用梯子看不了,就将就点吧

 

Tags: valet, docker, frp

Records:64312345678910»