I'm trying to make a Sublime Text-snippet that inserts a PHP boilerplate class, in the lines of:
<?php
namespace Namespace\Subnamespace;
class TestClass
{
public function __construct()
{
//code...
}
}
When using PHP-FIG standards(or similar), both the namespace and classname can be obtained from the path of the file. The file in the example above would be placed in /Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
.
This is what I have so far:
<snippet>
<content><![CDATA[
<?php
namespace ${1:Namespace};
class ${TM_FILENAME/(.*)[.](.*)/$1/g}
{
public function __construct()
{
${0://code...}
}
}
]]></content>
<tabTrigger>phpclass</tabTrigger>
<scope>text.html</scope>
</snippet>
I've already figured out how to get the classname - but getting the namespace has proven to be much more difficult. I'm far from an expert in regexes - and this one requires:
- Getting everything after
src/
- ...and before the last
/
- Flipping all the remaining slashes to backslashes.
/Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
becomes Namespace\Subnamespace
.
This is the most relevant thread I've found on the subject, but it is way over my head, and I couldn't even get it working.
Can anyone help me with this?