Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get directory path in PhpStorm Live Template

Tags:

phpstorm

I'm trying to generate a namespace through PhpStorm's templating system (File and Code Templates), I can't see any helpers / means of getting this through PhpStorm's documentation.

I was wondering if there was a way to automatically define the namespace based on the directory:

<?php
// I'm wanting to apply to $namespace
namespace $namespace;

class ${NAME} implements
    \Zend\Mail\Mailable
{
    /** \Zend\Mail\Factory */
    private $factory;
...

}#set( $factory = "factory" ) #set( $this- = "this-" ) #set( $directory = ?)

If my directory/filename is zend/mail/model/notice/send.php I'd like this to generate a namespace of Zend\Mail\Model\Notice; I can get the class name through the ${NAME} variable.

I'm aware I can have the template request the namespace through a dialog when the file is being created, however I was wondering if it's possible to automate this?

like image 895
Jack hardcastle Avatar asked Jan 22 '26 12:01

Jack hardcastle


1 Answers

if you want , you can use groovyScript.

for example, you can create a variable $FOLDER_OF_FILE$ and set this groovyScript to take the folder of file.

groovyScript("String[] p=_editor.getVirtualFile().getPath().split('/');String prevLast = p[p.length-2];")

you can change and adapt my example for your needs

like image 69
Luca Bottoni Avatar answered Jan 27 '26 00:01

Luca Bottoni