PHP Beautifier

2011. szeptember 16. 16:14

Automatikus és egységes kódformázás php-ban.

Telepítsük fel a PHP Beautifier PEAR modult:

pear install PHP_Beautifier

Ha szükséges futtassuk az alábbi parancsot:

pear config-set preferred_state beta

Ezek után már csak írnunk kell egy szkriptet ami elvégzi a formázást.
Akár ez is lehet:

#!/usr/bin/php -q
<?php
/**
 * This is a command line PHPBeautifier script.
 *
 * @author 		Sandor Papp
 * @copyright	2011 Sandor Papp
 * @link 		http://spappsite.hu
 * @license		http://creativecommons.org/licenses/by-nc/3.0/
 * @version		0.1
 */
require_once ('PHP/Beautifier.php');
require_once ('PHP/Beautifier/Batch.php');
/**
 * Enabled php file extensions.
 *
 * @var array
 */
$phpFileExtensions = array(
    'php',
    'inc',
    'phtml'
);
/**
 * If it is true then overwriting current files.
 * If it is false then it will write to the stdout.
 *
 * @var boolean
 */
$overwriteCurrentfile = false;
if ($argc < 2) {
    // if not arguments then show help and exit
    $pathinfo = pathinfo($argv[0]);
    $result = array(
        "\033[1;33mThis is a command line PHPBeautifier script.",
        "Usage: \033[0;33m" . $pathinfo['basename'] . "   \033[0m",
        ""
    );
    echo implode("\n", $result);
} else {
    $pb = new PHP_Beautifier();
    // set PHP_Beautifier options
    $pb->setIndentNumber(4);
    $pb->setIndentChar(" ");
    $pb->setNewLine("\n");
    // add some filters
    $pb->addFilter('ArrayNested');
    $pb->addFilter('Pear', array(
        'add_header' => false,
        'newline_class' => false,
        'newline_function' => false,
        'switch_without_indent' => false
    ));
    for ($i = 1; $i < $argc; $i++) {
        $pathinfo = pathinfo($argv[$i]);
        if (in_array($pathinfo['extension'], $phpFileExtensions) and file_exists($argv[$i]) and is_file($argv[$i])) {
            $pb->setInputString(file_get_contents($argv[$i]));
            $pb->process();
            if ($overwriteCurrentfile === true) {
                $pb->save($argv[$i]);
            } else {
                $pb->show();
            }
        }
    }
}
exit(0);
?>

KomodoEdit-eben az alábbi makro-val lehet megkönnyíteni a dolgot:

var cmd = null;
var file = komodo.interpolate('%F');
var currentPos = komodo.editor.currentPos;

try {
    if (komodo.view.scintilla) {
        komodo.view.scintilla.focus();
    }

    if(komodo.document.language === "JavaScript") {
        cmd = "jsbeautify " + file;
    } else if(komodo.document.language === "PHP"){
        cmd = "phpbeautifier.php " + file;
    }
    
    if(cmd !== null) {
        StatusBar_AddMessage("Beautifying " + file, "editor", 5000, true);
        komodo.doCommand('cmd_save');
        komodo.doCommand('cmd_selectAll');
        Run_RunEncodedCommand(window, cmd + " {'insertOutput': True}");
        komodo.editor.gotoPos(currentPos);
        StatusBar_AddMessage("Finished Beautification", "editor", 5000, true);
        komodo.doCommand('cmd_save');
    } else {
        alert("Not a supported file type: " + komodo.document.language);
    }
} catch(aErr) {
    alert(aErr);
}

Ebben a makróban benne van a JavaScript kezelése, de természetesen ki lehet egészíteni és el is lehet venni belőle.

Vissza