LoginSignup
226
226

More than 5 years have passed since last update.

VimでPHP開発環境を作成

Last updated at Posted at 2014-08-16

チップスは概要のみ掲載しているため、詳細は.vimrcを参照して下さい。

Vim本体設定

PHP設定

.vimrc
" $VIMRUNTIME/syntax/php.vim
let g:php_baselib       = 1
let g:php_htmlInStrings = 1
let g:php_noShortTags   = 1
let g:php_sql_query     = 1

DB設定

おそらくデフォルトでは'sqloracle'になっているかと思われます。使用しているDBが異なる場合には、使用しているDBに変更する事でsyntax highlight機能がいい感じに機能するようになります。

.vimrc
" $VIMRUNTIME/syntax/sql.vim
let g:sql_type_default = 'mysql' " MySQLの場合

LSP設定

Wikiintelephenseがおすすめとありますので、

そちらをインストールします。

npm -g install intelephense

Composer設定

JetBrains社製のPHPStormの関数情報をグローバルにインストールします。

$ composer global require "jetbrains/phpstorm-stubs:dev-master"

Plugin設定

ディレクトリ作成

$ mkdir -p \
    $HOME/.vim/dein.vim \
    $HOME/.vim/neosnippet.vim \
    $HOME/.vim/tags

Pluginインストール

Neobundle作者のShougoさんの新しいプラグイン管理プラグイン「dein.vim」を設定します。

.vimrc
let s:deinDir    = !exists('s:deinDir') ? $HOME . '/.vim/dein.vim' : s:deinDir
let &runtimepath = &runtimepath . ',' . s:deinDir . '/repos/github.com/Shougo/dein.vim'
" dein.vim
if dein#load_state(s:deinDir)
    call dein#begin(s:deinDir)
    call dein#load_dict({
    \    'KazuakiM/neosnippet-snippets':               {},
    \    'prabirshrestha/async.vim':                   {},
    \    'prabirshrestha/asyncomplete.vim':            {},
    \    'prabirshrestha/asyncomplete-file.vim':       {},
    \    'prabirshrestha/asyncomplete-lsp.vim':        {},
    \    'prabirshrestha/asyncomplete-neosnippet.vim': {},
    \    'prabirshrestha/vim-lsp':                     {},
    \    'Shougo/dein.vim':                            {},
    \    'Shougo/neosnippet.vim':                      {},
    \    'Shougo/vimproc.vim':                         {'build': 'make'},
    \    'StanAngeloff/php.vim':                       {'on_ft': 'php'},
    \    'thinca/vim-quickrun':                        {},
    \})
    call dein#end()
    call dein#save_state()
endif
filetype plugin indent on
syntax enable

Universal Ctags設定

tagsファイルを適切に生成することでタグジャンプが可能となり、時間短縮が期待できます。

設定ファイル作成

Universal Ctags設定ファイルの設置位置が変わりました。

詳しくはmanページをご参照下さい。

_.ctags
--exclude=*min.js
--input-encoding=UTF-8
--langmap=PHP:+.php.inc.tpl
--pattern-length-limit=300
--recurse=yes

alias設定

.bashrc.local
#.bashrc.localみたいな感じでGitHub管理外でよしなに管理する
tagsPhpCmd='ctags --languages=php -f'
tagsVariable=''
tagsVariable="$tagsVariable cd $HOME/sample1; $tagsPhpCmd $HOME/.vim/tags/sample1.tags $HOME/sample1;"
tagsVariable="$tagsVariable cd $HOME/sample2; $tagsPhpCmd $HOME/.vim/tags/sample2.tags $HOME/sample2;"
tagsVariable="$tagsVariable cd;"
alias TAGS=$tagsVariable

.vimrc設定

