trovit/php-code-formatter-bundle

该包已被废弃且不再维护。未建议替代包。

提供组织并执行 PHP 代码格式化器的基本系统

安装: 87

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 9

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.1 2016-10-14 14:05 UTC

This package is not auto-updated.

Last update: 2022-04-02 08:54:02 UTC


README

Build Status

Symfony 包,提供组织并执行 PHP 代码格式化器的基本系统。

安装

步骤 1:使用 composer 需求包

$ composer require trovit/php-formatter-bundle "^1.0"

步骤 2:启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Trovit\PhpCodeFormatterBundle\PhpCodeFormatterBundle(),
        // ...
    );
}

步骤 3:配置包

目前只有 2 个参数可用

  • temporary_path (字符串):临时文件应创建的临时路径。这是对于那些仅与文件系统工作的格式化库所必需的。

  • formatter_services (字符串数组):每个字符串表示一个格式化服务引用名称

示例

# app/config.yml

trovit_php_code_formatter:
    temporary_path: "%kernel.cache_dir%/tmp/"
    formatter_services:
      - 'trovit.php_code_formatter.formatters.php_cs_formatter'

步骤 4(可选):创建自己的格式化器

当你需要格式化代码,并且此包提供的格式化器无法满足你的需求(不同的代码语言、格式等)时,可以通过实现 Trovit\PhpCodeFormatter\Formatters\Formatter 接口来创建一个新的格式化器类,并实现其 formatCode 方法

之后,你需要将格式化器注册为服务,并在配置中添加服务引用名称(查看步骤 3)。

使用

在需要调用 execute 方法并传入未格式化的代码作为参数的地方获取管理服务。它将返回格式化后的代码。

使用 PhpCsFormatter 的示例

// src/AppBundle/Controller/DefaultController.php

$code = '<?php                    
                 echo "hola"; ?>';
$this->get('trovit.php_code_formatter.managers.formatter_manager')->execute($code);

// This will return
/*<?php

        echo 'hola';
*/

可用的格式化器列表

  • PhpCsFormatter(默认配置):由 Fabien Potencier 和 Dariusz Rumiński 提供的 PHP CS Fixer 的包装器

请随意添加更多格式化器并通过 PR 贡献。