Last active
July 7, 2016 03:23
-
-
Save mattstauffer/7989012 to your computer and use it in GitHub Desktop.
Vimscripts to easily create PHP class and easily add dependency to PHP class
Tweaked versions of a script by Jeffrey Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Note: To add this to your .vimrc, you'll have to delete any character that is written with a caret (e.g. "^M") and enter the real value; to enter ^M, enter insert mode and type CTRL-V CTRL-M. | |
" Easily create class. | |
function! Class() | |
let name = input('Class Name? ') | |
let namespace = input('Any Namespace? ') | |
if strlen(namespace) | |
exec 'normal i<?php namespace ' . namespace . ';^M^M^[' | |
else | |
exec 'normal i<?php^M^M^[' | |
endif | |
" Open class | |
exec 'normal iclass ' . name . ' {^M}^[O^[' | |
exec 'normal i^M public function __construct()^M {^M ^M }^M^[kkA' | |
endfunction | |
nmap <leader>1 :call Class()<cr> | |
function! AddDependency() | |
let dependency = input('Var Name: ') | |
let namespace = input('Class Path: ') | |
let segments = split(namespace, '\') | |
let typehint = segments[-1] | |
exec 'normal gg/construct^M%i, ' . typehint . ' $' . dependency . '^[/}^MO$this->^[a' . dependency . ' = $' . dependency . ';^[==o^[?{^MkO protected $' . dependency . ';^M^[?{^MOuse ' . namespace . ';^M^[' | |
" Remove opening comma if there is only one dependency | |
exec 'normal :%s/(, /(/g^M' | |
endfunction | |
nmap <leader>2 :call AddDependency()<cr> |
Using the e
flag when removing the opening comma if there is only one dependency on line 31 works better for me, as I get errors when no (,
is found.
exec 'normal :%s/(, /(/ge^M'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You for adding this in Laracast comments.