public function dump(Definition $definition, string $filepath) { $file = new SplFileInfo($filepath); if ($file->isDir() || !$file->isWritable()) { throw new RuntimeException("Given file {$filepath} is directory or is unwritable"); } $definitionHydrator = new DefinitionHydrator(); return yaml_emit_file($file->getPathname(), $definitionHydrator->extract($definition)); }
/** * @param string $filepath * @return Definition */ public function parse(string $filepath) : Definition { $file = new SplFileInfo($filepath); $rootPath = $file->getPath(); if (!$file->isFile() || !$file->isReadable()) { throw new RuntimeException("Given file {$filepath} isn't file or is unreadable"); } $ndocs = 0; $data = yaml_parse_file($file->getPathname(), 0, $ndocs, ['!include' => function (string $filepath) use($rootPath) { $file = new SplFileInfo($rootPath . DIRECTORY_SEPARATOR . $filepath); if (!$file->isFile() || !$file->isReadable()) { throw new RuntimeException("Given file {$filepath} isn't file or is unreadable"); } return file_get_contents($file->getPathname()); }]); $definitionHydrator = new DefinitionHydrator(); $definition = new Definition(); $definitionHydrator->hydrate($data, $definition); return $definition; }