|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * phpstorm follows symlinks when indexing, which creates an infinite loop of indexing. |
| 4 | + * The inclusion of the vendor folders is mostly the problem |
| 5 | + */ |
| 6 | + |
| 7 | +class phpstorm_exclude_recursive_folders { |
| 8 | + |
| 9 | + public static function init() { |
| 10 | + self::has_dot_idea_folder(); |
| 11 | + self::has_modules_xml(); |
| 12 | + self::has_project_iml(); |
| 13 | + self::set_exclude_in_project_iml(); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Check if .idea folder exists, if not create it. |
| 18 | + */ |
| 19 | + protected static function has_dot_idea_folder() { |
| 20 | + // Has .idea folder? |
| 21 | + if ( ! is_dir( '.idea' ) ) { |
| 22 | + mkdir( '.idea' ); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Check if .idea/modules.xml exists, if not create it. |
| 28 | + */ |
| 29 | + protected static function has_modules_xml() { |
| 30 | + // Does the modules.xml file exists? |
| 31 | + if ( is_file( '.idea/modules.xml' ) ) { |
| 32 | + return; // already exists |
| 33 | + } |
| 34 | + $project_name = self::get_project_name(); |
| 35 | + |
| 36 | + $modules_xml_content = <<<XML |
| 37 | +<?xml version="1.0" encoding="UTF-8"?> |
| 38 | +<project version="4"> |
| 39 | + <component name="ProjectModuleManager"> |
| 40 | + <modules> |
| 41 | + <module fileurl="file://\$PROJECT_DIR\$/.idea/{$project_name}.iml" filepath="\$PROJECT_DIR\$/.idea/{$project_name}.iml" /> |
| 42 | + </modules> |
| 43 | + </component> |
| 44 | +</project> |
| 45 | +XML; |
| 46 | + file_put_contents( '.idea/modules.xml', $modules_xml_content ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Check if .idea/PROJECT.iml exists, if not create it. |
| 51 | + */ |
| 52 | + protected static function has_project_iml() { |
| 53 | + // does the .iml file exists? |
| 54 | + if ( is_file( self::get_project_iml_path() ) ) { |
| 55 | + return; // already exists. |
| 56 | + } |
| 57 | + $modules_xml_content = <<<XML |
| 58 | +<?xml version="1.0" encoding="UTF-8"?> |
| 59 | +<module type="WEB_MODULE" version="4"> |
| 60 | + <component name="NewModuleRootManager"> |
| 61 | + <content url="file://\$MODULE_DIR\$"/> |
| 62 | + <orderEntry type="inheritedJdk" /> |
| 63 | + <orderEntry type="sourceFolder" forTests="false" /> |
| 64 | + </component> |
| 65 | +</module> |
| 66 | +XML; |
| 67 | + file_put_contents( self::get_project_iml_path(), $modules_xml_content ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get the name of the project, by default it's the same as the folder name. |
| 72 | + * |
| 73 | + * @return false|string |
| 74 | + */ |
| 75 | + protected static function get_project_name() { |
| 76 | + $pwd = getcwd(); |
| 77 | + $project_name = substr( $pwd, strrpos( $pwd, "/" ) + 1 ); |
| 78 | + |
| 79 | + return $project_name; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the project.iml file path. |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + protected static function get_project_iml_path() { |
| 87 | + $modules_xml = new DOMDocument(); |
| 88 | + $modules_xml->load( '.idea/modules.xml' ); |
| 89 | + $iml_file_path = $modules_xml->getElementsByTagName( 'component' )->item( 0 ) |
| 90 | + ->getElementsByTagName( 'modules' )->item( 0 ) |
| 91 | + ->getElementsByTagName( 'module' )->item( 0 )->getAttribute( 'filepath' ); |
| 92 | + $iml_file_path = str_replace( '$PROJECT_DIR$/', '', $iml_file_path ); |
| 93 | + |
| 94 | + return $iml_file_path; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Generate the list of the folders to exclude. |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + protected static function get_exclude_dir_list() { |
| 103 | + $command_folders = glob( '*', GLOB_ONLYDIR ); |
| 104 | + $exclude_folders = array(); |
| 105 | + foreach ( $command_folders as $command_folder ) { |
| 106 | + if ( ! is_dir( "{$command_folder}/vendor" ) ) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + $exclude_folders[] = "{$command_folder}/vendor"; |
| 110 | + } |
| 111 | + |
| 112 | + // hard code, always exclude the vendor/wp-cli, it's only symlinks. |
| 113 | + $exclude_folders[] = 'vendor/wp-cli'; |
| 114 | + $exclude_folders[] = 'builds/phar'; |
| 115 | + |
| 116 | + return $exclude_folders; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Add the folders to exclude in the iml file. |
| 121 | + */ |
| 122 | + protected static function set_exclude_in_project_iml() { |
| 123 | + $folders_to_exclude = self::get_exclude_dir_list(); |
| 124 | + $iml_xml = new DOMDocument(); |
| 125 | + $iml_xml->preserveWhiteSpace = false; |
| 126 | + $iml_xml->formatOutput = true; |
| 127 | + $iml_xml->load( self::get_project_iml_path() ); |
| 128 | + $iml_xml_content_node = $iml_xml->getElementsByTagName( 'component' )->item( 0 ) |
| 129 | + ->getElementsByTagName( 'content' )->item( 0 ); |
| 130 | + $xpath = new DomXpath( $iml_xml ); |
| 131 | + |
| 132 | + foreach ( $folders_to_exclude as $folder ) { |
| 133 | + $attributevalue = "file://\$MODULE_DIR\$/{$folder}"; |
| 134 | + |
| 135 | + // Check for duplicates. |
| 136 | + $duplicates = $xpath->query( '//excludeFolder[@url="' . $attributevalue . '"]' ); |
| 137 | + if ( 0 !== $duplicates->length ) { |
| 138 | + continue; // Don't add duplicates. |
| 139 | + } |
| 140 | + |
| 141 | + // Add child element. |
| 142 | + $exclude_node = $iml_xml->createElement( 'excludeFolder' ); |
| 143 | + $exclude_node->setAttribute( 'url', $attributevalue ); |
| 144 | + $iml_xml_content_node->appendChild( $exclude_node ); |
| 145 | + } |
| 146 | + |
| 147 | + $iml_xml->preserveWhiteSpace = false; |
| 148 | + $iml_xml->formatOutput = true; |
| 149 | + $iml_xml->save( self::get_project_iml_path() ); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * GO! |
| 155 | + */ |
| 156 | +phpstorm_exclude_recursive_folders::init(); |
0 commit comments