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

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#getYamlKeyValue() . 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: EntityHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void attachYamlFieldTypeName(String keyName, DoctrineModelField doctrineModelField, YAMLKeyValue yamlKeyValue) {

        if("fields".equals(keyName) || "id".equals(keyName)) {

            YAMLKeyValue yamlType = YamlHelper.getYamlKeyValue(yamlKeyValue, "type");
            if(yamlType != null) {
                doctrineModelField.setTypeName(yamlType.getValueText());
            }

            YAMLKeyValue yamlColumn = YamlHelper.getYamlKeyValue(yamlKeyValue, "column");
            if(yamlColumn != null) {
                doctrineModelField.setColumn(yamlColumn.getValueText());
            }

            return;
        }

        if(RELATIONS.contains(keyName.toLowerCase())) {
            YAMLKeyValue targetEntity = YamlHelper.getYamlKeyValue(yamlKeyValue, "targetEntity");
            if(targetEntity != null) {
                doctrineModelField.setRelationType(keyName);
                doctrineModelField.setRelation(getOrmClass(yamlKeyValue.getContainingFile(), targetEntity.getValueText()));
            }
        }

    }
 
Example 2
Source File: RouteHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Find controller definition in yaml structure
 *
 * foo:
 *   defaults: { _controller: "Bundle:Foo:Bar" }
 *   defaults:
 *      _controller: "Bundle:Foo:Bar"
 *   controller: "Bundle:Foo:Bar"
 */
@Nullable
public static String getYamlController(@NotNull YAMLKeyValue psiElement) {
    YAMLKeyValue yamlKeyValue = YamlHelper.getYamlKeyValue(psiElement, "defaults");
    if(yamlKeyValue != null) {
        final YAMLValue container = yamlKeyValue.getValue();
        if(container instanceof YAMLMapping) {
            YAMLKeyValue yamlKeyValueController = YamlHelper.getYamlKeyValue(container, "_controller", true);
            if(yamlKeyValueController != null) {
                String valueText = yamlKeyValueController.getValueText();
                if(StringUtils.isNotBlank(valueText)) {
                    return valueText;
                }
            }
        }
    }

    String controller = YamlHelper.getYamlKeyValueAsString(psiElement, "controller");
    if(controller != null && StringUtils.isNotBlank(controller)) {
        return controller;
    }

    return null;
}
 
Example 3
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement position = parameters.getPosition();

    if(!Symfony2ProjectComponent.isEnabled(position)) {
        return;
    }

    PsiElement serviceDefinition = position.getParent();
    if(serviceDefinition instanceof YAMLKeyValue) {
        YAMLKeyValue aClass = YamlHelper.getYamlKeyValue((YAMLKeyValue) serviceDefinition, "class");
        if(aClass == null) {
            PhpClassCompletionProvider.addClassCompletion(parameters, completionResultSet, position, false);
        }
    }
}
 
Example 4
Source File: FileResourceVisitorUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * foo:
 *   resources: 'FOO'
 */
private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<FileResourceConsumer> consumer) {
    for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues(yamlFile)) {
        YAMLKeyValue resourceKey = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource", true);
        if(resourceKey == null) {
            continue;
        }

        String resource = PsiElementUtils.trimQuote(resourceKey.getValueText());
        if(StringUtils.isBlank(resource)) {
            continue;
        }

        consumer.consume(new FileResourceConsumer(resourceKey, yamlKeyValue, normalize(resource)));
    }
}
 
Example 5
Source File: ConfigSchemaIndex.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {
    return inputData -> {
        Map<String, Set<String>> map = new THashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if(!Symfony2ProjectComponent.isEnabledForIndex(psiFile.getProject())) {
            return map;
        }

        if(!(psiFile instanceof YAMLFile) || !isValidForIndex(psiFile)) {
            return map;
        }

        for(YAMLKeyValue yamlKeyValue: YamlHelper.getTopLevelKeyValues((YAMLFile) psiFile)) {
            String key = PsiElementUtils.trimQuote(yamlKeyValue.getKeyText());

            if(StringUtils.isBlank(key) || key.contains("*")) {
                continue;
            }

            Set<String> mappings = new HashSet<>();
            YAMLKeyValue mapping = YamlHelper.getYamlKeyValue(yamlKeyValue, "mapping");
            if(mapping == null) {
                continue;
            }

            Set<String> keySet = YamlHelper.getKeySet(mapping);
            if(keySet != null) {
                mappings.addAll(keySet);
            }

            map.put(key, mappings);
        }

        return map;
    };

}
 
