如何在 vscode 中使用 php-cs-fixer 以及 php 和 Composer 的 docker 容器

amu*_*ell 12 php java laravel docker visual-studio-code

目标

我想使用 php-cs-fixer + .php_cs 配置文件来定义规则 - 无需在我的主机上安装 PHP。


问题

我在 Docker 容器内使用 PHP7.3 - 这也是安装 Composer 的地方。这意味着我的本地计算机上没有运行 php。

vscode 的扩展 - 比如 junstyle 的php-cs-fixer似乎要求可执行文件、配置文件和当前工作目录本地化。我无法让它发挥作用。


语境

  • 使用 php 和作曲家的 docker 容器运行本地开发(docker for mac,使用 docker-compose)
  • 项目是一个 Laravel API - 已friendsofphp/php-cs-fixer安装开发所需的作曲家
  • 使用 vscode,带有扩展:Simple PHP CS Fixer
  • 我的项目根目录有一个 .php_cs 配置文件
  • 我已经成功地/var/www/site/app/vendor/bin/php-cs-fixer fix --verbose --config=/var/www/site/.php_cs {PATH TO MY FILE I WANT TO FIX}从 docker 容器中运行作为概念证明。它工作得很好。
    • /var/www/site/~/code/project是映射到我的主机上的容器内的路径

“接近,但还不够接近”解决方案

