Skip to content

Instantly share code, notes, and snippets.

@FSX
Created April 24, 2010 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FSX/377548 to your computer and use it in GitHub Desktop.
Save FSX/377548 to your computer and use it in GitHub Desktop.
<?php
/* ---
A really simple Atom/XML parser with caching.
Usage:
try
{
$p = new AtomParser('fluxbb.atom');
print_r($p->parse());
}
catch (AtomParserException $e)
{
echo $e->getMessage(), "\n\n";
print_r($e->get_libxml_errors());
}
Changelog:
- 15 May, 2010: Replaced filectime() with filemtime() (Fixed by Franz/lie2815).
--- */
class AtomParser
{
const DS = DIRECTORY_SEPARATOR;
private $parser, $xml_file, $simplexml_obj, $cache, $cache_file,
$cache_file_exists, $cache_duraction;
public function __construct($xml_file, $cache=false, $cache_path='/tmp',
$cache_duraction=3600)
{
$this->xml_file = $xml_file;
if ($this->cache = $cache)
{
$this->cache_file = $cache_path.self::DS.'atom-parser-'.sha1($this->xml_file).'.php';
$this->cache_duraction = $cache_duraction;
$this->cache_file_exists = file_exists($this->cache_file);
}
}
public function parse()
{
// Read cache if cache file exists and not expired
if ($this->cache && $this->cache_file_exists &&
(time() - filemtime($this->cache_file)) < $this->cache_duraction)
{
return require $this->cache_file;
}
// Enable this so libxml_get_errors() can be used to get all the errors
libxml_use_internal_errors(true);
// LIBXML_NOCDATA: Merge CDATA as text nodes
// http://www.php.net/manual/en/libxml.constants.php
$this->simplexml_obj = @simplexml_load_file($this->xml_file, null, LIBXML_NOCDATA);
if (!$this->simplexml_obj)
{
$libxml_errors = libxml_get_errors();
libxml_clear_errors();
throw new AtomParserException('Failed to load XML file: '.$this->xml_file,
$libxml_errors);
}
$array = $this->object2array($this->simplexml_obj);
// Write array to the cache
if ($this->cache)
{
if (!@file_put_contents($this->cache_file, '<?php'."\n\n".'return '.var_export($array, true).';'))
throw new AtomParserException('Failed to write cache file: '.$this->cache_file);
}
return $array;
}
// A modified version of:
// http://nl.php.net/manual/en/function.simplexml-load-file.php#56691
private function object2array($object)
{
$return = null;
if (is_array($object))
foreach ($object as $key => $value)
$return[$key] = $this->object2array($value);
else
{
if (is_object($object))
{
$var = get_object_vars($object);
foreach ($var as $key => $value)
$return[$key] = ($key && !$value) ? null : $this->object2array($value);
}
else
return $object;
}
return $return;
}
}
class AtomParserException extends Exception
{
private $libxml_errors;
public function __construct($message, $libxml_errors, $code = 0,
Exception $previous = null)
{
$this->libxml_errors = $libxml_errors;
if (version_compare(PHP_VERSION, '5.3.0', '>='))
parent::__construct($message, $code, $previous);
else
parent::__construct($message, $code);
}
public function get_libxml_errors()
{
return $this->libxml_errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment