In my previous post I wrote about a regexp search and replace to create Doctrine entities easily from a phpmyadmin CSV export. After that post I found myself checking my own website to copy and paste the regexp. Then I tried recording a macro in Sublime, but it turns out sublime doesn’t record search and replace actions.
Then I learnt about the feature to install packages to your sublime install, which has a really cool RegReplace package. Just follow the instructions on that page. I was a bit confused at point three, but turns out I had to go to Package Control: install package. You can go there by pressing cmd+shift+p and then type Package Control. It should appear in the list.
Then this superuser post helped me configuring the RegReplace package. It took me a while to figure out how to use extended back references. But I got it all working now. I use Sublime Text 2, so go to Preferences → Package Settings → Reg Replace → Settings – Default.
At the replacements section I added this:
1 2 3 4 5 6 7 8 9 10 |
"comma_to_newline": { "find": ",", "replace": "\\n", "greedy": true }, "doctrine_entity": { "find": "^\"(.*)\"$", "replace": "\/**\n * @ORM\\\\Column(name=\"\\1\", type=\"\", nullable=false)\n */\nprivate $\\l\\1;\n", "greedy": true } |
Don’t forget to enable “extended_back_reference” at the end of this file.
Then I went to Preferences → Package Settings → Reg Replace → Commands – Default and added this at the end:
1 2 3 4 5 6 7 8 9 10 |
{ "caption": "CSV to Doctrine Entity", "command": "reg_replace", "args": { "replacements": [ "comma_to_newline", "doctrine_entity" ] } } |
Now when I am in my editor and press cmd+shift+p I can just type “Doctrine” and my command shows up. Then it’ll convert the cvs formatted table heads to a nice doctrine entity.