php-cs-fix 只格式化已经修改的代码 / 只格式化某次提交的代码 作者: Chuwen 时间: 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` 命令,从而格式化每个文件的代码。
Laravel 添加模型方法注释,使 PHPStorm 有语法提示 作者: Chuwen 时间: 2021-06-30 分类: Laravel,PHP 评论 ## 安装 1. 在 PHPStorm 上装 `Laravel` 插件 ![](https://cdn.nowtime.cc/2021/06/30/1433782083.png) 2. 在项目中添加 `barryvdh/laravel-ide-helper` 包 ```shell composer require barryvdh/laravel-ide-helper ``` ## 使用 1.为**所有模型**添加注释 ```shell php artisan ide-helper:models ``` 2.为**指定模型**添加注释 > 以下展示的是为 `App\Models\Admin` 模型添加注释,*\App\Models\Admin* 是模型的命名空间名字 ```shell php artisan ide-helper:models \App\Models\Admin ``` 3.为 Facades 生成注释 ```shell php artisan ide-helper:generate ``` 4.生成 PHPstorm Meta file ```shell php artisan ide-helper:meta ``` ## 运行为“模型添加注释”后的截图 ![](https://cdn.nowtime.cc/2021/06/30/3502548972.png)
Linux 指定用户执行命令 / Composer 因权限问题导致无法执行 作者: Chuwen 时间: 2021-05-14 分类: Linux 评论 ## 背景 在网上查到的使用 `su 用户名 -c "命令"` 即可指定用户执行某些命令 但是运行结果是这样的 > This account is currently not available. ```log [root@izvkgq9z www.xxxx.com]# su www -c "composer install" This account is currently not available. ``` 原来设置了该用户不能用以登录 ``` [root@izvkgq9z www.xxxx.com]# cat /etc/passwd | grep www www:x:1000:1000::/home/www:/sbin/nologin ``` ## 解决办法 > 使用 `su 用户名 -s /bin/bash -c "需要执行的命令"` ``` su -s /bin/bash -c "composer install" www ``` --- > 以下是额外话题 ## composer 安装出现新的问题 执行 `composer install` 报了以下错误 ``` [RuntimeException] Could not delete /home/wwwroot/www.xxxx.com/vendor/kylekatarnls/update-helper/composer.json: Directive 'track_errors' is deprecated ``` 大概意思是不能删除文件,然后就想到了权限问题,然后看了下权限信息 ``` [root@izvkgq9z www.xxxx.com]# ll | grep vendor drwxr-xr-x 47 root root 4096 May 14 13:55 vendor ``` 猜测是项目自动构建之类的,权限没弄好,本应该是 `www` 权限的,这个简单了,使用 `chgrp` 和 `chown` 命令改一下 `vender` 目录及其子目录/文件 用户组和所有者 ``` # 更改用户组 chgrp www -R vendor # 更改所有者 chown www -R vendor ```
Laravel 安装指定版本 作者: Chuwen 时间: 2020-11-23 分类: PHP 评论 ```shell composer create-project --prefer-dist laravel/laravel blog "6.*" ```
【阿里云镜像】macOS 安装 composer 作者: Chuwen 时间: 2020-11-23 分类: macOS 评论 # 下载 > 由于在官网下载 `composer` 比较慢,所以我们可以使用 **阿里云 Composer 全量镜像** 下载 > 但我还是贴出 *官方下载&安装:`curl -sS getcomposer.org/installer | php`* ```shell # 下载 composer.phar curl https://mirrors.aliyun.com/composer/composer.phar -O composer.phar # 检查版本号 php composer.phar -v ``` # 如果需要全局使用 > 可以移动 `composer.phar` 到 `/usr/local/bin/` 路径下 ```shell sudo mv composer.phar /usr/local/bin/composer ``` ## 可能因为权限问题,直接执行 `composer` 会报权限不够 ```log chuwen@WenzhoudeMBP cms.iton.pw % composer -v zsh: permission denied: composer ``` ## 设置下权限即可 ```log sudo chomd -R 755 /usr/local/bin/composer ``` # 测试(输出版本号) > 记得往下,改 `阿里云 Composer 镜像源`,安装项目起飞?️ ```shel chuwen@WenzhoudeMBP cms.iton.pw % composer -v ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.0.7 2020-11-13 17:31:06 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. --no-cache Prevent use of the cache -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: about Shows the short information about Composer. archive Creates an archive of this composer package. browse Opens the package's repository URL or homepage in your browser. cc Clears composer's internal package cache. check-platform-reqs Check that platform requirements are satisfied. clear-cache Clears composer's internal package cache. clearcache Clears composer's internal package cache. config Sets config options. create-project Creates new project from a package into given directory. depends Shows which packages cause the given package to be installed. diagnose Diagnoses the system to identify common errors. dump-autoload Dumps the autoloader. dumpautoload Dumps the autoloader. exec Executes a vendored binary/script. fund Discover how to help fund the maintenance of your dependencies. global Allows running commands in the global composer dir ($COMPOSER_HOME). help Displays help for a command home Opens the package's repository URL or homepage in your browser. i Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json. info Shows information about packages. init Creates a basic composer.json file in current directory. install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json. licenses Shows information about licenses of dependencies. list Lists commands outdated Shows a list of installed packages that have updates available, including their latest version. prohibits Shows which packages prevent the given package from being installed. remove Removes a package from the require or require-dev. require Adds required packages to your composer.json and installs them. run Runs the scripts defined in composer.json. run-script Runs the scripts defined in composer.json. search Searches for packages. self-update Updates composer.phar to the latest version. selfupdate Updates composer.phar to the latest version. show Shows information about packages. status Shows a list of locally modified packages. suggests Shows package suggestions. u Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. update Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. upgrade Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file. validate Validates a composer.json and composer.lock. why Shows which packages cause the given package to be installed. why-not Shows which packages prevent the given package from being installed. ``` # 切换成 阿里云 Composer 镜像源 ## 1. 全局配置(推荐) ```shell composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ # 取消当前项目配置 composer config --unset repos.packagist ``` ## 2. 仅在当前项目配置 ```shell composer config repo.packagist composer https://mirrors.aliyun.com/composer/ # 取消全局配置 composer config -g --unset repos.packagist ```