Plugin which guesses the namespace of the current file from composer.json
.
Note that the algorithm for determining the namespace is not 100% accurate (although it could be) but it should work in the majority of cases.
It should work with both psr-0
and psr-4
projects.
With Vundle:
Plugin "dantleech/vim-phpnamespace"
Map it:
nnoremap <silent><leader>nn :call PhpNamespaceInsert()<CR>
Call it:
let s:foo = PhpNamespaceGet()
Use it with ultisnips to generate class / interface templates:
snippet interface "Interface declaration template" !b
<?php
namespace ${1:`!v PhpNamespaceGet()`};
interface ${1:`!v expand("%:t:r")`}
{
$0;
}
endsnippet
snippet class "Class declaration template" !b
<?php
namespace ${1:`!v PhpNamespaceGet()`};
class ${1:`!v expand("%:t:r")`}
{
$0;
}
endsnippet