Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automatically generate the namespace & class depending on where the file is saved
File is located at:
No way to double-substitute or chain filters
Expected Output:
namespace MyApp\Bundle\BlogBundle\Entity\Blog;
class Post
Actual Output:
namespace MyApp/Bundle/BlogBundle/Entity/Blog;
class Post
namespace ${TM_FILEPATH/^.+\/[a-z]+\/([\w\/]+)\/\w+\.php$/$1/};
class ${TM_FILENAME/^([\w\/]+)\.php$/$1/}

This comment has been minimized.

Copy link

commented Jan 19, 2018

I had a similar problem, and finally I managed to solve it. For you it should look like this:

namespace MyApp${TM_FILEPATH/(?:.*MyApp)|(\/)([^\/]+)(?=\/)|(?:\/[^\/]+\.php$)/(?1:\\$^N:$^N)/g};

For me using Laravel, the path is /home/user/git/laravel-project/app/... and should be replaced by App:

namespace App${TM_FILEPATH/(?:.*app)|(\/)([^\/]+)(?=\/)|(?:\/[^\/]+\.php$)/(?1:\\$^N:$^N)/g};

I've taken the solution from Sublime Text snippet to insert PSR-0 namespace and adjusted it just a little bit.

The substitution for the classname could also be done a little bit shorter (just strip the .php suffix):

class ${TM_FILENAME/\.php$//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.