├── .gitignore ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── PhpBeautifier.sublime-commands ├── README.md └── php_beautifier.py /.gitignore: -------------------------------------------------------------------------------- 1 | php_beautifier.pyc 2 | -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_beautifier"} 3 | ] 4 | -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_beautifier"} 3 | ] 4 | -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_beautifier"} 3 | ] 4 | -------------------------------------------------------------------------------- /PhpBeautifier.sublime-commands: -------------------------------------------------------------------------------- 1 | [ 2 | { "caption": "Format: Php", "command": "php_beautifier" } 3 | ] 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP code formatter for Sublime Text editor with Pear php_beautifier 2 | #### [Sublime Text 2](http://www.sublimetext.com/2) 3 | #### [Php_beautifier](http://pear.php.net/package/PHP_Beautifier) 4 | 5 | ## About 6 | This is a Sublime Text 2 plugin allowing you to format your PHP code. 7 | It uses php beautifier. 8 | 9 | ## Installation 10 | Install php-pear and php-cli with your package manager : 11 | * php-pear & php5-cli with Debian 12 | * php-pear & php with Archlinux 13 | 14 | Install php beautifier from pear channel : 15 | `sudo pear install --alldeps channel://pear.php.net/php_beautifier-0.1.15` 16 | 17 | Clone or download the files and copy them to your `Packages` folder. You can access it via Preferences -> Browse Packages in sublime text. 18 | 19 | ## Usage 20 | ctrl + shift + P and type `Format: PHP`, or you can use the ctrl + alt + f keybinding. 21 | 22 | ## Customize 23 | You can define some options in the script `php_beautifier.py`. 24 | Default options are : `PHP_OPTIONS = "-s4 -l 'ArrayNested()' "` (indent with 4 spaces and nested arrays). 25 | 26 | -------------------------------------------------------------------------------- /php_beautifier.py: -------------------------------------------------------------------------------- 1 | import re 2 | import os 3 | import subprocess 4 | 5 | import sublime 6 | import sublime_plugin 7 | 8 | 9 | class PhpBeautifierCommand(sublime_plugin.TextCommand): 10 | def run(self, edit): 11 | # Test environment 12 | if self.view.is_scratch(): 13 | return 14 | 15 | if self.view.is_dirty(): 16 | return sublime.status_message("Please save the file.") 17 | 18 | FILE = self.view.file_name() 19 | if not FILE or not os.path.exists(FILE): 20 | return self.status("File does not exist.") 21 | 22 | if not FILE[-3:] == 'php': 23 | return self.status("File does not have php extension.") 24 | 25 | # Start doing stuff 26 | cmd = "php_beautifier" 27 | indent = "-s4" 28 | filters = "ArrayNested() NewLines(before=switch:while:for:foreach:T_CLASS:return:break) Pear(add-header=false)" 29 | 30 | allFile = sublime.Region(0, self.view.size()) 31 | AllFileText = self.view.substr(allFile).encode('utf-8') 32 | 33 | if os.name == 'nt': 34 | startupinfo = subprocess.STARTUPINFO() 35 | startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW 36 | startupinfo.wShowWindow = subprocess.SW_HIDE 37 | p = subprocess.Popen([cmd, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo) 38 | else: 39 | p = subprocess.Popen([cmd, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 40 | stdout, stderr = p.communicate(AllFileText) 41 | if len(stderr) == 0: 42 | self.view.replace(edit, allFile, self.fixup(stdout)) 43 | else: 44 | self.show_error_panel(self.fixup(stderr)) 45 | 46 | # Error panel & fixup from external command 47 | # https://github.com/technocoreai/SublimeExternalCommand 48 | def show_error_panel(self, stderr): 49 | panel = self.view.window().get_output_panel("php_beautifier_errors") 50 | panel.set_read_only(False) 51 | edit = panel.begin_edit() 52 | panel.erase(edit, sublime.Region(0, panel.size())) 53 | panel.insert(edit, panel.size(), stderr) 54 | panel.set_read_only(True) 55 | self.view.window().run_command("show_panel", {"panel": "output.php_beautifier_errors"}) 56 | panel.end_edit(edit) 57 | 58 | def fixup(self, string): 59 | return re.sub(r'\r\n|\r', '\n', string.decode('utf-8')) 60 | --------------------------------------------------------------------------------