Laravels + DcatAdmin 使用问题 作者: Shine 时间: 2023-12-04 分类: PHP 评论 如果按照 Laravels 的文档所说的,在 `config/laravels.php` 文件的 `cleaners` 数组加上: ```php 'cleaners' => [ Hhxsv5\LaravelS\Illuminate\Cleaners\SessionCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\AuthCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner::class ], ``` 实际上还是会有问题的,比如我遇到的就是菜单栏不会渲染问题 如果要解决这个问题也很简单,参照 https://github.com/hhxsv5/laravel-s/issues/251#issuecomment-971224092 写一个清理累去清理。 以下是我根据这个 issues 摘录的操作步骤 ## 步骤 1. 创建如 `app/Cleaners` 文件夹 2. 新建一个文件 `app/Cleaners/DcatAdminCleaner.php` 内容为 ```php currentApp); $flushAdminState->handle(null); } } ``` 3. 将 `config/laravels.php` 的内容 `Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner::class` 替换为 `App\Cleaners\DcatAdminCleaner::class` 即可 
PHP 8.1 Enum (枚举) 根据名字取得枚举 作者: Shine 时间: 2023-09-21 分类: PHP 评论 灵感来源于:https://github.com/php/php-src/issues/9352#issuecomment-1216839118 ```php $enumClass * @param string $name * @return T|null */ function tryFromName(string $enumClass, string $name) { $constName = $enumClass . '::' . $name; return is_subclass_of($enumClass, UnitEnum::class) && defined($constName) ? constant($constName) : null; } // enum(Gender::UNKNOWN) var_dump(tryFromName(Gender::class, 'UNKNOWN')); // enum(Gender::MALE) var_dump(tryFromName(Gender::class, 'MALE')); // enum(Gender::FEMALE) var_dump(tryFromName(Gender::class, 'FEMALE')); // NULL var_dump(tryFromName(Gender::class, 'null')); ```
php-cs-fix 只格式化已经修改的代码 / 只格式化某次提交的代码 作者: Shine 时间: 2023-09-21 分类: PHP 评论 ## 前提 注意需要在 `composer.json` 中的 `scripts:` 部分添加 `"cs-fix": "php-cs-fixer fix $1"`,如下示例 ```json { "name": "hyperf/hyperf-skeleton", "type": "project", // ... "autoload": { "psr-4": { "App\\": "app/" }, "files": [] }, "scripts": { // ...... "cs-fix": "php-cs-fixer fix $1", // ...... }, } ``` ## 命令 ### 只格式化已经修改的代码 ```shell git diff --name-only --cached | grep '\.php$' | xargs -n1 composer cs-fix ``` 1. `git diff --name-only --cached`:这部分命令用于获取你已经暂存(staged)的修改文件的列表。 2. `grep '\.php$'`:通过管道将文件列表传递给grep命令,这将只选择扩展名为 `.php` 的文件。 3. `xargs -n1 composer cs-fix`:xargs命令将每个文件名作为参数传递给 `composer cs-fix` 命令,从而格式化每个文件的代码。 ### 格式化某次提交的代码 ```shell git diff-tree --no-commit-id --name-only -r | grep '\.php$' | xargs -n1 composer cs-fix ``` 1. `git diff-tree --no-commit-id --name-only -r `:这部分命令获取了特定提交记录中的修改文件列表。 > **** 可以从 `git log` 获取 2. `grep '\.php$'`:通过管道将文件列表传递给 `grep` 命令,这将只选择扩展名为 `.php` 的文件。 3. `xargs -n1 composer cs-fix`:`xargs` 命令将每个文件名作为参数传递给 `composer cs-fix` 命令,从而格式化每个文件的代码。
pecl install swoole 安装出错:fatal error: pcre2.h: No such file or directory 作者: Shine 时间: 2023-09-19 分类: PHP,其他分类 评论 安装错误日志: ```log In file included from /home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/include/php/ext/spl/spl_iterators.h:22, from /home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/include/php/ext/spl/spl_array.h:22, from /tmp/pear/temp/swoole/ext-src/swoole_coroutine.cc:27: /home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/include/php/ext/pcre/php_pcre.h:23:10: fatal error: pcre2.h: No such file or directory 23 | #include "pcre2.h" | ^~~~~~~~~ compilation terminated. make: *** [Makefile:233: ext-src/swoole_coroutine.lo] Error 1 ERROR: `make' failed ``` ## 错误原因 这个错误消息表明编译或构建过程中缺少 pcre2.h 头文件,导致无法找到它。pcre2.h 是 PCRE2(Perl Compatible Regular Expressions)库的头文件,通常用于正则表达式操作。 ## 解决方案 **安装 PCRE2 库** 在 Ubuntu/Debian 上: ```shell sudo apt-get install libpcre2-dev ``` 在 CentOS/RHEL 上: ```shell sudo yum install pcre2-devel ``` 在 macOS 上,你可以使用 Homebrew 安装: ```shell brew install pcre2 ``` 最终完美解决,我这里正常安装 `swoole` 扩展 ``` Build process completed successfully Installing '/home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/pecl/20210902/swoole.so' Installing '/home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/include/php/ext/swoole/config.h' Installing '/home/linuxbrew/.linuxbrew/Cellar/php@8.1/8.1.23/include/php/ext/swoole/php_swoole.h' install ok: channel://pecl.php.net/swoole-5.0.3 Extension swoole enabled in php.ini ```
macOS PHP 使用 Pecl 安装 Swoole 扩展并启用 openssl 作者: Shine 时间: 2023-08-26 分类: PHP 评论 ## 安装 PHP 可以参照此教程安装:https://stitcher.io/blog/php-82-upgrade-mac#upgrade-with-shivammathur/homebrew-php 为了节约时间,我摘抄部分 ```shell brew tap shivammathur/php brew install shivammathur/php/php@8.2 # 切换至 PHP 8.2 brew link --overwrite --force php@8.2 ``` ## 安装 openssl & 获取安装路径 如果你没有安装,可以使用此命令进行安装 ```shell brew install openssl ```  比如我的就是 `/usr/local/Cellar/openssl@3/3.1.2` 这个路径,请记住这个路径 ## 安装 swoole 扩展 & 启用 openssl 支持 > 确保你的网络环境正常 执行以下命令 ```shell sudo pecl install swoole ``` 当询问你是否启用 openssl 是,你就输入 `yes --with-openssl-dir=/usr/local/Cellar/openssl@3/3.1.2` > 其中 `/usr/local/Cellar/openssl@3/3.1.2` 是 openssl 的安装路径  其他的按照自己需求填入 `yes` OR `no` 就好了,不过需要注意可能需要指定相关库的路径,详情请查看[官方文档 -> 编译选项 -> 通用参数](https://wiki.swoole.com/#/environment?id=%e9%80%9a%e7%94%a8%e5%8f%82%e6%95%b0 "官方文档") ## 安装完成  检查是否安装成功,执行命令 `php --ri swoole`,如果有内容输出(如图)表示安装成功 