Chạy php trên sublime text

Sau 1 ngày vọc Sublime Text 2, dù chỉ xài bản unregistered của nó nhưng thực sự mình cảm thấy rất hay ‘falling in love with Sublime’ !
Nhưng để dùng nó thực sự không dễ, sau đây là 1 số kinh nghiệm của mình, sẽ share ra cho các bạn, ai có idea gì hay chỉ mình với nhé.

1) Tuy là bản unregistered nhưng xài thoải mái, thỉnh thoảng sublime text mới hiện lên pop-up báo là bạn đang dùng thử, vui lòng mua, vậy thôi.

2) Về Plugin của Sublime Text 2 mà mình đang dùng: là 1 dev php, mình cần những plugin để giúp mình hoàn thành công việc, do đó mình chọn plugin sau: Package manager, Alignment, Jdoc, Jslint, CodeIntel (Đặc biệt là thằng này rất hay).
Các Plugin đều rất dễ install và sử dụng, duy chỉ có Jslint và Code Intel là hơi khổ 1 tí.
a) Jslint bạn phải cấu hình cho nó chạy oke. Mình chỉ cấu hình đơn giản như sau:

{

    "jslint_options": "--regexp --white --sloppy"

}


b) Code Intel sau khi install, để chạy nó, bạn cần làm là:
– Tạo 1 project, trong project đó có folder source của bạn. Ví dụ mình tạo 2 folder là A và B. Với A là folder web của mình và B là folder library (có thể là WP, Zend…).


– Bạn chỉ việc alt + Click vào hàm muốn dùng, lần đầu tiên CodeIntel sẽ run toàn bộ 2 folder của bạn, việc này mất khoảng 10p nếu folder rộng. Sau đó 1 là nó sẽ show definition, 2 là open file đó giúp bạn. Khi new File thì nó sẽ load thư viện phù hợp giúp bạn.(Trong trường hợp muốn SublimeCodeIntel scan lại thì vào trong folder: C:Userscomputer-name.codeintel và delete thư mục ‘db’)


3) Học các phím tắt quan trọng của nó như: Ctrl + P, Ctrl + F5(Jslint)… và chuyển các phím tắt mình thường dùng sang Sublime Text 2. Tắt minimap đi vì mình thấy minimap không cần thiết lắm. Bật Wordwrap.

4) Tạo theme, mình dùng color Scheme ‘Mac Classic’ và theme Soda dark. Cuối cùng là cấu hình preferences -> Finish!

5) Tạo regedit cho sublime Text 2 để nó có thể code cho mọi file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT*shellEdit with Sublime Text 2Command]

@=""D:\Your_URL\Software\Sublime Text 2\sublime_text.exe","%1""

6) Tạo validate cho PHP file: Code php mà không validate được thì thật nguy hiểm. Để validate được, đầu tiên ta cần cài PHP CodeSniffer. Cách cài thì follow theo hướng dẫn tạiđây.

1

pear install PHP_CodeSniffer

Sau khi install PHP_CodeSniffer thì ta phải dùng tới công cụ Build System để validate. Vào Tool -> Build System -> New Build System -> Gõ đoạn code này vào (sẽ kiểm tra cả phplint và PHP_CodeSniffer):

{

   "cmd": ["d:/xampp/php/php", "-l", "$file", "&", "d:/xampp/php/phpcs.bat", "-l", "--standard=MyRule", "$file"],

   "shell": true,

   "selector": "source.php",

   "file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)"

}

Thay d:/xampp/php/php và d:/xampp/php/phpcs.bat thành thư mục của bạn.
Sau đó save file này tại: Sublime Text 2DataPackagesUser, đặt tên là php.sublime-build.
Vào lại Sublime Text -> Tool -> Build (Nhớ kiểm tra xem Build System có check là Automatic chưa?)
Trong quá trình build, nếu thấy báo lỗi ‘Unable to load dynamic library…’ thì vào file php.ini để kiểm tra xem extension và browse cap có đặt đúng đường dẫn chưa?

browscap = "d:/xampp/php/extras/browscap.ini"

extension_dir = "d:/xampp/php/ext"

Để tạo MyRule ta tạo folder MyRule và ruleset.xml trong:
phppearPHPCodeSnifferStandardsMyRuleruleset.xml
Config cho standard MyRule trong ruleset.xml ta cần lưu ý như sau:
ref = Generic.Files.LineEndings có nghĩa là ta sẽ gán biến eolChar vào file
phppearPHPCodeSnifferStandardsGenericSniffsFilesLineLengthSniff.php
File ruleset.xml của mình như sau:

xml version="1.0"?>

<ruleset name="MyRule">

 <description>A coding standard based on an early Zend Framework coding standard. Note that this standard is out of date.description>

 <rule ref="Squiz.WhiteSpace.OperatorSpacing"/>

 <rule ref="Squiz.Operators.ComparisonOperatorUsage"/>

 <rule ref="Generic.WhiteSpace.ScopeIndent"/>

 <rule ref="Squiz.Strings.DoubleQuoteUsage"/>

 <rule ref="Generic.PHP.UpperCaseConstant"/>

 <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>

 <rule ref="Zend">

   <exclude name="Generic.Files.LineLength"/>

   <exclude name="Generic.Files.LineEndings"/>

   <exclude name="Zend.NamingConventions.ValidVariableName"/>

   <exclude name="PEAR.Classes.ClassDeclaration"/>

   <exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman"/>

 rule>

ruleset>

Document ruleset.xml tại đây.
Sau khi setup đầy đủ, bấm Ctrl+B để cảm nhận thành quả!

7) Cho phép PHP block hiển thị trong Javascript:


Đầu tiên phải tìm được chỗ cần lấy ra có scope là gì? Để con trỏ chuột vào chỗ cần lấy, gõ Ctrl+Alt+Shift+P sẽ show ra được ở console dòng cuối là: string.quoted.double.js. Chỉ việc tìm đoạn đó và include vào đoạn hỗ trợ PHP code #php. Thực thi như sau:
Tìm file /Sublime Text 2/Data/Packages/JavaScript/JavaScript.tmLanguage. Thêm đoạn này vào gần cuối:

<dict>

    <key>beginkey>

    <string>'string>

    <key>beginCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.begin.jsstring>

        dict>

    dict>

    <key>endkey>

    <string>'string>

    <key>endCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.end.jsstring>

        dict>

    dict>

    <key>namekey>

    <string>string.quoted.single.jsstring>

    <key>patternskey>

    <array>

        <dict>

            <key>matchkey>

            <string>\(xh{2}|[0-2][0-7]{,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)string>

            <key>namekey>

            <string>constant.character.escape.jsstring>

        dict>

        <dict>

            <key>includekey>

            <string>#phpstring>

        dict>

    array>

dict>

<dict>

    <key>beginkey>

    <string>"string>

    <key>beginCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.begin.jsstring>

        dict>

    dict>

    <key>endkey>

    <string>"string>

    <key>endCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.end.jsstring>

        dict>

    dict>

    <key>namekey>

    <string>string.quoted.double.jsstring>

    <key>patternskey>

    <array>

        <dict>

            <key>matchkey>

            <string>\(xh{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)string>

            <key>namekey>

            <string>constant.character.escape.jsstring>

        dict>

        <dict>

            <key>includekey>

            <string>#phpstring>

        dict>

    array>

dict>

...

<key>repositorykey>

<dict>

    <key>phpkey>

    <dict>

        <key>beginkey>

        <string>(?=(^s*)?<?)string>

        <key>endkey>

        <string>(?!(^s*)?<?)string>

        <key>patternskey>

        <array>

            <dict>

                <key>includekey>

                <string>source.phpstring>

            dict>

        array>

    dict>

dict>

Tương tự như vậy, muốn hỗ trợ tag HTML trong php code thì ta làm như sau: Tìm Sublime Text 2/Data/Packages/PHP/PHP.tmLanguage thêm vào include text.html.basic

<key>string-double-quotedkey>

<dict>

    <key>beginkey>

    <string>"string>

    <key>beginCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.begin.phpstring>

        dict>

    dict>

    <key>commentkey>

    <string>This contentName is just to allow the usage of “select scope” to select the string contents first, then the string with quotesstring>

    <key>contentNamekey>

    <string>meta.string-contents.quoted.double.phpstring>

    <key>endkey>

    <string>"string>

    <key>endCaptureskey>

    <dict>

        <key>0key>

        <dict>

            <key>namekey>

            <string>punctuation.definition.string.end.phpstring>

        dict>

    dict>

    <key>namekey>

    <string>string.quoted.double.phpstring>

    <key>patternskey>

    <array>

        <dict>

            <key>includekey>

            <string>#interpolationstring>

        dict>

        <dict>

            <key>includekey>

            <string>text.html.basicstring>

        dict>

    array>

dict>

8) Cho phép mouse select trong 1 click:
Chỉnh tại: Packages/User/Default (Windows).sublime-mousemap

[

    {

        "button": "button1", "count": 1, "modifiers": ["ctrl"],

        "press_args": {"by": "words"},

        "press_command": "drag_select"

    }

]

Tóm lại Sublime Text 2 là 1 text editor tuyệt vời mà mình nghĩ nếu ai biết nó cũng sẽ LOVE.