Example 6
Source File: EntityHelper.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static Map<String, YAMLKeyValue> getYamlModelFieldKeyValues(YAMLKeyValue yamlKeyValue) {
    Map<String, YAMLKeyValue> keyValueCollection = new HashMap<>();

    for(String fieldMap: new String[] { "id", "fields", "manyToOne", "oneToOne", "manyToMany", "oneToMany"}) {
        YAMLKeyValue targetYamlKeyValue = YamlHelper.getYamlKeyValue(yamlKeyValue, fieldMap, true);
        if(targetYamlKeyValue != null) {
            keyValueCollection.put(fieldMap, targetYamlKeyValue);
        }
    }

    return keyValueCollection;
}
 
Example 7
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 8
Source File: DotEnvUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * environment:
 *   - FOOBAR=0
 *
 * environment:
 *   FOOBAR: 0
 */
private static void visitEnvironmentSquenceItems(@NotNull Consumer<Pair<String, PsiElement>> consumer, @NotNull YAMLKeyValue yamlKeyValue) {
    YAMLKeyValue environment = YamlHelper.getYamlKeyValue(yamlKeyValue, "environment");
    if (environment != null) {
        // FOOBAR=0
        for (YAMLSequenceItem yamlSequenceItem : YamlHelper.getSequenceItems(environment)) {
            YAMLValue value = yamlSequenceItem.getValue();
            if (value instanceof YAMLScalar) {
                String textValue = ((YAMLScalar) value).getTextValue();
                if (StringUtils.isNotBlank(textValue)) {
                    String[] split = textValue.split("=");
                    if (split.length > 1) {
                        consumer.accept(Pair.create(split[0], value));
                    }
                }
            }
        }

        // FOOBAR: 0
        YAMLMapping childOfType = PsiTreeUtil.getChildOfType(environment, YAMLMapping.class);
        if (childOfType != null) {
            for (Map.Entry<String, YAMLValue> entry : YamlHelper.getYamlArrayKeyMap(childOfType).entrySet()) {
                consumer.accept(Pair.create(entry.getKey(), entry.getValue()));
            }
        }
    }
}
 
Example 9
Source File: YamlServiceArgumentInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private boolean isValidService(ServiceActionUtil.ServiceYamlContainer serviceYamlContainer) {
    YAMLKeyValue serviceKey = serviceYamlContainer.getServiceKey();

    Set<String> keySet = YamlHelper.getKeySet(serviceKey);
    if(keySet == null) {
        return true;
    }

    for(String s: INVALID_KEYS) {
        if(keySet.contains(s)) {
            return false;
        }
    }

    // check autowire scope
    Boolean serviceAutowire = YamlHelper.getYamlKeyValueAsBoolean(serviceKey, "autowire");
    if(serviceAutowire != null) {
        // use service scope for autowire
        if(serviceAutowire) {
            return false;
        }
    } else {
        // find file scope defaults
        // defaults: [autowire: true]
        YAMLMapping key = serviceKey.getParentMapping();
        if(key != null) {
            YAMLKeyValue defaults = YamlHelper.getYamlKeyValue(key, "_defaults");
            if(defaults != null) {
                Boolean autowire = YamlHelper.getYamlKeyValueAsBoolean(defaults, "autowire");
                if(autowire != null && autowire) {
                    return false;
                }
            }
        }
    }

    return true;
}
 
Example 10
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public static void fixServiceArgument(@NotNull YAMLKeyValue yamlKeyValue) {
    YAMLKeyValue argumentsKeyValue = YamlHelper.getYamlKeyValue(yamlKeyValue, "arguments");

    List<String> yamlMissingArgumentTypes = ServiceActionUtil.getYamlMissingArgumentTypes(
        yamlKeyValue.getProject(),
        ServiceActionUtil.ServiceYamlContainer.create(yamlKeyValue),
        false,
        new ContainerCollectionResolver.LazyServiceCollector(yamlKeyValue.getProject())
    );

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

    InsertServicesCallback insertServicesCallback;

    if(argumentsKeyValue == null) {
        // there is no "arguments" key so provide one
        insertServicesCallback = new YamlCreateServiceArgumentsCallback(yamlKeyValue);
    } else {
        insertServicesCallback = new YamlUpdateArgumentServicesCallback(
            yamlKeyValue.getProject(),
            argumentsKeyValue,
            yamlKeyValue
        );
    }

    ServiceActionUtil.fixServiceArgument(yamlKeyValue.getProject(), yamlMissingArgumentTypes, insertServicesCallback);
}