更新:新解决方案
跳至下面的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 以在以下文件中替换 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",
我故意不强制保存 - 而是为插件的命令(ctrl+s,而不是 cmd + s)进行了键绑定,所以我可以控制何时要完全删除我的文件。
tldr;
我派生了插件以提供 docker-relative 路径和自定义可执行路径到 php-cs-fixer 的选项 - 我也确实对插件进行了 PR - 但如果这不被接受......你可以侧面加载像这样的扩展:
- 正常使用市场“安装”插件
- 转到您的扩展文件夹(在我的 Mac 上是 $HOME/.vscode/extensions
git clone git@github.com:amurrell/simple-php-cs-fixer.git
- 删除由市场安装的一个,例如:
calebporzio.simple-php-cs-fixer-x.x.x
希望 PR 被接受...
与此同时,通过 ctrl+s 键绑定和这个带有额外设置的修改插件,我现在使用我的 docker-container 根据我的配置获得一个固定文件。
另一个关闭解决方案
更新:这并没有真正准确地解决我的问题 - 我的脚本会在保存时触发,但它实际上并没有处理当前正在编辑/保存的文档文件 - 它正在触发脚本扫描我在我的配置文件中定义的所有文件。这意味着插件也无法控制配置文件。
我得到了我想要的!假设我在我的问题中的上下文(例如,您的项目在作曲家中需要friendsofphp/php-cs-fixer):
- 使用下面的 sn-p 对您的 .zshrc 或 .bashrc 文件进行编辑
- 安装 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);