/**
  * Loads a Ini, JSON,PHP, XML or YAML file using a FileReader. Throws exception if file cannot be loaded.
  *
  * @param  string $filePath
  * @return array
  * @throws \Sonrisa\Component\FileReader\Exceptions\FileReaderException
  */
 public static function parse($filePath)
 {
     if (File::exists($filePath)) {
         //get file extension
         $ext = File::getExtension($filePath);
         switch ($ext) {
             case 'ini':
                 $data = IniFileReader::parse($filePath);
                 break;
             case 'json':
                 $data = JsonFileReader::parse($filePath);
                 break;
             case 'php':
                 $data = PhpFileReader::parse($filePath);
                 break;
             case 'xml':
                 $data = XmlFileReader::parse($filePath);
                 break;
             case 'yaml':
             case 'yml':
                 $data = YmlFileReader::parse($filePath);
                 break;
             default:
                 throw new FileReaderException("Could not read {$filePath} using a FileReader.");
                 break;
         }
         if (empty($data)) {
             return array();
         } else {
             return $data;
         }
     } else {
         throw new FileReaderException("Could not load {$filePath}. File does not exist.");
     }
 }
示例#2
0
 public function testGetExtensionNonExistentFile()
 {
     $file = '/THIS/DIRECTORY/DOES/NOT/EXIST/' . $this->filename;
     $result = File::getExtension($file);
     $this->assertFalse($result);
 }