class OutputManager {
public $output;
public $dom;
private $matches;
function __construct( &$output )
{
$this->output = $output;
$this->dom = new DOMDocument();
$this->dom->loadHTML( $this->output );
$this->dom->normalizeDocument();
$this->matches = array();
}
/**
* put your comment there...
*
* @param mixed $attr
* @param mixed $val
*/
public function elementExists( $tagName, $attr=NULL, $attr_val=NULL )
{
if (count( $this->matches[$tagName] ) == 0) {
$this->matches[$tagName] = $this->dom->getElementsByTagName( $tagName );
}
if ($attr == NULL && $attr_val == NULL) {
return $this->matches->length > 0;
}
else
if ($attr != NULL && $attr_val == NULL) {
foreach ($this->matches[$tagName] as $match) {
if ($this->match->hasAttribute($attr))
return true;
}
}
else
if ($attr != NULL && $attr_val != NULL) {
foreach ($this->matches[$tagName] as $match) {
if (trim( $match->getAttribute($attr), "/" ) == trim( $attr_val, "/" ))
return true;
}
}
return false;
}
public function addScript( $src, $at="head" )
{
if (!$this->elementExists( "script", "src", $src )) {
$result = $this->dom->getElementsByTagName($at);
$new_script = $this->dom->createElement( "script" );
$attribute_src = $this->dom->createAttribute( "src" );
$attribute_src->value = $src;
$new_script->appendChild( $attribute_src );
$attribute_type = $this->dom->createAttribute( "type" );
$attribute_type->value = "text/javascript";
$new_script->appendChild( $attribute_type );
$result->item(0)->appendChild( $new_script );
}
}
public function addStylesheet( $href )
{
if (!$this->elementExists( "link", "href", $href )) {
$result = $this->dom->getElementsByTagName($at);
$new_stylesheet = $this->dom->createElement( "link" );
$attribute_href = $this->dom->createAttribute( "href" );
$attribute_href->value = $href;
$new_stylesheet->appendChild( $attribute_href );
$attribute_type = $this->dom->createAttribute( "type" );
$attribute_type->value = "text/css";
$new_stylesheet->appendChild( $attribute_type );
$attribute_rel = $this->dom->createAttribute( "rel" );
$attribute_rel->value = "stylesheet";
$new_stylesheet->appendChild( $attribute_rel );
$result->item(0)->appendChild( $new_stylesheet );
}
}
/**
* Returns the final output.
*
*/
public function getOutput()
{
return $this->dom->saveHTML();
}
public function getStyles()
{
return $matches['link'];
}
}