Symfony bundle which provides a basic system to organize and execute php code formatters.
$ composer require trovit/php-formatter-bundle "^1.0"
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Trovit\PhpCodeFormatterBundle\PhpCodeFormatterBundle(),
// ...
);
}
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'
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).
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';
*/
- 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!