phpstorm Dynamically creating an external library


How to include an external library in phpstorm? 

I tried this

Path path = getOrCreateVersionPath(version);
LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
Library library = table.getLibraryByName(LIB_NAME);
if (library == null) {
    library = LibraryUtil.createLibrary(table, LIB_NAME);
Library.ModifiableModel model = library.getModifiableModel();
model.addRoot(VfsUtil.getUrlForLibraryRoot(path), OrderRootType.CLASSES);

This works in IDEA, but doesn't work in phpstorm

For phpstorm, it works like this

<extensions defaultExtensionNs="com.jetbrains.php">
    <libraryRoot id="mylib"  runtime="false" path="/src" />

But I need to dynamically change source versions depending on user settings


Hi, Prihod! We have a set of API to allow to add libraries from plugins, please check out  `com.jetbrains.php.config.library.PhpLibraryRoot`, `com.jetbrains.php.config.library.PhpLibraryRoot#EP_NAME`, `com.jetbrains.php.config.library.PhpLibraryRootProvider`. This should help to achieve what you need!


If I use 


the files are not visible in “External Libraries”, although they are indexed.


I did it like this

Path path = getOrCreateVersionPath(version);
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path);
if (file != null) {
    Module[] modules = ModuleManager.getInstance(project).getModules();
    if (modules.length > 0) {
        ModifiableRootModel model = ModuleRootManager.getInstance(modules[0]).getModifiableModel();
        LibraryTable table = model.getModuleLibraryTable();
        Library library = table.getLibraryByName(LIB_NAME);
        if (library != null) {
            ModuleRootModificationUtil.removeDependency(modules[0], library);
        ModuleRootModificationUtil.addModuleLibrary(modules[0], LIB_NAME, Collections.singletonList(file.getUrl()), Collections.emptyList(), DependencyScope.COMPILE);