Changeset View
Changeset View
Standalone View
Standalone View
src/parser/PhutilEditorConfig.php
| Show First 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | final class PhutilEditorConfig extends Phobject { | ||||
| * | * | ||||
| * @param string | * @param string | ||||
| * @return map<string, wild> | * @return map<string, wild> | ||||
| */ | */ | ||||
| public function getProperties($path) { | public function getProperties($path) { | ||||
| $configs = $this->getEditorConfigs($path); | $configs = $this->getEditorConfigs($path); | ||||
| $matches = array(); | $matches = array(); | ||||
| // Normalize directory separators to "/". The ".editorconfig" standard | |||||
| // uses only "/" as a directory separator, not "\". | |||||
| $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); | |||||
| foreach ($configs as $config) { | foreach ($configs as $config) { | ||||
| list($path_prefix, $editorconfig) = $config; | list($path_prefix, $editorconfig) = $config; | ||||
| // Normalize path separators, as above. | |||||
| $path_prefix = str_replace(DIRECTORY_SEPARATOR, '/', $path_prefix); | |||||
| foreach ($editorconfig as $glob => $properties) { | foreach ($editorconfig as $glob => $properties) { | ||||
| if (!$glob) { | if (!$glob) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if (strpos($glob, '/') === false) { | if (strpos($glob, '/') === false) { | ||||
| $glob = '**/'.$glob; | $glob = '**/'.$glob; | ||||
| } else if (strncmp($glob, '/', 0)) { | } else if (strncmp($glob, '/', 0)) { | ||||
| Show All 37 Lines | final class PhutilEditorConfig extends Phobject { | ||||
| * | * | ||||
| * Find and parse all `.editorconfig` files between the specified path and | * Find and parse all `.editorconfig` files between the specified path and | ||||
| * the root directory. The results are returned in the same order that they | * the root directory. The results are returned in the same order that they | ||||
| * should be matched. | * should be matched. | ||||
| * | * | ||||
| * return list<pair<string, map>> | * return list<pair<string, map>> | ||||
| */ | */ | ||||
| private function getEditorConfigs($path) { | private function getEditorConfigs($path) { | ||||
| $configs = array(); | $configs = array(); | ||||
| $found_root = false; | |||||
| $root = $this->root; | |||||
| do { | $found_root = false; | ||||
| $path = dirname($path); | $paths = Filesystem::walkToRoot($path, $this->root); | ||||
| foreach ($paths as $path) { | |||||
| $file = $path.'/.editorconfig'; | $file = $path.'/.editorconfig'; | ||||
| if (!Filesystem::pathExists($file)) { | if (!Filesystem::pathExists($file)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $contents = Filesystem::readFile($file); | $contents = Filesystem::readFile($file); | ||||
| $config = phutil_ini_decode($contents); | $config = phutil_ini_decode($contents); | ||||
| if (idx($config, 'root') === true) { | if (idx($config, 'root') === true) { | ||||
| $found_root = true; | $found_root = true; | ||||
| } | } | ||||
| unset($config['root']); | unset($config['root']); | ||||
| array_unshift($configs, array($path, $config)); | array_unshift($configs, array($path, $config)); | ||||
| if ($found_root) { | if ($found_root) { | ||||
| break; | break; | ||||
| } | } | ||||
| } while ($path != $root && Filesystem::isDescendant($path, $root)); | } | ||||
| return $configs; | return $configs; | ||||
| } | } | ||||
| } | } | ||||