Java Code Examples for com.jetbrains.php.lang.PhpLangUtil#toPresentableFQN()

The following examples show how to use com.jetbrains.php.lang.PhpLangUtil#toPresentableFQN() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoctrineUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static boolean repositoryClassExists(PhpDocTag phpDocTag)
{
    StringLiteralExpression repositoryClass = AnnotationUtil.getPropertyValueAsPsiElement(phpDocTag, "repositoryClass");
    if(repositoryClass == null || StringUtils.isBlank(repositoryClass.getContents())) {
        return false;
    }

    PhpClass phpClass = PhpElementsUtil.getClassInsideAnnotation(repositoryClass, repositoryClass.getContents());
    if(phpClass != null) {
        return true;
    }

    PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(repositoryClass, PhpDocComment.class);
    if(phpDocComment == null) {
        return false;
    }

    PhpPsiElement phpClassContext = phpDocComment.getNextPsiSibling();
    if(!(phpClassContext instanceof PhpClass)) {
        return false;
    }

    String ns = ((PhpClass) phpClassContext).getNamespaceName();
    String repoClass = repositoryClass.getContents();
    String targetClass;

    if(repoClass.startsWith("\\") || repoClass.startsWith(ns)) {
        targetClass = repoClass;
    } else {
        targetClass = ns + repoClass;
    }

    String classPath = PhpLangUtil.toPresentableFQN(targetClass);
    PhpClass repoPhpClass = PhpElementsUtil.getClass(phpClassContext.getProject(), classPath);

    return repoPhpClass != null;
}
 
Example 2
Source File: DoctrineOrmRepositoryIntention.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {

    PhpClass phpClass = getScopedPhpClass(element);
    if(phpClass == null) {
        return;
    }

    String fileName = phpClass.getName() + "Repository.php";
    String namespace = DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName());
    PsiDirectory dir = phpClass.getContainingFile().getContainingDirectory();

    PsiDirectory repositoryDir = dir;
    if (dir.getParentDirectory() != null) {
        PsiDirectory checkDir = dir.getParentDirectory().findSubdirectory("Repository");

        if (dir.findFile(fileName) == null && checkDir != null) {
            repositoryDir = checkDir;
        }
    }

    String repoClass = phpClass.getPresentableFQN() + "Repository";
    PhpClass repoPhpClass = PhpElementsUtil.getClass(project, repoClass);

    if (repoPhpClass == null && dir != repositoryDir) {
        // Entity/../Repository/
        namespace = phpClass.getNamespaceName();
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace + "\\Repository";
        namespace = DoctrineUtil.trimBlackSlashes(namespace);

        repoClass = namespace + "\\" + phpClass.getName() + "Repository";
        repoClass = PhpLangUtil.toPresentableFQN(repoClass);

        repoPhpClass = PhpElementsUtil.getClass(project, repoClass);
    }

    if(repoPhpClass == null) {
        if(repositoryDir.findFile(fileName) == null) {
            final FileTemplate fileTemplate = FileTemplateManager.getInstance(project).getTemplate("Doctrine Entity Repository");
            final Properties defaultProperties = FileTemplateManager.getInstance(project).getDefaultProperties();

            Properties properties = new Properties(defaultProperties);

            properties.setProperty("NAMESPACE", namespace);
            properties.setProperty("NAME", phpClass.getName() + "Repository");

            properties.setProperty("ENTITY_NAMESPACE", DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName()));
            properties.setProperty("ENTITY_NAME", phpClass.getName());

            try {
                PsiElement newElement = FileTemplateUtil.createFromTemplate(fileTemplate, fileName, properties, repositoryDir);

                new OpenFileDescriptor(project, newElement.getContainingFile().getVirtualFile(), 0).navigate(true);
            } catch (Exception e) {
                return;
            }
        } else {
            if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
                HintManager.getInstance().showErrorHint(editor, "Repository already exists ");
            }
        }
    }

    PhpDocTagAnnotation ormEntityPhpDocBlock = DoctrineUtil.getOrmEntityPhpDocBlock(phpClass);
    if(ormEntityPhpDocBlock != null) {
        PhpDocTag phpDocTag = ormEntityPhpDocBlock.getPhpDocTag();
        PhpPsiElement firstPsiChild = phpDocTag.getFirstPsiChild();
        insertAttribute(editor, repoClass, phpDocTag, firstPsiChild);
    }

}