【问题标题】:How to use php-cs-fixer in vscode with docker container for php and composer如何在 vscode 中使用 php-cs-fixer 和用于 php 和 composer 的 docker 容器
【发布时间】:2021-07-08 02:25:42
【问题描述】:

目标

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


问题

我在 docker 容器中使用 PHP7.3 - 这也是安装 composer 的地方。这意味着我的本地机器上没有运行 php。

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


上下文

  • 使用 docker 容器为 php 和 composer 运行本地开发(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 "$@"

但是,即使将 junstyle 扩展的 vscode 设置从 "php-cs-fixer.executablePath": "php-cs-fixer" 定义为 "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

【问题讨论】:

    标签: java php laravel docker visual-studio-code


    【解决方案1】:

    更新:新解决方案

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

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

    我深入杂草,我 fork 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 $@
      

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

    • hostPath - 所以我可以告诉它我的项目路径,例如。 "/Users/me/code/myproject/"

    • dockerPath - 所以我可以告诉它我的 docker 容器路径,例如。 "/var/www/site"

    然后我修改了 extension.js 以在以下文件中替换 hostNamedockerPath

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

    因此,当使用它时,我会像这样为每个文件夹创建我的设置:

    "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",
    

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

    tldr;

    我派生了插件以提供 docker-relative 路径和自定义可执行路径到 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. 使用下面的 sn-p 对您的 .zshrc 或 .bashrc 文件进行编辑
    2. 安装 Simple PHP CS Fixer 扩展并使用所需设置进行更新

    对您的 .zshrc 或 .bashrc 进行编辑

    这个source 让我想到了将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 $@
    }
    

    适应我的情况

    我将此粘贴到我的.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 $@
    }
    
    • mycontainername 可以通过以下方式检索:docker ps 并查看最后一列“名称”
    • /var/www/site/app/ 是我的 composer.json 文件所在的项目应用文件夹的容器路径
    • /var/www/site/.php_cs 是我项目的 .php_cs 文件的容器路径,该文件位于我的项目的根目录,例如。 ~/code/project/.php_cs

    安装简单的 PHP CS FIXER

    This extension for vscode 让它变得简单。我假设它只会调用“php-cs-fixer”,并且编辑您的配置文件会找到该功能并为您运行 docker 的东西。

    这是我的设置更改:

    "simple-php-cs-fixer.config": ".php_cs",
    "simple-php-cs-fixer.save": true,
    

    奖励说明:

    我的 .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);
    
    

    【讨论】:

    • 当我了解更多关于使这个更“多项目”友好的信息时,我会更新这个,但现在我的建议是让你的 vscode 设置基于每个文件夹或工作区而不是用户,以避免并发症!
    • 这是我讨厌 Linux 的地方,如果我们想要像你所做的那样,我们必须得到一个可怕的别名,这比安装 PHP 看起来要复杂得多和插件...但是...这就是我们所拥有的...感谢您分享您的解决方案!
    • 是的,但是如果我们必须设置 PHP,必须更改版本以支持不同的项目是很烦人的 - 并且使新/初级开发人员的配置更加困难。我同意这比它应该的要复杂得多。正是出于这种多功能性的原因,我还没有 100% 购买我的解决方案。
    • 不,你只需要本地有PHP,本地也有PHP CS Fixer,那么,对于任何项目,你必须使用docker并使用你喜欢的任何版本,你可以两者都有,但是PHP将在您机器上的 docker 和 PHP CS 上...
    • 你不能只使用 VSCode 远程 WSL 和/或容器扩展并安装 PHP CS 修复程序作为开发依赖项吗?然后只定义在容器内使用的路径?
    猜你喜欢
    • 2017-03-05
    • 1970-01-01
    • 2017-07-05
    • 2018-02-11
    • 2019-08-12
    • 2018-02-12
    • 2019-03-05
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多