trovit / php-code-formatter-bundle
Provides a basic system to organize and execute php code formatters
Installs: 89
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 9
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- symfony/framework-bundle: ^2.8|^3.1
- trovit/php-code-formatter: ^1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2022-04-02 08:54:02 UTC
README
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!