public function save(array $tree, $fileName) { $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION); if (!TreeSerializerFactory::isRegisteredByFileExtension($fileExtension)) { throw new NotRegisteredException(sprintf('Unable to find serializer for extension "%s".', $fileExtension)); } $serializer = TreeSerializerFactory::getByFileExtension($fileExtension); $serializedTree = $serializer->serialize($tree); file_put_contents($fileName, $serializedTree); }
/** * Fetch the data from the specified location of the Xref. * * @param Xref $xref * @param boolean $force If true and Xref already fetched, force the resolver to fetch the data again. * @throws XrefResolverFetchException */ public static function resolve(Xref $xref, $force = false) { if ($xref->isResolved() && !$force) { return; } static::matchType($xref); if (!$xref->hasLocation()) { throw new XrefResolverFetchException($xref); } $xrefLocation = $xref->getLocation(); if (!file_exists($xrefLocation)) { throw new XrefResolverFetchException($xref, 'File does not exist.'); } try { $data = file_get_contents($xrefLocation); } catch (\Exception $e) { throw new XrefResolverFetchException($xref, $e->getMessage()); } $xref->setResolved(false); if ($xref->hasContentType()) { $serializer = TreeSerializerFactory::getByContentType($xref->getContentType()); } else { $fileExtension = pathinfo($xrefLocation, PATHINFO_EXTENSION); $serializer = TreeSerializerFactory::getByFileExtension($fileExtension); $xref->setContentType($serializer::getContentType()); } try { $data = $serializer::deserialize($data); } catch (TreeSerializerSyntaxException $e) { throw new XrefResolverFetchException($xref, $e->getMessage()); } $xref->setData($data); $xref->setResolved(true); }
/** * Fetch the data from the specified location of the Xref. * * @param Xref $xref * @param boolean $force If true and Xref already fetched, force the resolver to fetch the data again. * @throws UnknownXrefTypeException * @throws XrefResolverFetchException * @throws \ConfigToken\TreeCompiler\XrefResolver\Exception\InvalidXrefTypeException * @throws \ConfigToken\TreeSerializer\Exception\UnknownContentTypeException * @throws \ConfigToken\TreeSerializer\Exception\UnknownFileExtensionException */ public static function resolve(Xref $xref, $force = false) { if ($xref->isResolved() && !$force) { return; } static::matchType($xref); if (!$xref->hasLocation()) { throw new XrefResolverFetchException($xref); } $data = null; $httpCode = null; $contentType = null; $eventManager = EventManager::getInstance(); if ($eventManager->hasListeners()) { $event = new Event(self::EVENT_ID_RESOLVE_URL); $event->data[self::EVENT_URL] = $xref->getLocation(); $event->data[Event::RESULT] = false; $eventManager->dispatch($event); if (isset($event->data[Event::RESULT])) { if ($event->data[Event::RESULT] === true) { $data = $event->data[self::EVENT_DATA]; $contentType = $event->data[self::EVENT_CONTENT_TYPE]; $httpCode = 200; } else { if ($event->data[Event::RESULT] !== false) { throw new XrefResolverFetchException($xref, sprintf('Got error message from listener: %s', $event->data[Event::RESULT])); } } } } if (!isset($data)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $xref->getLocation()); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, false); $data = curl_exec($ch); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } if ($httpCode != 200) { throw new XrefResolverFetchException($xref, sprintf('Got response code %d', $httpCode)); } if (TreeSerializerFactory::isRegisteredByContentType($contentType)) { $serializer = TreeSerializerFactory::getByContentType($contentType); $xref->setContentType($contentType); } else { $path = parse_url($xref->getLocation(), PHP_URL_PATH); $fileExtension = pathinfo($path, PATHINFO_EXTENSION); if (!TreeSerializerFactory::isRegisteredByFileExtension($fileExtension)) { throw new UnknownXrefTypeException(sprintf('Unable to find resolver for Xref content type "%s" or file extension "%s" for location "%s".', $contentType, $fileExtension, $xref->getLocation())); } $serializer = TreeSerializerFactory::getByFileExtension($fileExtension); $xref->setContentType($serializer::getContentType()); } // TODO: catch exception, show xref location in error message $data = $serializer::deserialize($data); $xref->setData($data); $xref->setResolved(true); }
/** * @expectedException \ConfigToken\TreeSerializer\Exception\UnknownFileExtensionException */ public function testUnknownFileExtension() { TreeSerializerFactory::getByFileExtension('rubbish'); }