在 Laravel 之外使用 illuminate 组件

当代框架基本都是有组件构成,这使得框架变得更加灵活。The Laravel Components | github Laravel 中有不少优质组件,那如何在 Laravel 之外使用 illuminate 组件呢? illuminate/validation 以 illuminate/validation 为例,validation 有丰富的数据验证功能。 在项目的 composer.json 文件中添加: ... "require": { ... "illuminate/validation": "^5.8", ... 从 Laravel-Lang/lang 项目中复制需要的语言文件放到自己的项目中。 例如:在 Yii2 项目中,复制对应语言文件到项目中的 assets/lang/zh-CN/validation.php。 创建 common/Validator.php: <?php namespace app\common; use Illuminate\Filesystem\Filesystem; use Illuminate\Translation\FileLoader; use Illuminate\Translation\Translator; use Illuminate\Validation\Factory; class Validator { private static $instance = null; private function __construct() { } public static function getInstance(): Factory { if (null === static::$instance) { $translationPath = get_alias('@assets/lang'); $translationLocale = 'zh-CN'; $transFileLoader = new FileLoader(new Filesystem(), $translationPath); $translator = new Translator($transFileLoader, $translationLocale); static::$instance = new Factory($translator); } return static::$instance; } } 在全局函数文件添加: // https://learnku.com/docs/laravel/5.8/validation/3899#manually-creating-validators // $rules = [ // 'name' => 'required|string|min:2|max:5', // 'code' => 'required|string|min:2|max:5', // ]; function validator(array $data, array $rules, array $messages = [], array $customAttributes = []) { return \app\common\Validator::getInstance()->make($data, $rules, $messages, $customAttributes); } 测试使用: ...

September 11, 2020 · 1 min · 173 words · Me

Redis 正则批量删除 key

EVAL "return redis.call('del', 'defaultKey', unpack(redis.call('keys', ARGV[1])))" 0 prefix:* 循环删除: EVAL "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,5000 do \n redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) \n end \n return keys" 0 prefix:* References How to atomically delete keys matching a pattern using Redis | stackoverflow – EOF –

August 17, 2020 · 1 min · 48 words · Me

MySQL JSON 数据类型

The JSON Data Type | mysql As of MySQL 5.7.8, MySQL supports a native JSON data type JSON Function Reference | mysql A JSON column cannot have a non-NULL default value. 索引 设置虚拟列 -> 虚拟列建立索引 在 MySQL 5.7 中,支持两种 Generated Column,即 Virtual Generated Column 和 Stored Generated Column,前者只将 Generated Column 保存在数据字典中(表的元数据),并不会将这一列数据持久化到磁盘上;后者会将 Generated Column 持久化到磁盘上,而不是每次读取的时候计算所得。很明显,后者存放了可以通过已有数据计算而得的数据,需要更多的磁盘空间,与 Virtual Column 相比并没有优势,因此,MySQL 5.7 中,不指定 Generated Column 的类型,默认是 Virtual Column。 如果需要 Stored Generated Golumn 的话,可能在 Virtual Generated Column 上建立索引更加合适,一般情况下,都使用 Virtual Generated Column,这也是 MySQL 默认的方式。 ...

August 14, 2020 · 1 min · 160 words · Me

Composer vendor 提交至 Git

应该将 vendor 提交到 Git 吗 一般建议是 不。vendor 目录应添加到 .gitignore。 最佳实践是让所有开发人员使用 Composer 来安装依赖项。类似地,构建服务器、CI、部署工具等都应该作为项目启动的一部分来运行 Composer。 虽然在某些环境下这样做很诱人,但也会导致一些问题: 大型 VCS 存储库的大小和更新代码时的差异。 在你自己的 VCS 复制你所有依赖的历史。 将通过 git 安装的依赖项添加到 git repo 中将显示为 submodules。这是有问题的,因为它们不是真正的 submodules,您将会遇到问题。 如果你真的觉得你必须这样做,你有几个选择: 限制自己安装带标记的版本(没有 dev 版本),这样就只能安装压缩版,并避免与 git submodules 有关的问题。 Use --prefer-dist or set preferred-install to dist in your config. Remove the .git directory of every dependency after the installation, then you can add them to your git repo. You can do that with rm -rf vendor/\*\*/.git in ZSH or find vendor/ -type d -name ".git" -exec rm -rf {} \; in Bash. 但这意味着您必须在运行 composer 更新之前从磁盘中删除这些依赖项。 Add a .gitignore rule /vendor/**/.git to ignore all the vendor .git folders. 这种方法不需要在运行编写器更新之前从磁盘删除依赖项。 我的做法 问题解决了,但是不确信做法是否正确。 ...

August 10, 2020 · 1 min · 142 words · Me

PHP Swoft 框架环境配置

2023-12-22:内容不再维护,推荐使用 Docker。 安装 Swoole pecl install swoole 可能出现: Connection to `ssl://pecl.php.net:443′ failed: # 检查 php -r "print_r(openssl_get_cert_locations());" Array ( [default_cert_file] => /private/etc/ssl/cert.pem [default_cert_file_env] => SSL_CERT_FILE [default_cert_dir] => /private/etc/ssl/certs [default_cert_dir_env] => SSL_CERT_DIR [default_private_dir] => /private/etc/ssl/private [default_default_cert_area] => /private/etc/ssl [ini_cafile] => [ini_capath] => ) ls /private/etc/ssl/ # 现没有 cert.pem 这个证书 # 下载证书 wget -c https://curl.haxx.se/ca/cacert.pem /private/etc/ssl/cert.pem --no-check-certificate # 再次执行 pecl install swoole PEAR PECL Composer PEAR:PHP Extension and Application Repository,PEAR 将 PHP 程序开发过程中常用的功能编写成类库,涵盖了页面呈现、数据库访问、文件操作、数据结构、缓存操作、网络协议、WebService 等许多方面,用户可以通过下载这些类库并适当的作一些定制以实现自己需要的功能。避免重复发明“车轮”。PEAR 的出现大大提高了 PHP 程序的开发效率和开发质量。使用的时候,要在代码中进行 Include 才能够使用。但基本已经没落,被 Composer 取而代之。 ...

August 7, 2020 · 1 min · 121 words · Me

tcpdump 入门使用

tcpdump 是 Unix/Linux 下的抓包工具,可以针对指定网卡、端口、协议进行抓包。 字太多不看 sudo tcpdump host api.test and tcp port 80 -A -nn sudo tcpdump dst api.test and tcp port 80 -A 一举成名天下知 man tcpdump 获取适配器列表 tcpdump -D tcpdump --list-interfaces 1.en0 [Up, Running] 2.p2p0 [Up, Running] 3.awdl0 [Up, Running] 4.llw0 [Up, Running] 5.utun0 [Up, Running] 6.utun1 [Up, Running] 7.utun2 [Up, Running] 8.en5 [Up, Running] 9.lo0 [Up, Running, Loopback] 10.bridge0 [Up, Running] 11.en1 [Up, Running] 12.en2 [Up, Running] 13.en3 [Up, Running] 14.en4 [Up, Running] 15.gif0 [none] 16.stf0 [none] 17.XHC0 [none] 18.XHC1 [none] 19.ap1 [none] 20.XHC20 [none] 21.VHC128 [none] 监听适配器 Listen on interface. ...

June 29, 2020 · 3 min · 639 words · Me

配置 Laradock PhpStorm Xdubug

最近在学习 Yii2 的源码,为了方便调试所以研究下 Laradock + PhpStorm + Xdubug 的配置。 环境 macOS Laradock v10.0 请保证 Laradock 是最新的版本,可以减少不必要的麻烦。也推荐使用我精简过的项目 imzyf/my-dock | github。 配置 Laradock vim .env WORKSPACE_INSTALL_XDEBUG=true PHP_FPM_INSTALL_XDEBUG=true 重新编译 php-fpm 和 workspace 容器: docker-compose build php-fpm workspace 配置 PhpStorm 配置 Docker Preferences > Build, Execution, Deploymnent > Docker 配置 PHP Preferences > Languages & Frameworks > PHP,PHP CLI Interpreter 点 ... 点击 +,选择 From Docker, Vagrant… ...

May 26, 2020 · 1 min · 137 words · Me

PHP float 精度

实例 1 $a = 1.1; var_dump(gettype($a)); // string(6) "double" var_dump($a); // float(1.1) 实例 2 $a = "123456789.1100110011"; $a = (float) $a; var_dump($a); // float(123456789.11001) var_dump(sprintf('%.11f', $a)); // string(21) "123456789.11001099646" $b = 123456789.11001; var_dump($b); // float(123456789.11001) var_dump(sprintf('%.11f', $b)); // string(21) "123456789.11000999808" $c = '123456789.1100110011'; $c = (float) $c; var_dump($c); // float(123456789.11001) $c = (string) $c; var_dump($c); // string(15) "123456789.11001" $c = (float) $c; var_dump($c); // float(123456789.11001) var_dump(sprintf('%.11f', $c)); // string(21) "123456789.11000999808" var_dump($a === $b); // bool(false) - 说明 $a 还是携带着 float 的精度 var_dump($b === $c); // bool(true) 实例 3 // # 1 var_dump(120085 === 1200.85 * 100); // bool(false) // # 2 var_dump(120085 == 1200.85 * 100); // bool(false) // # 3 var_dump(120081 == 1200.81 * 100); // bool(true) // # 4 var_dump(120085 - 1200.85 * 100); // float(1.4551915228367E-11) 实例 4 $a = 0.1; $b = 0.9; $c = 1; var_dump(($a + $b) == $c); // bool(true) var_dump(($c - $b) == $a); // bool(false) var_dump(sprintf('%.20f', $a + $b)); // string(22) "1.00000000000000000000" var_dump(sprintf('%.20f', $c - $b)); // string(22) "0.09999999999999997780" var_dump((0.5 - 0.25) === 0.25); // bool(true) 0.5 二进制 0.1,0.25 二进制 0.01 var_dump((0.25 + 0.25) === 0.5); // bool(true) 实例 5 $n = 19.99; dump($n * 100); // 1999.0 dump((int) ($n * 100)); // 1998 !!! dump((string) ($n * 100)); // "1999" dump((int) (string) ($n * 100)); // 1999 dump(round($n * 100)); // 1999.0 !!! dump((int) round($n * 100)); // 1999 分析 看文档: ...

May 9, 2020 · 3 min · 428 words · Me

在 CentOS 编译安装 PHP

2023-12-22:内容不再维护,推荐使用 Docker。 环境 CentOS 7.4 PHP 5.6.40 下载 PHP 源码地址:https://www.php.net/releases/#5.6.40 cd /tmp wget https://www.php.net/distributions/php-5.6.40.tar.gz --no-check-certificate tar zxvf php-5.6.40.tar.gz 安装相关库 yum groupinstall "Development tools" 配置 核心配置选项列表 | php.net # 查看配置参数 ./configure --help ./configure --help | grep openssl # 注意替换 --prefix ./configure --prefix=/usr/local/php56 \ --with-openssl \ --enable-mbstring \ --enable-ftp 编译安装 # 4核编译 make clean && make -j4 make install – EOF –

April 29, 2020 · 1 min · 60 words · Me

在 macOS 编译安装 PHP

2023-12-22:内容不再维护,推荐使用 Docker。 环境 macOS 10.15.7 PHP 7.1.33 下载 PHP 源码地址:https://www.php.net/releases/#7.1.33 cd /tmp wget https://www.php.net/distributions/php-7.1.33.tar.gz --no-check-certificate tar zxvf php-7.1.33.tar.gz 安装相关库 确保 Xcode 正确版本正常安装。 # libiconv brew install libiconv # openssl brew install openssl # zlib 实现 GZIP 压缩页面 brew install zlib 配置编译安装 核心配置选项列表 | php.net # 查看配置参数 ./configure --help ./configure --help | grep openssl # 注意替换 --prefix # 按需进行调整 ./configure --prefix=/Users/yifan/php/php71 \ --enable-bcmath \ --enable-calendar \ --enable-dba \ --enable-debug \ --enable-exif \ --enable-ftp \ --enable-fpm \ --enable-mbregex \ --enable-mbstring \ --enable-mysqlnd \ --enable-opcache-file \ --enable-soap \ --enable-sockets \ --enable-zip \ --with-curl=/usr/local/opt/curl \ --with-freetype-dir=/usr/local/opt/freetype \ --with-gd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-iconv=/usr/local/opt/libiconv \ --with-icu-dir=/usr/local/opt/icu4c \ --with-jpeg-dir=/usr/local/opt/jpeg \ --with-libzip \ --with-openssl=/usr/local/opt/openssl@1.1 \ --with-bz2=/usr/local/opt/bzip2 \ --with-libxml-dir=/usr/local/opt/libxml2 \ --with-zlib=/usr/local/opt/zlib # 4核编译 make clean && make -j4 make install 安装常用库: ...

April 28, 2020 · 3 min · 475 words · Me