Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getClass()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getClass() . 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: DoctrineYamlAnnotationLookupBuilder.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static Set<String> getAnnotations(Project project, String className) {

        HashSet<String> map = new HashSet<>();

        PhpClass phpClass = PhpElementsUtil.getClass(project, className);
        if(phpClass == null) {
            return map;
        }

        for(Field field: phpClass.getFields()) {
            if(!field.isConstant()) {
                map.add(field.getName());
            }
        }

        return map;
    }
 
Example 2
Source File: FormUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public PhpClass getFormTypeToClass(@Nullable String formType) {

    if(formType == null) {
        return null;
    }

    // formtype can also be a direct class name
    if(formType.contains("\\")) {
        PhpClass phpClass = PhpElementsUtil.getClass(PhpIndex.getInstance(project), formType);
        if(phpClass != null) {
            return phpClass;
        }
    }

    return this.getFormTypeClass(formType);

}
 
Example 3
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Get class factory method attribute
 *
 * <factory class="FooBar" method="cre<caret>ate"/>
 */
@Nullable
public static PhpClass getPhpClassForClassFactory(@NotNull XmlAttributeValue xmlAttributeValue) {
    String method = xmlAttributeValue.getValue();
    if(StringUtils.isBlank(method)) {
        return null;
    }

    XmlTag parentOfType = PsiTreeUtil.getParentOfType(xmlAttributeValue, XmlTag.class);
    if(parentOfType == null) {
        return null;
    }

    String aClass = parentOfType.getAttributeValue("class");
    if(aClass == null || StringUtils.isBlank(aClass)) {
        return null;
    }

    return PhpElementsUtil.getClass(xmlAttributeValue.getProject(), aClass);
}
 
Example 4
Source File: ServiceUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static Collection<PhpClass> getTaggedClasses(@NotNull Project project, @NotNull String tagName) {

        List<PhpClass> phpClasses = new ArrayList<>();

        Set<String> taggedServices = getTaggedServices(project, tagName);
        if(taggedServices.size() == 0) {
            return phpClasses;
        }

        ContainerCollectionResolver.ServiceCollector collector = ContainerCollectionResolver.ServiceCollector.create(project);
        for(String serviceName: taggedServices) {
            String resolvedService = collector.resolve(serviceName);
            if(resolvedService != null) {
                PhpClass phpClass = PhpElementsUtil.getClass(project, resolvedService);
                if(phpClass != null) {
                    phpClasses.add(phpClass);
                }
            }
        }

        return phpClasses;
    }
 
Example 5
Source File: JavascriptServiceNameStrategy.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static Object run(@NotNull Project project, @NotNull String className, @NotNull String serviceJsNameStrategy) throws ScriptException {

    JsonObject jsonObject = new JsonObject();

    jsonObject.addProperty("className", className);
    jsonObject.addProperty("projectName", project.getName());
    jsonObject.addProperty("projectBasePath", project.getBasePath());
    jsonObject.addProperty("defaultNaming", new DefaultServiceNameStrategy().getServiceName(new ServiceNameStrategyParameter(project, className)));

    PhpClass aClass = PhpElementsUtil.getClass(project, className);
    if(aClass != null) {
        String relativePath = VfsUtil.getRelativePath(aClass.getContainingFile().getVirtualFile(), ProjectUtil.getProjectDir(aClass), '/');
        if(relativePath != null) {
            jsonObject.addProperty("relativePath", relativePath);
        }
        jsonObject.addProperty("absolutePath", aClass.getContainingFile().getVirtualFile().toString());
    }

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    if(engine == null) {
        return null;
    }

    return engine.eval("var __p = eval(" + jsonObject.toString() + "); result = function(args) { " + serviceJsNameStrategy + " }(__p)");
}
 
Example 6
Source File: ServiceBuilder.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void serviceTagCallback(String className, TagCallbackInterface callback) {

        PhpClass phpClass = PhpElementsUtil.getClass(project, className);
        if(phpClass == null) {
            return;
        }

        List<ServiceTag> serviceTags = new ArrayList<>();
        for (String tag : ServiceUtil.getPhpClassServiceTags(phpClass)) {
            ServiceTag serviceTag = new ServiceTag(phpClass, tag);
            ServiceUtil.decorateServiceTag(serviceTag);
            serviceTags.add(serviceTag);
        }

        if(serviceTags.size() == 0) {
            return;
        }

        callback.onTags(serviceTags);
    }
 
Example 7
Source File: ShopwareUtil.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public static void collectControllerAction(Project project, String controllerName, ControllerActionVisitor visitor, String... modules) {

        for(String moduleName: modules) {
            PhpClass phpClass = PhpElementsUtil.getClass(project, String.format("Shopware_Controllers_%s_%s", moduleName, controllerName));
            if(phpClass != null) {
                for(Method method: phpClass.getMethods()) {
                    if(method.getAccess().isPublic() && method.getName().endsWith("Action")) {
                        visitor.visitMethod(method, method.getName().substring(0, method.getName().length() - 6), moduleName, controllerName);
                    }
                }
            }
        }

    }
 
Example 8
Source File: ShopwareUtil.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
/**
 * "Enlight_Controller_Action_PostDispatchSecure_Frontend_Payment" _> Shopware_Controllers_Frontend_Payment
 */
@Nullable
public static PhpClass getControllerOnActionSubscriberName(@NotNull Project project, @NotNull String subscriberName) {
    Pattern pattern = Pattern.compile("Enlight_Controller_Action_\\w+_(Frontend|Backend|Core|Widgets)_(\\w+)");
    Matcher matcher = pattern.matcher(subscriberName);

    if(matcher.find()) {
        PhpClass phpClass = PhpElementsUtil.getClass(project, String.format("Shopware_Controllers_%s_%s", matcher.group(1), matcher.group(2)));
        if(phpClass != null) {
            return phpClass;
        }
    }

    return null;
}
 
Example 9
Source File: FormTypeClass.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public PhpClass getPhpClass(Project project) {

    if(phpClass != null) {
        return phpClass;
    }

    if(this.phpClassName == null) {
        return null;
    }

    return PhpElementsUtil.getClass(project, this.phpClassName);
}
 
Example 10
Source File: XmlServiceSuggestIntentionAction.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @Nullable Editor editor, @NotNull PsiElement psiElement, @NotNull PsiElement psiElement1) {
    if(editor == null) {
        return;
    }

    PhpClass phpClass = PhpElementsUtil.getClass(project, expectedClass);
    if(phpClass == null) {
        return;
    }

    Collection<ContainerService> suggestions = ServiceUtil.getServiceSuggestionForPhpClass(phpClass, ContainerCollectionResolver.getServices(project));
    if(suggestions.size() == 0) {
        HintManager.getInstance().showErrorHint(editor, "No suggestion found");
        return;
    }

    XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
    if(xmlTag == null) {
        return;
    }

    ServiceSuggestDialog.create(
        editor,
        ContainerUtil.map(suggestions, ContainerService::getName),
        new XmlServiceSuggestIntention.MyInsertCallback(xmlTag)
    );
}
 
Example 11
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {

    PsiElement position = completionParameters.getPosition();
    if(!Symfony2ProjectComponent.isEnabled(position)) {
        return;
    }

    YAMLCompoundValue yamlCompoundValue = PsiTreeUtil.getParentOfType(position, YAMLCompoundValue.class);
    if(yamlCompoundValue == null) {
        return;
    }

    String className = YamlHelper.getYamlKeyValueAsString(yamlCompoundValue, "targetEntity", false);
    if(className == null) {
        return;
    }

    PhpClass phpClass = PhpElementsUtil.getClass(position.getProject(), className);
    if(phpClass == null) {
        return;
    }

    for(DoctrineModelField field: EntityHelper.getModelFields(phpClass)) {
        if(field.getRelation() != null) {
            completionResultSet.addElement(new DoctrineModelFieldLookupElement(field));
        }
    }

}