开发规范

QueryPHP 遵循 PSR-2 命名规范和 PSR-4 自动加载规范。

文件和目录

PSR-4 基础目录使用小写,其它依次使用大驼峰法命名,例如。

  1. /data/codes/queryphp/application/app/Domain/Entity/
  2. /data/codes/queryphp/application/app/Domain/Entity/Test.php

其中 composer 配置

  1. "autoload": {
  2. "psr-4": {
  3. "App\\" : "application/app",
  4. "Admin\\" : "application/admin",
  5. "Common\\" : "common"
  6. }
  7. }

不存在类文件,请使用小写目录,其文件也一样:

  1. /data/codes/queryphp/option/
  2. /data/codes/queryphp/option/app.php

统一代码风格

为了屏蔽不同用户的不同代码风格习惯,QueryPHP 设置一个统一的代码格式化配置来规范团队的代码风格,这符合 PSR-2 规范并且可以通过 StyleCI 规范。

手工优化

在使用前您需要安装 php-cs-fixer,这样子才能够进行下面的工作。

  1. /data/codes/queryphp/.php_cs.dist # 应用
  2. /data/codes/queryphp/vendor/hunzhiwange/framework/.php_cs.dist # 框架核心包

可以通过下面的方式来格式化代码风格:

  1. $cd /data/codes/queryphp
  2. $php-cs-fixer fix --config=.php_cs.dist

结合 Git Hooks 来格式化代码:

  1. /data/codes/queryphp/build/pre-commit.sh
  2. /data/codes/queryphp/vendor/hunzhiwange/framework/build/pre-commit.sh

应用脚本 /data/codes/queryphp/build/pre-commit.sh

这里脚本也包含一段 JS 的脚本,这个用于格式化 QueryPHP 的通用前端后台的 JS 代码风格,跟 PHP 差不多。

  1. #!/bin/bash
  2. #
  3. # check PHP code syntax error and standard with phpcs
  4. # https://blog.csdn.net/xsgnzb/article/details/52222366?locationNum=4&fps=1
  5. # https://blog.csdn.net/ljihe/article/details/80826071
  6. # =================== how to use ====================
  7. # ln -s pre-commit.sh ./../.git/hooks/pre-commit
  8. # git commit -h
  9. # git commit -n -m 'pass hook' #bypass pre-commit and commit-msg hooks
  10. # ==================== end ==========================
  11. PROJECT=$(git rev-parse --show-toplevel)
  12. cd $PROJECT
  13. SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
  14. # Determine if a file list is passed
  15. if [ "$#" -ne 0 ]
  16. then
  17. exit 0
  18. fi
  19. echo "Checking PHP Lint..."
  20. for FILE in $SFILES
  21. do
  22. php -l -d display_errors=0 $FILE
  23. if [ $? != 0 ]
  24. then
  25. echo "Fix the php error before commit."
  26. exit 1
  27. fi
  28. FILES="$FILES $PROJECT/$FILE"
  29. done
  30. # format code style
  31. if [ "$FILES" != "" ]
  32. then
  33. echo "Running Code Sniffer..."
  34. isCheck=""
  35. for FILE in $SFILES
  36. do
  37. result=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`
  38. if [ "$result" != "" ]
  39. then
  40. echo $result
  41. isCheck=$result
  42. git add $FILE
  43. fi
  44. done
  45. if [ "$isCheck" != "" ]
  46. then
  47. echo "The file has been automatically formatted."
  48. fi
  49. fi
  50. # for js
  51. jsfiles=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.vue" "*.css" "*.less" | tr '\n' ' ')
  52. [ -z "$jsfiles" ] && exit 0
  53. # Prettify all staged .js files
  54. echo "$jsfiles" | xargs ./frontend/node_modules/.bin/prettier --config frontend/.prettierrc.js --ignore-path frontend/.prettierignore --write
  55. # Add back the modified/prettified files to staging
  56. echo "$jsfiles" | xargs git add
  57. git update-index -g
  58. exit $?

核心包脚本 /data/codes/queryphp/build/pre-commit.sh

  1. #!/bin/bash
  2. #
  3. # check PHP code syntax error and standard with phpcs
  4. # https://blog.csdn.net/xsgnzb/article/details/52222366?locationNum=4&fps=1
  5. # https://blog.csdn.net/ljihe/article/details/80826071
  6. # =================== how to use ====================
  7. # ln -s pre-commit.sh ./../.git/hooks/pre-commit
  8. # git commit -h
  9. # git commit -n -m 'pass hook' #bypass pre-commit and commit-msg hooks
  10. # ==================== end ==========================
  11. PROJECT=$(git rev-parse --show-toplevel)
  12. cd $PROJECT
  13. SFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
  14. # Determine if a file list is passed
  15. if [ "$#" -ne 0 ]
  16. then
  17. exit 0
  18. fi
  19. echo "Checking PHP Lint..."
  20. for FILE in $SFILES
  21. do
  22. php -l -d display_errors=0 $FILE
  23. if [ $? != 0 ]
  24. then
  25. echo "Fix the php error before commit."
  26. exit 1
  27. fi
  28. FILES="$FILES $PROJECT/$FILE"
  29. done
  30. # format code style
  31. if [ "$FILES" != "" ]
  32. then
  33. echo "Running Code Sniffer..."
  34. isCheck=""
  35. for FILE in $SFILES
  36. do
  37. result=`~/.composer/vendor/bin/php-cs-fixer fix $FILE --config=.php_cs.dist`
  38. if [ "$result" != "" ]
  39. then
  40. echo $result
  41. isCheck=$result
  42. git add $FILE
  43. fi
  44. done
  45. if [ "$isCheck" != "" ]
  46. then
  47. echo "The file has been automatically formatted."
  48. fi
  49. fi
  50. git update-index -g
  51. exit $?

Git Commit

  1. git commit -m 'pass hook'

上述脚本就会自动运行帮助你格式化代码,你也可以忽略脚本。

  1. git commit -n -m 'pass hook'

这样子我们再也不需要浪费时间在无意义的代码风格的讨论上了。