|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.action.generator.naming; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.intellij.openapi.project.Project; |
| 5 | +import com.intellij.openapi.vfs.VfsUtil; |
| 6 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 7 | +import fr.adrienbrault.idea.symfony2plugin.Settings; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 10 | +import org.apache.commons.lang.StringUtils; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | + |
| 14 | +import javax.script.ScriptEngine; |
| 15 | +import javax.script.ScriptEngineManager; |
| 16 | +import javax.script.ScriptException; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 20 | + */ |
| 21 | +public class JavascriptServiceNameStrategy implements ServiceNameStrategyInterface { |
| 22 | + |
| 23 | + @Nullable |
| 24 | + @Override |
| 25 | + public String getServiceName(@NotNull ServiceNameStrategyParameter parameter) { |
| 26 | + |
| 27 | + String serviceJsNameStrategy = Settings.getInstance(parameter.getProject()).serviceJsNameStrategy; |
| 28 | + if(serviceJsNameStrategy == null || StringUtils.isBlank(serviceJsNameStrategy)) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + Object eval = run(parameter.getProject(), parameter.getClassName(), serviceJsNameStrategy); |
| 34 | + if(!(eval instanceof String)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + return StringUtils.isNotBlank((String) eval) ? (String) eval : null; |
| 38 | + } catch (ScriptException e) { |
| 39 | + Symfony2ProjectComponent.getLogger().error(String.format("ScriptException: '%s' - Script: '%s'", e.getMessage(), serviceJsNameStrategy)); |
| 40 | + } |
| 41 | + |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + @Nullable |
| 46 | + public static Object run(@NotNull Project project, @NotNull String className, @NotNull String serviceJsNameStrategy) throws ScriptException { |
| 47 | + |
| 48 | + JsonObject jsonObject = new JsonObject(); |
| 49 | + |
| 50 | + jsonObject.addProperty("className", className); |
| 51 | + jsonObject.addProperty("projectName", project.getName()); |
| 52 | + jsonObject.addProperty("projectBasePath", project.getBasePath()); |
| 53 | + |
| 54 | + PhpClass aClass = PhpElementsUtil.getClass(project, className); |
| 55 | + if(aClass != null) { |
| 56 | + String relativePath = VfsUtil.getRelativePath(aClass.getContainingFile().getVirtualFile(), project.getBaseDir(), '/'); |
| 57 | + if(relativePath != null) { |
| 58 | + jsonObject.addProperty("relativePath", relativePath); |
| 59 | + } |
| 60 | + jsonObject.addProperty("absolutePath", aClass.getContainingFile().getVirtualFile().toString()); |
| 61 | + } |
| 62 | + |
| 63 | + ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); |
| 64 | + if(engine == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + return engine.eval("var __p = eval(" + jsonObject.toString() + "); result = function(args) { " + serviceJsNameStrategy + " }(__p)"); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments