Chris@0: name = $name; Chris@0: $this->directories = $directories; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function findAll() { Chris@0: $all = []; Chris@0: Chris@0: $files = $this->findFiles(); Chris@0: $provider_by_files = array_flip($files); Chris@0: Chris@0: $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->name); Chris@0: Chris@0: // Try to load from the file cache first. Chris@0: foreach ($file_cache->getMultiple($files) as $file => $data) { Chris@0: $all[$provider_by_files[$file]] = $data; Chris@0: unset($provider_by_files[$file]); Chris@0: } Chris@0: Chris@0: // If there are files left that were not returned from the cache, load and Chris@0: // parse them now. This list was flipped above and is keyed by filename. Chris@0: if ($provider_by_files) { Chris@0: foreach ($provider_by_files as $file => $provider) { Chris@0: // If a file is empty or its contents are commented out, return an empty Chris@0: // array instead of NULL for type consistency. Chris@0: $all[$provider] = $this->decode($file); Chris@0: $file_cache->set($file, $all[$provider]); Chris@0: } Chris@0: } Chris@0: Chris@0: return $all; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Decode a YAML file. Chris@0: * Chris@0: * @param string $file Chris@0: * Yaml file path. Chris@0: * @return array Chris@0: */ Chris@0: protected function decode($file) { Chris@0: return Yaml::decode(file_get_contents($file)) ?: []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of file paths, keyed by provider. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function findFiles() { Chris@0: $files = []; Chris@0: foreach ($this->directories as $provider => $directory) { Chris@0: $file = $directory . '/' . $provider . '.' . $this->name . '.yml'; Chris@0: if (file_exists($file)) { Chris@0: $files[$provider] = $file; Chris@0: } Chris@0: } Chris@0: return $files; Chris@0: } Chris@0: Chris@0: }