/** * @param EveApiReadWriteInterface $data * * @return self */ public function retrieveEveApi(EveApiReadWriteInterface $data) { $mess = sprintf('Started filesystem retrieve for %1$s/%2$s', $data->getEveApiSectionName(), $data->getEveApiName()); $this->getLogger()->debug($mess); try { $cachePath = $this->getSectionCachePath($data->getEveApiSectionName()); $this->checkUsableCachePath($cachePath); $cacheFile = $cachePath . $data->getEveApiName() . $data->getHash() . '.xml'; $this->checkUsableCacheFile($cacheFile); $this->prepareConnection($cacheFile); $result = $this->readXmlData($cacheFile); $this->__destruct(); if ($this->isExpired($result)) { $mess = sprintf('Deleting expired cache file %1$s', $cacheFile); $this->getLogger()->debug($mess); @unlink($cacheFile); return $this; } } catch (YapealRetrieverException $exc) { $mess = 'Could NOT get XML data'; $this->getLogger()->debug($mess, ['exception' => $exc]); return $this; } $data->setEveApiXml($result); return $this; }
/** * @param EveApiReadWriteInterface $data * * @return self * @throws \LogicException */ public function retrieveEveApi(EveApiReadWriteInterface $data) { $mess = sprintf('Started network retrieve for %1$s/%2$s', $data->getEveApiSectionName(), $data->getEveApiName()); $this->getLogger()->debug($mess); $result = $this->readXmlData($this->prepareConnection($data)); $data->setEveApiXml($this->addYapealProcessingInstructionToXml($result, $data->getEveApiArguments())); $this->__destruct(); return $this; }
/** * @param string $messagePrefix * @param EveApiReadWriteInterface $data * * @return string * @throws \LogicException */ protected function createEveApiMessage(string $messagePrefix, EveApiReadWriteInterface $data) : string { $mess = $messagePrefix . ' Eve API %1$s/%2$s'; $subs = [lcfirst($data->getEveApiSectionName()), $data->getEveApiName()]; if ($data->hasEveApiArgument('keyID')) { $mess .= ' for keyID = %3$s'; $subs[] = $data->getEveApiArgument('keyID'); if ($data->hasEveApiArgument('characterID')) { $mess .= ' and characterID = %4$s'; $subs[] = $data->getEveApiArgument('characterID'); } elseif ($data->hasEveApiArgument('corporationID')) { $mess .= ' and corporationID = %4$s'; $subs[] = $data->getEveApiArgument('corporationID'); } } return vsprintf($mess, $subs); }
/** * @param EveApiReadWriteInterface $data * @param string $eventSuffix * @param string $eventPrefix * * @return string[] * @throws \LogicException */ private function getEmitterEvents(EveApiReadWriteInterface $data, string $eventSuffix, string $eventPrefix) : array { // Prefix.Section.Api.Suffix, Prefix.Api.Suffix, // Prefix.Section.Suffix, Prefix.Suffix /** * @var string[] $eventNames */ $eventNames = explode(',', sprintf('%3$s.%1$s.%2$s.%4$s,%3$s.%2$s.%4$s,%3$s.%1$s.%4$s,%3$s.%4$s', ucfirst($data->getEveApiSectionName()), $data->getEveApiName(), $eventPrefix, $eventSuffix)); return $eventNames; }
/** * @param EveApiReadWriteInterface $data * * @return false|string * @throws \DomainException * @throws \InvalidArgumentException * @throws \LogicException * @throws \UnexpectedValueException */ private function getXsdFileContents(EveApiReadWriteInterface $data) { try { $xsdFile = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), $data->getEveApiName(), 'xsl'); } catch (YapealFileSystemException $exc) { $messagePrefix = 'Failed to find accessible XSD file during the validation of'; $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($messagePrefix, $data), ['exception' => $exc]); return false; } $contents = $this->safeFileRead($xsdFile); if (false === $contents) { $messagePrefix = sprintf('Failed to read XSD file %s during the validation of', $xsdFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($messagePrefix, $data)); return false; } if ('' === $contents) { $messagePrefix = sprintf('Received an empty XSD file %s during the validation of', $xsdFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($messagePrefix, $data)); return false; } $messagePrefix = sprintf('Using XSD file %s during the validation of', $xsdFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data)); return $contents; }
/** * @param string $eventName * @param \Yapeal\Xml\EveApiReadWriteInterface $data * @param array $context * * @return bool|string * @throws \DomainException * @throws \InvalidArgumentException * @throws \LogicException */ protected function getContentsFromTwig(string $eventName, EveApiReadWriteInterface $data, array $context) { $yem = $this->getYem(); try { $templateName = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), $data->getEveApiName(), $this->getTwigExtension()); } catch (YapealFileSystemException $exc) { $mess = 'Failed to find accessible twig template file during'; $yem->triggerLogEvent('Yapeal.Log.log', Logger::WARNING, $this->createEventMessage($mess, $data, $eventName), ['exception' => $exc]); return false; } $mess = sprintf('Using %1$s template file in twig to create file of', $templateName); $yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($mess, $data)); try { $contents = $this->getTwig()->render($templateName, $context); } catch (\Twig_Error $exc) { $mess = 'Creation of file failed because of twig exception during'; $yem->triggerLogEvent('Yapeal.Log.log', Logger::WARNING, $this->createEventMessage($mess, $data, $eventName), ['exception' => $exc]); return false; } return $contents; }
/** * @param EveApiReadWriteInterface $data * * @return string|false * @throws \DomainException * @throws \InvalidArgumentException * @throws \LogicException * @throws \UnexpectedValueException */ private function getXmlFileContents(EveApiReadWriteInterface $data) { // BaseSection/ApiHash.xml $cacheFile = sprintf('%1$s%2$s/%3$s%4$s.xml', $this->getCachePath(), ucfirst($data->getEveApiSectionName()), ucfirst($data->getEveApiName()), $data->getHash()); if (false === ($xml = $this->safeFileRead($cacheFile))) { $messagePrefix = sprintf('Failed to read XML file %s during the retrieval of', $cacheFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($messagePrefix, $data)); return false; } if ('' === $xml) { $messagePrefix = sprintf('Received an empty XML file %s during the retrieval of', $cacheFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($messagePrefix, $data)); return false; } $data->setEveApiXml($xml); if ($this->isExpired($data)) { $this->deleteWithRetry($cacheFile); return false; } $messagePrefix = sprintf('Using cached XML file %s during the retrieval of', $cacheFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data)); return $xml; }
/** * @param EveApiReadWriteInterface $data * * @throws LogicException * @return self */ protected function xsltTransform(EveApiReadWriteInterface $data) { $xslt = new XSLTProcessor(); $oldErrors = libxml_use_internal_errors(true); libxml_clear_errors(); $dom = new DOMDocument(); $dom->load($this->getXslName($data->getEveApiName(), $data->getEveApiSectionName())); $xslt->importStylesheet($dom); $xml = false; try { $xml = $xslt->transformToXml(new SimpleXMLElement($data->getEveApiXml())); } catch (\Exception $exc) { $mess = sprintf('XML cause SimpleXMLElement exception in Eve API %1$s/%2$s', lcfirst($data->getEveApiSectionName()), $data->getEveApiName()); $logger = $this->getLogger(); $logger->warning($mess, ['exception' => $exc]); } if (false === $xml) { $logger = $this->getLogger(); /** * @type array $errors */ $errors = libxml_get_errors(); if (0 !== count($errors)) { foreach ($errors as $error) { $logger->debug($error->message); } } libxml_clear_errors(); libxml_use_internal_errors($oldErrors); return $this; } libxml_clear_errors(); libxml_use_internal_errors($oldErrors); $config = ['indent' => true, 'indent-spaces' => 2, 'input-xml' => true, 'newline' => 'LF', 'output-xml' => true, 'wrap' => '1000']; // Tidy $tidy = new tidy(); $data->setEveApiXml($tidy->repairString($xml, $config, 'utf8')); return $this; }
/** * Handles loading, verifying, and return the correct XSL style sheet as a SimpleXMLElement instance. * * @param EveApiReadWriteInterface $data * * @return \SimpleXMLElement|false * @throws \DomainException * @throws \InvalidArgumentException * @throws \LogicException * @throws \UnexpectedValueException */ private function getStyleSheetInstance(EveApiReadWriteInterface $data) { try { $xslFile = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), $data->getEveApiName(), 'xsl'); } catch (YapealFileSystemException $exc) { $messagePrefix = 'Failed to find accessible XSL file during the transform of'; $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data), ['exception' => $exc]); return false; } $styleSheet = $this->safeFileRead($xslFile); if (false === $styleSheet) { $messagePrefix = sprintf('Failed to read XSL file %s during the transform of', $xslFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data)); return false; } if ('' === $styleSheet) { $messagePrefix = sprintf('Received an empty XSL file %s during the transform of', $xslFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data)); return false; } libxml_clear_errors(); libxml_use_internal_errors(true); $instance = false; try { $instance = new \SimpleXMLElement($styleSheet); } catch (\Exception $exc) { $messagePrefix = sprintf('SimpleXMLElement exception caused by XSL file %s during the transform of', $xslFile); $this->getYem()->triggerLogEvent('Yapeal.log.log', Logger::DEBUG, $this->createEveApiMessage($messagePrefix, $data), ['exception' => $exc]); $this->checkLibXmlErrors($data, $this->getYem()); } libxml_use_internal_errors(false); if (false !== $instance) { $messagePrefix = sprintf('Using XSL file %s during the transform of', $xslFile); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $this->createEveApiMessage($messagePrefix, $data)); } return $instance; }
/** * @param EveApiReadWriteInterface $data * * @throws LogicException * @return self */ protected function xsltTransform(EveApiReadWriteInterface &$data) { $xslt = new XSLTProcessor(); $oldErrors = libxml_use_internal_errors(true); libxml_clear_errors(); $dom = new DOMDocument(); $dom->load($this->getXslName($data->getEveApiName(), $data->getEveApiSectionName())); $xslt->importStylesheet($dom); $xml = $xslt->transformToXml(new SimpleXMLElement($data->getEveApiXml())); if (false === $xml) { $logger = $this->getLogger(); foreach (libxml_get_errors() as $error) { $logger->debug($error->message); } libxml_clear_errors(); libxml_use_internal_errors($oldErrors); return $this; } libxml_clear_errors(); libxml_use_internal_errors($oldErrors); $config = ['indent' => true, 'indent-spaces' => 2, 'output-xml' => true, 'input-xml' => true, 'wrap' => '1000']; // Tidy $tidy = new tidy(); $data->setEveApiXml($tidy->repairString($xml, $config, 'utf8')); return $this; }
/** * @param EveApiReadWriteInterface $data * * @return static Fluent interface. * @throws \DomainException * @throws \InvalidArgumentException * @throws \LogicException * @throws \UnexpectedValueException */ protected function updateCachedUntil(EveApiReadWriteInterface $data) { if ('' === $data->getEveApiXml()) { return $this; } $currentTime = (string) (new \SimpleXMLElement($data->getEveApiXml()))->currentTime[0]; if ('' === $currentTime) { return $this; } $dateTime = gmdate('Y-m-d H:i:s', strtotime($currentTime . '+00:00') + $data->getCacheInterval()); $row = ['accountKey' => $data->hasEveApiArgument('accountKey') ? $data->getEveApiArgument('accountKey') : '0', 'apiName' => $data->getEveApiName(), 'expires' => $dateTime, 'ownerID' => $this->extractOwnerID($data->getEveApiArguments()), 'sectionName' => $data->getEveApiSectionName()]; $sql = $this->getCsq()->getUpsert('utilCachedUntil', array_keys($row), 1); $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $sql); $pdo = $this->getPdo(); $pdo->beginTransaction(); $context = []; $success = false; try { $pdo->prepare($sql)->execute(array_values($row)); $pdo->commit(); $success = true; } catch (\PDOException $exc) { $pdo->rollBack(); $context = ['exception' => $exc]; } $mess = $success ? 'Updated cached until date/time of' : 'Could NOT update cached until date/time of'; $this->getYem()->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $this->createEveApiMessage($mess, $data), $context); return $this; }