Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#isClassServiceId()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#isClassServiceId() . 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: ServiceContainerUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static String getServiceClassFromServiceMapping(@NotNull YAMLMapping yamlMapping) {
    YAMLKeyValue classKeyValue = yamlMapping.getKeyValueByKey("class");

    // Symfony 3.3: "class" is optional; use service id for class
    // Foo\Bar:
    //   arguments: ~
    if(classKeyValue != null) {
        return classKeyValue.getValueText();
    }

    PsiElement parent = yamlMapping.getParent();
    if(parent instanceof YAMLKeyValue) {
        String keyText = ((YAMLKeyValue) parent).getKeyText();
        if(YamlHelper.isClassServiceId(keyText)) {
            return keyText;
        }
    }

    return null;
}
 
Example 2
Source File: XmlReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
    if(!Symfony2ProjectComponent.isEnabled(psiElement) || !(psiElement instanceof XmlAttributeValue)) {
        return new PsiReference[0];
    }

    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof XmlAttribute) && YamlHelper.isClassServiceId(parent.getText())) {
        return new PsiReference[0];
    }

    // invalidate on class attribute
    PsiElement xmlTag = parent.getParent();
    if(!(xmlTag instanceof XmlTag) || ((XmlTag) xmlTag).getAttribute("class") != null) {
        return new PsiReference[0];
    }

    return new PsiReference[] {
        new ServiceIdWithoutParameterReference((XmlAttributeValue) psiElement)
    };
}
 
Example 3
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PhpClass getPhpClassFromXmlTag(@NotNull XmlTag xmlTag, @NotNull ContainerCollectionResolver.LazyServiceCollector collector) {
    String className = xmlTag.getAttributeValue("class");
    if(className == null) {
        String id = xmlTag.getAttributeValue("id");
        if(id == null || !YamlHelper.isClassServiceId(id)) {
            return null;
        }

        className = id;
    }

    // @TODO: cache defs
    PhpClass resolvedClassDefinition = ServiceUtil.getResolvedClassDefinition(xmlTag.getProject(), className, collector);
    if(resolvedClassDefinition == null) {
        return null;
    }

    return resolvedClassDefinition;
}
 
Example 4
Source File: YamlClassInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement psiElement) {
            if ((YamlElementPatternHelper.getSingleLineScalarKey("class", "factory_class").accepts(psiElement) || YamlElementPatternHelper.getParameterClassPattern().accepts(psiElement)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(psiElement)) {
                // foobar.foo:
                //   class: Foobar\Foo
                invoke(psiElement, holder);
            } else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) {
                // Foobar\Foo: ~
                String text = PsiElementUtils.getText(psiElement);
                if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) {
                    PsiElement yamlKeyValue = psiElement.getParent();
                    if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) {
                        invoke(psiElement, holder);
                    }
                }
            }

            super.visitElement(psiElement);
        }
    };
}
 
Example 5
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * Extract service class for class or id attribute on shortcut
 *
 * <service id="foo" class="Foobar"/> => Foobar
 * <service class="Foobar"/> => Foobar
 */
@Nullable
public static String getClassFromServiceDefinition(@NotNull XmlTag xmlTag) {
    String classAttribute = xmlTag.getAttributeValue("class");
    if(StringUtils.isNotBlank(classAttribute)) {
        return classAttribute;
    }

    String id = xmlTag.getAttributeValue("id");
    if(id == null || StringUtils.isBlank(id) || !YamlHelper.isClassServiceId(id)) {
        return null;
    }

    return id;
}
 
Example 6
Source File: YamlServiceTagIntention.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private static Pair<PhpClass, Set<String>> invoke(@NotNull Project project, @NotNull YAMLKeyValue serviceKeyValue) {

    String aClass = YamlHelper.getYamlKeyValueAsString(serviceKeyValue, "class");
    if(aClass == null || StringUtils.isBlank(aClass)) {
        PsiElement key = serviceKeyValue.getKey();
        if (key != null) {
            String text = key.getText();
            if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text)) {
                aClass = text;
            }
        }
    }

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

    PhpClass resolvedClassDefinition = ServiceUtil.getResolvedClassDefinition(project, aClass, new ContainerCollectionResolver.LazyServiceCollector(project));
    if(resolvedClassDefinition == null) {
        return null;
    }

    Set<String> phpClassServiceTags = ServiceUtil.getPhpClassServiceTags(resolvedClassDefinition);

    Set<String> strings = YamlHelper.collectServiceTags(serviceKeyValue);
    if(strings.size() > 0) {
        for (String s : strings) {
            if(phpClassServiceTags.contains(s)) {
                phpClassServiceTags.remove(s);
            }
        }
    }

    return Pair.create(resolvedClassDefinition, phpClassServiceTags);
}
 
Example 7
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * fo<caret>o:
 *   class: foo
 *   arguments: []
 */
@Nullable
public static ServiceYamlContainer create(@NotNull YAMLKeyValue yamlServiceKeyValue) {
    YAMLMapping childOfType = PsiTreeUtil.getChildOfType(yamlServiceKeyValue, YAMLMapping.class);
    if(childOfType == null) {
        return null;
    }

    String serviceClass = null;

    YAMLKeyValue aClass = childOfType.getKeyValueByKey("class");
    if(aClass != null) {
        // service:
        //  class: Foobar
        YAMLValue value = aClass.getValue();
        if(value instanceof YAMLScalar) {
            serviceClass =  ((YAMLScalar) value).getTextValue();
        }
    } else {
        // Foobar:
        //  argument: ~
        String keyText = yamlServiceKeyValue.getKeyText();

        if(StringUtils.isNotBlank(keyText) && YamlHelper.isClassServiceId(keyText)) {
            serviceClass = keyText;
        }
    }

    if (StringUtils.isBlank(serviceClass)) {
        return null;
    }

    return new ServiceYamlContainer(yamlServiceKeyValue, childOfType.getKeyValueByKey("arguments"), serviceClass);
}