这篇博客文章提供了有关制作脚本的提示,例如包含以下内容的`/usr/local/bin/docker-phpcs(以及 chmod +x 以使其可执行)

#!/bin/bash

/usr/bin/docker-compose exec -T app-php /var/www/vendor/bin/php-cs-fixer "$@"
Run Code Online (Sandbox Code Playgroud)

"php-cs-fixer.executablePath": "php-cs-fixer" 但是,即使在为junstyle 扩展定义 vscode 设置时,我也无法使其工作"php-cs-fixer.executablePath": "/usr/local/bin/docker-phpcs",

这给了我很多:

[2021-04-12 18:36:39.174] [exthost] [error] [junstyle.php-cs-fixer] provider FAILED
[2021-04-12 18:36:39.174] [exthost] [error] undefined
Run Code Online (Sandbox Code Playgroud)

amu*_*ell 6

更新:新解决方案

跳到tldr;下面的部分以获得直接解决方案。否则,享受:

我不得不接受没有php-cs-fixer vscode 插件可以解决我的主机和 docker 容器之间的路径差异。

我深入研究并分叉了Simple PHP CS FIXER添加了更多配置选项:

  • executablePath - 相对于我的主机,这样我就可以在我的项目中使用脚本。例如。/Users/me/myproject/docker-phpcs- 这个项目最初是php-cs-fixer在这里硬编码的。这仍然是我的版本中的默认设置。

    这是一个示例脚本:

    #!/bin/bash
    
    docker exec -t "mycontainer" /var/www/site/app/vendor/bin/php-cs-fixer $@
    
    Run Code Online (Sandbox Code Playgroud)

    注意:我确保chmod +x在我的脚本上使其可执行

  • hostPath - 所以我可以告诉它我的项目路径,例如。“/用户/我/代码/myproject/”

  • dockerPath - 所以我可以告诉它我的 docker 容器路径,例如。“/var/www/站点”

然后我修改了extension.js,将以下文件中的hostName替换dockerPath :

  • 配置文件(如果已设置)。
  • 正在保存的文档的文件名。

因此,在使用它时,我为每个文件夹创建设置,如下所示:

"simple-php-cs-fixer.executablePath": "/Users/me/code/myproject/docker-phpcs",
"simple-php-cs-fixer.config": ".php_cs",
"simple-php-cs-fixer.hostPath": "/Users/me/code/myproject",
"simple-php-cs-fixer.dockerPath": "/var/www/site",
Run Code Online (Sandbox Code Playgroud)

我故意不强制保存 - 而是为插件的命令(ctrl+s,而不是 cmd + s)进行键绑定,这样我就可以控制何时完全删除我的文件。

太棒了;

我分叉了该插件以提供 docker 相对路径和 php-cs-fixer 的自定义可执行路径的选项 - 我也确实为该插件制作了 PR - 但如果没有被接受......你可以侧面加载扩展,例如所以:

  1. 正常使用市场“安装”插件
  2. 转到您的扩展文件夹(在我的 Mac 上,它是 $HOME/.vscode/extensions
  3. git clone git@github.com:amurrell/simple-php-cs-fixer.git
  4. 删除市场安装的一个,例如:calebporzio.simple-php-cs-fixer-x.x.x

希望PR能被接受...

与此同时,通过 ctrl+s 键绑定和带有额外设置的修改插件,我现在使用我的 docker-container 根据我的配置获得一个固定文件。


另一个封闭的解决方案

更新:这并没有真正准确地解决我的问题 - 我的脚本在保存时会触发,但它实际上并没有处理当前正在编辑/保存的文档文件 - 它正在触发脚本,该脚本将扫描我的配置文件中定义的所有文件。这意味着插件也无法控制配置文件。


我得到了我想要的!假设我的问题中的上下文(例如,您的项目在作曲家中需要friendsofphp/php-cs-fixer):

  1. 使用下面的代码片段编辑您的 .zshrc 或 .bashrc 文件
  2. 安装 Simple PHP CS Fixer 扩展并使用所需的设置进行更新

编辑您的 .zshrc 或 .bashrc

这个来源给了我像这样存储php-cs-fixer在我的.zshrc或文件中的想法:.bashrc

php-cs-fixer () {
  docker run -it --rm --net host -v `pwd`:/app  ypereirareis/php-cs-fixer fix --level=psr2 --verbose $@
}
Run Code Online (Sandbox Code Playgroud)

适合我的情况

我将其粘贴到文件的底部.zshrc

php-cs-fixer () {
    docker exec -t "mycontainername" /var/www/site/app/vendor/bin/php-cs-fixer fix --verbose --config=/var/www/site/.php_cs $@
}
Run Code Online (Sandbox Code Playgroud)
  • mycontainername 可以通过以下方式检索:docker ps并查看最后一列“名称”
  • /var/www/site/app/是容器的路径到我的项目的应用程序文件夹,其中我的composer.json文件是
  • /var/www/site/.php_cs是我的项目的 .php_cs 文件的容器路径,该文件位于我的项目的根目录,例如。~/code/project/.php_cs

安装简单的 PHP CS FIXER

vscode 的这个扩展让一切变得简单。我假设它只会调用“php-cs-fixer”,并且编辑您的个人资料将找到该函数并为您运行 docker 内容。

这是我的设置更改:

"simple-php-cs-fixer.config": ".php_cs",
"simple-php-cs-fixer.save": true,
Run Code Online (Sandbox Code Playgroud)

奖金说明:

我的 .php_cs 文件以防有人好奇 - 我确实使我的 project_path 相对于容器而不是我的主机。

<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$rules = [
    'array_syntax' => ['syntax' => 'short'],
    'binary_operator_spaces' => [
        'default' => 'single_space',
        'operators' => ['=>' => null],
    ],
    'blank_line_after_namespace' => false,
    'blank_line_after_opening_tag' => false,
    // 'blank_line_before_statement' => [
    //     'statements' => ['return'],
    // ],
    'braces' => true,
    'cast_spaces' => true,
    'class_attributes_separation' => [
        'elements' => ['method'],
    ],
    'class_definition' => true,
    'concat_space' => [
        'spacing' => 'one',
    ],
    'declare_equal_normalize' => true,
    'elseif' => true,
    'encoding' => true,
    'full_opening_tag' => true,
    'fully_qualified_strict_types' => true, // added by Shift
    'function_declaration' => true,
    'function_typehint_space' => true,
    'heredoc_to_nowdoc' => true,
    'include' => true,
    'increment_style' => ['style' => 'post'],
    'indentation_type' => true,
    'linebreak_after_opening_tag' => false,
    'line_ending' => true,
    'lowercase_cast' => true,
    'lowercase_constants' => true,
    'lowercase_keywords' => true,
    'lowercase_static_reference' => true, // added from Symfony
    'magic_method_casing' => true, // added from Symfony
    'magic_constant_casing' => true,
    'method_argument_space' => true,
    'native_function_casing' => true,
    'no_alias_functions' => true,
    'no_extra_blank_lines' => [
        'tokens' => [
            'extra',
            'throw',
            'use',
            'use_trait',
        ],
    ],
    'no_blank_lines_after_class_opening' => false,
    'no_blank_lines_after_phpdoc' => true,
    'no_closing_tag' => true,
    'no_empty_phpdoc' => true,
    'no_empty_statement' => true,
    'no_leading_import_slash' => true,
    'no_leading_namespace_whitespace' => true,
    'no_mixed_echo_print' => [
        'use' => 'echo',
    ],
    'no_multiline_whitespace_around_double_arrow' => true,
    'multiline_whitespace_before_semicolons' => [
        'strategy' => 'no_multi_line',
    ],
    'no_short_bool_cast' => true,
    'no_singleline_whitespace_before_semicolons' => true,
    'no_spaces_after_function_name' => true,
    'no_spaces_around_offset' => true,
    'no_spaces_inside_parenthesis' => true,
    'no_trailing_comma_in_list_call' => true,
    'no_trailing_comma_in_singleline_array' => true,
    'no_trailing_whitespace' => true,
    'no_trailing_whitespace_in_comment' => true,
    'no_unneeded_control_parentheses' => true,
    'no_unreachable_default_argument_value' => true,
    'no_useless_return' => true,
    'no_whitespace_before_comma_in_array' => true,
    'no_whitespace_in_blank_line' => true,
    'normalize_index_brace' => true,
    'not_operator_with_successor_space' => true,
    'object_operator_without_whitespace' => true,
    'ordered_imports' => ['sortAlgorithm' => 'alpha'],
    'phpdoc_indent' => true,
    'phpdoc_inline_tag' => true,
    'phpdoc_no_access' => true,
    'phpdoc_no_package' => true,
    'phpdoc_no_useless_inheritdoc' => true,
    'phpdoc_scalar' => true,
    'phpdoc_single_line_var_spacing' => true,
    'phpdoc_summary' => true,
    'phpdoc_to_comment' => true,
    'phpdoc_trim' => true,
    'phpdoc_types' => true,
    'phpdoc_var_without_name' => true,
    'psr4' => true,
    'self_accessor' => true,
    'short_scalar_cast' => true,
    'simplified_null_return' => false, // disabled by Shift
    'single_blank_line_at_eof' => true,
    'single_blank_line_before_namespace' => false,
    'single_class_element_per_statement' => true,
    'single_import_per_statement' => true,
    'single_line_after_imports' => true,
    'single_line_comment_style' => [
        'comment_types' => ['hash'],
    ],
    'single_quote' => true,
    'space_after_semicolon' => true,
    'standardize_not_equals' => true,
    'switch_case_semicolon_to_colon' => true,
    'switch_case_space' => true,
    'ternary_operator_spaces' => true,
    'trailing_comma_in_multiline_array' => true,
    'trim_array_spaces' => true,
    'unary_operator_spaces' => true,
    'visibility_required' => [
        'elements' => ['method', 'property'],
    ],
    'whitespace_after_comma_in_array' => true,
];

$project_path = '/var/www/site/app';
$finder = Finder::create()
    ->in([
        $project_path . '/app',
        $project_path . '/config',
        $project_path . '/database',
        $project_path . '/resources',
        $project_path . '/routes',
        $project_path . '/tests',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return Config::create()
    ->setFinder($finder)
    ->setRules($rules)
    ->setRiskyAllowed(true)
    ->setUsingCache(false);

Run Code Online (Sandbox Code Playgroud)