tethys/app/Library/Xml/XmlModel.php

181 lines
4.6 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
/**
* Footer
*
* Main footer file for the theme.
*
* @category Components
* @package ResearchRepository
* @subpackage Publish
* @author Your Name <yourname@example.com>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://gisgba.geologie.ac.at
* @since 1.0.0
*/
namespace App\Library\Xml;
2018-09-10 13:09:10 +00:00
use App\Models\XmlCache;
2018-08-06 12:30:51 +00:00
use Illuminate\Support\Facades\Log;
class XmlModel
{
/**
* Holds current configuration.
* @var Conf
*/
2018-09-10 13:09:10 +00:00
private $config = null;
2018-08-06 12:30:51 +00:00
/**
* Holds current xml strategy object.
* @var Strategy
*/
2018-09-10 13:09:10 +00:00
private $strategy = null;
2018-08-06 12:30:51 +00:00
/**
* TODO
* @var XmlCache
*/
2018-09-10 13:09:10 +00:00
private $cache = null;
2018-08-06 12:30:51 +00:00
/**
* Do some initial stuff like setting of a XML version and an empty
* configuration.
*/
public function __construct()
{
2018-09-10 13:09:10 +00:00
$this->strategy = new Strategy();// Opus_Model_Xml_Version1;
$this->config = new Conf();
$this->strategy->setup($this->config);
2018-08-06 12:30:51 +00:00
}
/**
* Set a new XML version with current configuration up.
*
* @param Strategy $strategy Version of Xml to process
*
* @return XmlModel fluent interface.
*/
public function setStrategy(Strategy $strategy)
{
2018-09-10 13:09:10 +00:00
$this->strategy = $strategy;
$this->strategy->setup($this->config);
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Set a new XML version with current configuration up.
*
* @param XmlCache $cache cach table
*
* @return XmlModel fluent interface.
*/
public function setXmlCache(XmlCache $cache)
{
2018-09-10 13:09:10 +00:00
$this->cache = $cache;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Return cache table.
*
* @return XmlCache
*/
public function getXmlCache()
{
2018-09-10 13:09:10 +00:00
return $this->cache;
2018-08-06 12:30:51 +00:00
}
/**
* Set the Model for XML generation.
*
2018-09-10 13:09:10 +00:00
* @param \App\Models\Dataset $model Model to serialize.
2018-08-06 12:30:51 +00:00
*
* @return XmlModel Fluent interface.
*/
public function setModel($model)
{
2018-09-10 13:09:10 +00:00
$this->config->model = $model;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Define that empty fields (value===null) shall be excluded.
*
* @return XmlModel Fluent interface
*/
public function excludeEmptyFields()
{
2018-09-10 13:09:10 +00:00
$this->config->excludeEmpty = true;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* If a model has been set this method generates and returnes
* DOM representation of it.
*
* @return \DOMDocument DOM representation of the current Model.
*/
public function getDomDocument()
{
2018-09-10 13:09:10 +00:00
$dataset = $this->config->model;
2018-08-06 12:30:51 +00:00
$domDocument = $this->getDomDocumentFromXmlCache();
if (!is_null($domDocument)) {
return $domDocument;
}
//create xml:
2018-09-10 13:09:10 +00:00
$domDocument = $this->strategy->getDomDocument();
2018-08-06 12:30:51 +00:00
//if caching is not desired, return domDocument
2018-09-10 13:09:10 +00:00
if (is_null($this->cache)) {
2018-08-06 12:30:51 +00:00
return $domDocument;
} else {
//create cache relation
2018-09-10 13:09:10 +00:00
$this->cache->fill(array(
2018-08-06 12:30:51 +00:00
'document_id' => $dataset->id,
2018-09-10 13:09:10 +00:00
'xml_version' => (int)$this->strategy->getVersion(),
2018-08-06 12:30:51 +00:00
'server_date_modified' => $dataset->server_date_modified,
'xml_data' => $domDocument->saveXML()
));
2018-09-10 13:09:10 +00:00
$this->cache->save();
2018-08-06 12:30:51 +00:00
Log::debug(__METHOD__ . ' cache refreshed for ' . get_class($dataset) . '#' . $dataset->id);
return $domDocument;
}
}
/**
* This method tries to load the current model from the xml cache. Returns
* null in case of an error/cache miss/cache disabled. Returns DOMDocument
* otherwise.
*
* @return \DOMDocument DOM representation of the current Model.
*/
private function getDomDocumentFromXmlCache()
{
2018-09-10 13:09:10 +00:00
$dataset = $this->config->model;
if (null === $this->cache) {
2018-08-06 12:30:51 +00:00
//$logger->debug(__METHOD__ . ' skipping cache for ' . get_class($model));
Log::debug(__METHOD__ . ' skipping cache for ' . get_class($dataset));
return null;
}
2018-09-10 13:09:10 +00:00
//$cached = $this->cache->hasValidEntry(
2018-08-06 12:30:51 +00:00
// $dataset->id,
2018-09-10 13:09:10 +00:00
// (int) $this->strategy->getVersion(),
2018-08-06 12:30:51 +00:00
// $dataset->server_date_modified
//);
//$cached = false;
$cache = XmlCache::where('document_id', $dataset->id)
->first();// model or null
if (!$cache) {
Log::debug(__METHOD__ . ' cache miss for ' . get_class($dataset) . '#' . $dataset->id);
return null;
} else {
return $cache->getDomDocument();
}
}
}