Skip to content

Latest commit

 

History

History
79 lines (52 loc) · 2.32 KB

README.md

File metadata and controls

79 lines (52 loc) · 2.32 KB

Php Code Formatter Bundle

Build Status

Symfony bundle which provides a basic system to organize and execute php code formatters.

Installation

Step 1: Require bundle using composer

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

Step 2: Enable the bundle

<?php
// app/AppKernel.php

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

Step 3: Configure the bundle

There are only 2 parameters available at the moment:

  • temporary_path (string): temporary path where the temporary files should be created. This is necessary for those formatter libraries that only works with filesystem.

  • formatter_services (string[]): each string represents the reference name of a formatter service

Example:

# app/config.yml

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

Step 4 (optional): Create your own Formatter

When you need to format your code and the formatters provided by this bundle doesn't satisfy your needs (different code language, formats, etc...) there is the possibility to create a new Formatter class by implementing the Formatter interface (Trovit\PhpCodeFormatter\Formatters\Formatter) and implement its method formatCode

After that, you have to register the formatter as a service and add the service reference name in the config (check step 3).

Usage

Get the manager service wherever you want to call the method execute with the bad formatted code as a parameter. It will return the formatted code.

Example with the 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';
*/

List of available formatters

  • PhpCsFormatter (with default configuration): Wrapper of PHP CS Fixer by Fabien Potencier & Dariusz Rumiński

Feel free to add more formatters and contribute by PR!