Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decouple Twig namespace loading and provide more default namespace wh…
- Loading branch information
Showing
10 changed files
with
296 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/fr/adrienbrault/idea/symfony2plugin/templating/path/BundleTwigNamespaceExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.templating.path; | ||
|
||
import com.intellij.psi.PsiDirectory; | ||
import com.jetbrains.php.PhpIndex; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtension; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtensionParameter; | ||
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/** | ||
* FooBundle/Resources/views/foo.html.twig => FooBundle:foo.html.twig | ||
* FooBundle/Resources/views/foo.html.twig => @Foo/foo.html.twig | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class BundleTwigNamespaceExtension implements TwigNamespaceExtension { | ||
@NotNull | ||
@Override | ||
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) { | ||
Collection<TwigPath> twigPaths = new ArrayList<>(); | ||
|
||
Collection<SymfonyBundle> symfonyBundles = new SymfonyBundleUtil(PhpIndex.getInstance(parameter.getProject())).getBundles(); | ||
for (SymfonyBundle bundle : symfonyBundles) { | ||
PsiDirectory views = bundle.getSubDirectory("Resources", "views"); | ||
if(views == null) { | ||
continue; | ||
} | ||
|
||
// strip starting backslash to force relative path | ||
String path = StringUtils.stripStart(views.getVirtualFile().getPath(), "/"); | ||
|
||
String bundleName = bundle.getName(); | ||
|
||
twigPaths.add(new TwigPath(path, bundleName, TwigPathIndex.NamespaceType.BUNDLE)); | ||
if(bundleName.endsWith("Bundle")) { | ||
twigPaths.add(new TwigPath(path, bundleName.substring(0, bundleName.length() - 6), TwigPathIndex.NamespaceType.ADD_PATH)); | ||
} | ||
} | ||
|
||
return twigPaths; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/fr/adrienbrault/idea/symfony2plugin/templating/path/ContainerTwigNamespaceExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.templating.path; | ||
|
||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtension; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtensionParameter; | ||
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Collects path on compiled container: appDevDebugProjectContainer.xml | ||
* | ||
* <call method="addPath"> | ||
* <argument>... ymfony\Bundle\FrameworkBundle/Resources/views</argument> | ||
* <argument>Framework</argument> | ||
* </call> | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class ContainerTwigNamespaceExtension implements TwigNamespaceExtension { | ||
@NotNull | ||
@Override | ||
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) { | ||
TwigPathServiceParser twigPathServiceParser = ServiceXmlParserFactory.getInstance(parameter.getProject(), TwigPathServiceParser.class); | ||
|
||
return new ArrayList<>( | ||
twigPathServiceParser.getTwigPathIndex().getTwigPaths() | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/fr/adrienbrault/idea/symfony2plugin/templating/path/GlobalAppTwigNamespaceExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.templating.path; | ||
|
||
import com.intellij.openapi.vfs.VfsUtil; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import fr.adrienbrault.idea.symfony2plugin.Settings; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtension; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtensionParameter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* app/Resources/views/foo.html.twig => :foo.html.twig | ||
* app/Resources/views/foo.html.twig => foo.html.twig | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class GlobalAppTwigNamespaceExtension implements TwigNamespaceExtension { | ||
@NotNull | ||
@Override | ||
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) { | ||
String appDirectoryName = Settings.getInstance(parameter.getProject()).directoryToApp + "/Resources/views"; | ||
VirtualFile baseDir = parameter.getProject().getBaseDir(); | ||
|
||
VirtualFile globalDirectory = VfsUtil.findRelativeFile(baseDir, appDirectoryName.split("/")); | ||
if(globalDirectory == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
String path = globalDirectory.getPath(); | ||
|
||
return Arrays.asList( | ||
new TwigPath(path, TwigPathIndex.MAIN, TwigPathIndex.NamespaceType.BUNDLE), | ||
new TwigPath(path, TwigPathIndex.MAIN, TwigPathIndex.NamespaceType.ADD_PATH) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ienbrault/idea/symfony2plugin/tests/templating/path/BundleTwigNamespaceExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.tests.templating.path; | ||
|
||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.testFramework.VfsTestUtil; | ||
import com.intellij.util.containers.ContainerUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtensionParameter; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.path.BundleTwigNamespaceExtension; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPath; | ||
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase; | ||
|
||
import java.io.File; | ||
import java.util.Collection; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
* @see fr.adrienbrault.idea.symfony2plugin.templating.path.BundleTwigNamespaceExtension | ||
*/ | ||
public class BundleTwigNamespaceExtensionTest extends SymfonyLightCodeInsightFixtureTestCase { | ||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
VirtualFile virtualFile = myFixture.copyFileToProject("classes.php"); | ||
VfsTestUtil.createDir(virtualFile.getParent(), "Resources/views"); | ||
} | ||
|
||
public String getTestDataPath() { | ||
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath(); | ||
} | ||
|
||
public void testThatBundleNamespacesAreAdded() { | ||
Collection<TwigPath> namespaces = new BundleTwigNamespaceExtension() | ||
.getNamespaces(new TwigNamespaceExtensionParameter(getProject())); | ||
|
||
assertNotNull(ContainerUtil.find(namespaces, twigPath -> | ||
"FooBundle".equals(twigPath.getNamespace()) && "src/Resources/views".equals(twigPath.getPath())) | ||
); | ||
|
||
assertNotNull(ContainerUtil.find(namespaces, twigPath -> | ||
"Foo".equals(twigPath.getNamespace()) && "src/Resources/views".equals(twigPath.getPath())) | ||
); | ||
} | ||
} |
Oops, something went wrong.