.vimrc
".vimrc.localみたいな感じでGitHub管理外でよしなに管理する
augroup MyTagsAutoCmd
    autocmd!
    autocmd BufNewFile,BufRead $HOME/sample1/*.php setlocal tags=$HOME/.vim/tags/sample1.tags
    autocmd BufNewFile,BufRead $HOME/sample2/*.php setlocal tags=$HOME/.vim/tags/sample2.tags
augroup END

LSP環境構築

LSPでは以下の機能を実現してもらいます。LSPにより多くのプログラミング言語において特殊な設定が不要となります。
* 関数説明(ポップアップ)
* 補完機能
* Syntaxチェック

.vimrc
" asyncomplete-neosnippet.vim
" neosnippet-snippets
" neosnippet.vim
" asyncomplete-file.vim
" asyncomplete-lsp.vim
" vim-lsp
" asyncomplete.vim
let g:lsp_preview_keep_focus = 0
nmap <silent> <Subleader>N <Plug>(lsp-previous-error)
nmap <silent> <Subleader>n <Plug>(lsp-next-error)
nmap <silent> <Subleader>b <Plug>(lsp-document-diagnostics)
nmap <silent> K <Plug>(lsp-hover)
augroup MyAsyncomplete
    autocmd!
    autocmd User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#neosnippet#get_source_options({
    \   'name': 'neosnippet',
    \   'completor': function('asyncomplete#sources#neosnippet#completor'),
    \   'whitelist': ['*'],
    \ }))

    autocmd User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#file#get_source_options({
    \   'name': 'file',
    \   'completor': function('asyncomplete#sources#file#completor'),
    \   'priority': 10,
    \   'whitelist': ['*'],
    \ }))

    autocmd User lsp_setup call lsp#register_server({
    \ 'name': 'php',
    \ 'cmd': {server_info->['node', expand('/usr/local/lib/node_modules/intelephense/lib/intelephense.js'), '--stdio']},
    \ 'initialization_options': {'storagePath': s:envHome . '.cache/intelephense'},
    \ 'whitelist': ['php'],
    \ })
augroup END

Vim内でPHPプログラム実行設定

Quickrunを応用し、円滑にコマンドを実行できるようにします。

.vimrc
" vim-quickrun
nnoremap <Leader>run  :<C-u>QuickRun<CR>
nnoremap <Leader>phpf :<C-u>QuickRun<Space>phpfixer<CR>
nnoremap <Leader>phpi :<C-u>QuickRun<Space>phpinfo<CR>
nnoremap <Leader>phpt :<C-u>QuickRun<Space>phpunit<CR>
let g:quickrun_config = {
\    '_': {
\        'hook/close_buffer/enable_empty_data': 0,
\        'hook/close_buffer/enable_failure':    0,
\        'outputter':                           'multi:buffer:quickfix',
\        'outputter/buffer/close_on_empty':     1,
\        'outputter/buffer/split':              ':botright',
\        'runner':                              'job'
\    },
\    'php': {
\        'command':                             'php',
\        'exec':                                '%c %s',
\        'hook/close_buffer/enable_empty_data': 0,
\        'hook/close_buffer/enable_failure':    0,
\        'outputter':                           'buffer',
\        'outputter/buffer/close_on_empty':     0,
\        'outputter/buffer/into':               0,
\        'outputter/buffer/split':              ':botright 7sp'
\    },
\    'phpfixer': {
\        'command':                'php-cs-fixer',
\        'cmdopt':                 'fix',
\        'exec':                   '%c %o %s:p',
\        'outputter':              'buffer',
\        'outputter/buffer/into':  1,
\        'outputter/buffer/split': ':botright 7sp',
\        'runner':                 'system'
\    },
\    'phpinfo': {
\        'command':   'php',
\        'cmdopt':    '-info',
\        'exec':      '%c %o',
\        'outputter': 'buffer'
\    },
\    'phpunit': {
\        'command':   'phpunit',
\        'exec':      '%c %s',
\        'outputter': 'buffer'
\    },
\}
226
226
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
226
226