简体   繁体   English

如何在 vscode 中使用 php-cs-fixer 和 docker 容器,用于 php 和作曲家

[英]How to use php-cs-fixer in vscode with docker container for php and composer

Goal目标

I want to use php-cs-fixer +.php_cs config file for defining rules - without having to install PHP on my host machine.我想使用 php-cs-fixer +.php_cs 配置文件来定义规则 - 而不必在我的主机上安装 PHP。


Issue问题

I am using PHP7.3 inside of a docker container - this is also where composer is installed.我在 docker 容器内使用 PHP7.3 - 这也是安装 composer 的地方。 This means I do not have php running on my local machine.这意味着我的本地计算机上没有运行 php。

Extensions for vscode - like junstyle's php-cs-fixer seemed to require that the executable, config file, and current working directory are localized. vscode 的扩展——比如 junstyle 的php-cs- fixer 似乎需要对可执行文件、配置文件和当前工作目录进行本地化。 I could not get this to work.我无法让它工作。


Context语境

  • Running local development with docker container for php & composer (docker for mac, using docker-compose)使用 php 和作曲家的 docker 容器运行本地开发(docker for mac,使用 docker-compose)
  • Project is a Laravel API - has friendsofphp/php-cs-fixer installed with composer required for dev项目是 Laravel API - 安装了开发人员所需的friendsofphp/php-cs-fixer
  • Using vscode, with extension: Simple PHP CS Fixer使用 vscode,带扩展名: Simple PHP CS Fixer
  • I have a.php_cs config file at the root of my project我的项目根目录中有一个 .php_cs 配置文件
  • I have successfully ran /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} as a proof of concept from within the docker container.我已成功运行/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 容器内的概念证明。 It worked perfectly.它工作得很好。
    • /var/www/site/ is the path inside the container that maps to my ~/code/project on my host machine /var/www/site/是容器内的路径,它映射到我的主机上的~/code/project

"Close, but not close enough" Solution “接近,但不够接近”解决方案

This blog post gave a tip about making a script eg `/usr/local/bin/docker-phpcs with the following contents (and chmod +x to make it executable)这篇博客文章提供了一个关于制作脚本的技巧,例如 `/usr/local/bin/docker-phpcs 具有以下内容(和 chmod +x 使其可执行)

#!/bin/bash

/usr/bin/docker-compose exec -T app-php /var/www/vendor/bin/php-cs-fixer "$@"

But, I couldn't get this to work, even when defining the vscode setting for the junstyle extension from "php-cs-fixer.executablePath": "php-cs-fixer" to "php-cs-fixer.executablePath": "/usr/local/bin/docker-phpcs",但是,即使将 junstyle 扩展的 vscode 设置从"php-cs-fixer.executablePath": "php-cs-fixer"定义为“php-cs-fixer”到"php-cs-fixer.executablePath": "/usr/local/bin/docker-phpcs",

This gave me a ton of:这给了我很多:

[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

Update: New Solution更新:新解决方案

Skip to the tldr;跳到tldr; section below for direct solution.下面的部分直接解决。 Otherwise, enjoy:否则,享受:

I had to accept that there were no php-cs-fixer vscode plugins that would work with solving the path difference between my host machine and docker container.我不得不接受没有 php-cs-fixer vscode 插件可以解决我的主机和 docker 容器之间的路径差异。

I got deep into the weeds and I forked Simple PHP CS FIXER adding a few more configuration options:我深入了解杂草,并分叉了Simple PHP CS FIXER ,添加了更多配置选项:

  • executablePath - relative to my host machine so I could use a script in my project. executablePath - 相对于我的主机,所以我可以在我的项目中使用脚本。 eg.例如。 /Users/me/myproject/docker-phpcs - this project was originally hardcoding php-cs-fixer here. /Users/me/myproject/docker-phpcs - 这个项目最初是在这里硬编码php-cs-fixer fixer。 That is still the default in my version.这仍然是我版本中的默认设置。

    Here's an example script:这是一个示例脚本:

     #!/bin/bash docker exec -t "mycontainer" /var/www/site/app/vendor/bin/php-cs-fixer $@

    Note: I made sure to chmod +x on my script to make it executable注意:我确保在我的脚本上chmod +x以使其可执行

  • hostPath - so I could tell it my project path eg. hostPath - 所以我可以告诉它我的项目路径,例如。 "/Users/me/code/myproject/" “/用户/我/代码/我的项目/”

  • dockerPath - so I could tell it my docker container path eg. dockerPath - 所以我可以告诉它我的 docker 容器路径,例如。 "/var/www/site" “/var/www/站点”

Then I modified the extension.js to replace the hostName with the dockerPath in the following files:然后我修改了 extension.js 以将以下文件中的hostName替换dockerPath

  • the config file , if one is set.配置文件,如果设置了。
  • the document-being-saved's fileName.正在保存的文档的文件名

So when using this, I create my settings per-folder like this:因此,当使用它时,我为每个文件夹创建我的设置,如下所示:

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

And I purposefully do not force this on save - and instead made a key-binding for the plugin's command (ctrl+s, instead of cmd + s), so I can control when I want to completely obliterate my file.而且我故意不强制保存 - 而是为插件的命令(ctrl + s,而不是 cmd + s)进行键绑定,所以我可以控制何时想要完全删除我的文件。

tldr; tldr;

I forked the plugin to provide options for docker-relative paths and custom executable path to php-cs-fixer - I also did make a PR to the plugin - but if that does not get accepted... you can side load the extension like so:我分叉了插件以提供 docker-relative 路径和 php-cs-fixer 的自定义可执行路径的选项 - 我也确实对插件进行了 PR - 但如果这不被接受......你可以像侧面加载扩展所以:

  1. "install" the plugin using marketplace as normal像往常一样使用市场“安装”插件
  2. go to your extensions folder (on my mac it was $HOME/.vscode/extensions go 到您的扩展文件夹(在我的 Mac 上是 $HOME/.vscode/extensions
  3. git clone git@github.com:amurrell/simple-php-cs-fixer.git
  4. remove the one installed by marketplace like: calebporzio.simple-php-cs-fixer-xxx删除由市场安装的一个,例如: calebporzio.simple-php-cs-fixer-xxx

Hopefully PR gets accepted...希望PR被接受...

In the meantime, with ctrl+s key-binding and this modified plugin with extra settings, I now get a fixed file according to my configuration, using my docker-container.同时,通过 ctrl+s 键绑定和这个带有额外设置的修改插件,我现在使用我的 docker-container 根据我的配置获得一个固定文件。


Another Close Solution另一个关闭的解决方案

Update : This was not really accurately solving my problem - my script would trigger when saving but it wasn't actually processing the current document file being edited/saved - it was triggering the script which would scan all my files defined in my configuration file.更新:这并没有真正准确地解决我的问题 - 我的脚本会在保存时触发,但它实际上并没有处理正在编辑/保存的当前文档文件 - 它正在触发将扫描我在我的配置文件中定义的所有文件的脚本。 This means the plugin wasn't getting to control the configuration file either.这意味着插件也无法控制配置文件。


I got what I wanted.我得到了我想要的。 Assuming the context I have in my question (eg: your project has friendsofphp/php-cs-fixer required in composer):假设我在问题中的上下文(例如:您的项目在作曲家中需要friendsofphp/php-cs-fixer):

  1. Make an edit to your.zshrc or.bashrc file with snippet below使用下面的代码段对您的 .zshrc 或 .bashrc 文件进行编辑
  2. Install the Simple PHP CS Fixer extension & update with desired settings安装 Simple PHP CS Fixer 扩展并使用所需设置进行更新

Make Edit to your.zshrc or.bashrc对 your.zshrc 或 .bashrc 进行编辑

This source gave me the idea to store php-cs-fixer in my .zshrc or .bashrc file like this:这个来源让我想到了将php-cs-fixer .zshrc在我的 .zshrc 或.bashrc文件中,如下所示:

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

Adapted to my circumstance :适应我的情况

I pasted this into the bottom of my .zshrc file:我将它粘贴到我的.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 can be retrieved by: docker ps and looking at last column "Names" mycontainername 可以通过以下方式检索: docker ps并查看最后一列“名称”
  • /var/www/site/app/ is the container's path to my project's app folder where my composer.json file is /var/www/site/app/是我的 composer.json 文件所在的项目应用程序文件夹的容器路径
  • /var/www/site/.php_cs is the container's path to my project's.php_cs file which is at the root of my project eg. /var/www/site/.php_cs是我项目的.php_cs 文件的容器路径,该文件位于我的项目的根目录,例如。 ~/code/project/.php_cs

Install Simple PHP CS FIXER安装简单 PHP CS FIXER

This extension for vscode makes it easy. vscode 的这个扩展使它变得容易。 I assume it will just call "php-cs-fixer" and the edit your profile will find that function and run the docker stuff for you.我假设它只会调用“php-cs-fixer”并且编辑您的配置文件会发现 function 并为您运行 docker 的东西。

Here are my setting changes:这是我的设置更改:

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

BONUS Note:奖金注意:

My.php_cs file in case anyone is curious - I did make my project_path relative to the container and not my host machine. My.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);

As part of a package solution for overall application code quality control ( interitty/code-checker ), I solved the situation of running a php-cs-fixer inside a docker container from NetBeans IDE on a guest machine. As part of a package solution for overall application code quality control ( interitty/code-checker ), I solved the situation of running a php-cs- fixer inside a docker container from NetBeans IDE on a guest machine.

So, I created a bash script that emulates the php-cs-fixer interface and passes the parameters to the specified container.因此,我创建了一个bash 脚本,该脚本模拟 php-cs-fixer 接口并将参数传递给指定的容器。

Related documentation can be found in the readme相关文档可以在自述文件中找到

I hope that this should help you too.我希望这也能帮助你。

I come with my 2 cents for vscode, as I gave up on using any php-cs-fixer related extension.我为 vscode 带来了 2 美分,因为我放弃了使用任何 php-cs-fixer 相关的扩展。

Install this extensions https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave .安装此扩展https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave

Basically, it allows you to run any command when saving a file;基本上,它允许您在保存文件时运行任何命令; it is not targeted specifically to php fixer它不是专门针对 php 固定器的

Then, in workspace's or folder's settings, setup a command to forward instruction on saved file, using extension's placeholder.然后,在工作区或文件夹的设置中,使用扩展名的占位符设置命令以转发已保存文件的指令。 {containerName} is the name of the container you want to forward command to, ${relativeFile} is the extension's placeholder for the relative path of the saved file. {containerName}是您要将命令转发到的容器的名称, ${relativeFile}是已保存文件的相对路径的扩展占位符。

"emeraldwalk.runonsave": {
    "commands": [
        {
            "match": ".php",
            "cmd": "docker exec -i {containerName} /var/www/html/tools/php-cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --format=json fix ${relativeFile}"
        },
    ]
}

Make sure to set the appropriate path (inside the container) of php-cs-fixer executable.确保设置 php-cs-fixer 可执行文件的适当路径(在容器内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM