[英]How to use php-cs-fixer in vscode with docker container for php and composer
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。
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.
我无法让它工作。
friendsofphp/php-cs-fixer
installed with composer required for devfriendsofphp/php-cs-fixer
/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
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
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 :
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)进行键绑定,所以我可以控制何时想要完全删除我的文件。
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 - 但如果这不被接受......你可以像侧面加载扩展所以:
git clone git@github.com:amurrell/simple-php-cs-fixer.git
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 根据我的配置获得一个固定文件。
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):
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 $@
}
docker ps
and looking at last column "Names" 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
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,
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.