Java Code Examples for com.jetbrains.php.lang.psi.PhpPsiElementFactory#createPsiFileFromText()

The following examples show how to use com.jetbrains.php.lang.psi.PhpPsiElementFactory#createPsiFileFromText() . 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: DoctrineUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see DoctrineUtil#getClassRepositoryPair
 */
public void testGetClassRepositoryPairForStringValue() {
    PsiFile psiFileFromText = PhpPsiElementFactory.createPsiFileFromText(getProject(), "" +
        "<?php\n" +
        "\n" +
        "namespace Foo;\n" +
        "\n" +
        "use Doctrine\\ORM\\Mapping as ORM;\n" +
        "\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=\"MyBundle\\Entity\\Repository\\AddressRepository\")\n" +
        " */\n" +
        "class Apple {\n" +
        "}\n"
    );

    Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);

    Pair<String, String> next = classRepositoryPair.iterator().next();

    assertEquals("Foo\\Apple", next.getFirst());
    assertEquals("MyBundle\\Entity\\Repository\\AddressRepository", next.getSecond());
}
 
Example 2
Source File: DoctrineUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see DoctrineUtil#getClassRepositoryPair
 */
public void testGetClassRepositoryPairForClassConstant() {
    myFixture.configureByText(PhpFileType.INSTANCE, "<?php class Foobar {};");

    PsiFile psiFileFromText = PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "\n" +
        "namespace Foo;\n" +
        "\n" +
        "use Doctrine\\ORM\\Mapping as ORM;\n" +
        "use Foobar;\n" +
        "\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=Foobar::class)\n" +
        " */\n" +
        "class Apple {\n" +
        "}\n"
    );

    Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);

    Pair<String, String> next = classRepositoryPair.iterator().next();

    assertEquals("Foo\\Apple", next.getFirst());
    assertEquals("Foobar", next.getSecond());
}
 
Example 3
Source File: TranslationPsiParser.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void parse(File file) {
    VirtualFile virtualFile = VfsUtil.findFileByIoFile(file, true);
    if(virtualFile == null) {
        Symfony2ProjectComponent.getLogger().info("VfsUtil missing translation: " + file.getPath());
        return;
    }

    PsiFile psiFile;
    try {
        psiFile = PhpPsiElementFactory.createPsiFileFromText(this.project, StreamUtil.readText(virtualFile.getInputStream(), "UTF-8"));
    } catch (IOException e) {
        return;
    }

    if(psiFile == null) {
        return;
    }

    Symfony2ProjectComponent.getLogger().info("update translations: " + file.getPath());

    Collection<NewExpression> messageCatalogues = PsiTreeUtil.collectElementsOfType(psiFile, NewExpression.class);
    for(NewExpression newExpression: messageCatalogues) {
        ClassReference classReference = newExpression.getClassReference();
        if(classReference != null) {
            PsiElement constructorMethod = classReference.resolve();
            if(constructorMethod instanceof Method) {
                PhpClass phpClass = ((Method) constructorMethod).getContainingClass();
                if(phpClass != null && PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Translation\\MessageCatalogueInterface")) {
                    this.getTranslationMessages(newExpression);
                }
            }
        }
    }
}
 
Example 4
Source File: DoctrinePhpMappingDriverTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see DoctrinePhpMappingDriver#getMetadata(fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.driver.DoctrineMappingDriverArguments)
 */
public void testPhpFlowAnnotationsMetadata() {
    PsiFile psiFile = PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php $foo = null;");

    DoctrineMetadataModel metadata = new DoctrinePhpMappingDriver().getMetadata(
        new DoctrineMappingDriverArguments(getProject(), psiFile, "\\Doctrine\\Flow\\Orm\\Annotation")
    );

    assertEquals("string", metadata.getField("email").getTypeName());

    assertEquals("ManyToMany", metadata.getField("car").getRelationType());
    assertEquals("\\DateTime", metadata.getField("car").getRelation());
}
 
Example 5
Source File: DoctrineUtilTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void testGetClassRepositoryPairForClassConstanta() {
    myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
        "namespace Bar;\n" +
        "" +
        "class Foobar {};\n"
    );

    PsiFile psiFileFromText = PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "\n" +
        "namespace Foo;\n" +
        "\n" +
        "use Doctrine\\ORM\\Mapping as ORM;\n" +
        "use Bar\\Foobar;\n" +
        "use Bar\\Foobar as Car;\n" +
        "use Bar as BarAlias;\n" +
        "" +
        "\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=Foobar::class)\n" +
        " */\n" +
        "class Apple {}\n" +
        "" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=Car::class)\n" +
        " */\n" +
        "class Banana {}\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=\\Bar\\Foobar::class)\n" +
        " */\n" +
        "class Yellow {}\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=BarAlias\\Foobar::class)\n" +
        " */\n" +
        "class Red {}\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=\"BarAlias\\Foobar\")\n" +
        " */\n" +
        "class Black {}\n" +
        "/**\n" +
        " * @ORM\\Entity(repositoryClass=\"Foobar\")\n" +
        " */\n" +
        "class White {}\n"
    );

    Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);

    Pair<String, String> apple = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\Apple".equals(stringStringPair.getFirst())).findFirst().get();
    assertEquals("Bar\\Foobar", apple.getSecond());

    Pair<String, String> banana = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\Banana".equals(stringStringPair.getFirst())).findFirst().get();
    assertEquals("Bar\\Foobar", banana.getSecond());

    Pair<String, String> yellow = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\Yellow".equals(stringStringPair.getFirst())).findFirst().get();
    assertEquals("Bar\\Foobar", yellow.getSecond());

    Pair<String, String> black = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\Black".equals(stringStringPair.getFirst())).findFirst().get();
    assertEquals("BarAlias\\Foobar", black.getSecond());

    Pair<String, String> white = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\White".equals(stringStringPair.getFirst())).findFirst().get();
    assertEquals("Foo\\Foobar", white.getSecond());
}