¤  tinycrud project site > /lib/classes/XMLBoundObject.php
<?
/*
Copyright (c) 2004-2007, Dave Gullo <dave@gullo.tv>
{{{
All rights reserved.
  
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  
Neither the name of the Dave Gullo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}}}
*/
require_once $_SERVER['DOCUMENT_ROOT'] ."/lib/classes/Object.php";
require_once 
$_SERVER['DOCUMENT_ROOT'] ."/lib/classes/XMLPersistable.php";

if (!
defined("ASDOM")) {
  
define("ASDOM"1); //-= Optional param to toXML which returns ref to DOM document instead.
}
if (!
defined("ASXML")) {
  
define("ASXML"4); //-= Optional param to toXML which returns ref to DOM document instead.
}
if (!
defined("ASOBJ")) {
  
define("ASOBJ"5); //-= Optional param to toXML which returns ref to DOM document instead.
}

abstract class 
XMLBoundObject extends Object implements XMLPersistable {
  private 
$schema;
                                                                                                                                                               
  function 
setSchema($uri) {
    
//-= for use in debug mode for validating XML during toXML calls...
    
if (!isset($uri)) {
      
$this->schema null;
      return;
    }
    if (!
eregi("^http(s)?"$uri)) {
      throw new 
Exception("$this->classname::setSchema expects a URL in the form http(s)?");
      return;
    }
    
$this->schema $uri;
  }
                                                                                                                                                               
  function 
getSchema() {
    return 
$this->schema;
  }
                                                                                                                                                               
  function 
toXML($mode null) {
    
$doc = new DomDocument("1.0");
    
$rootname $this->classname;
    
$root $doc->createElement($rootname);
    
$root $doc->appendChild($root);   
    if (isset(
$this->id)) {
      
$attr $doc->createAttribute("id");
      
$value $doc->createTextNode($this->id);
      
$attr->appendChild($value);
      
$root->appendChild($attr);
    }
    if (
$this instanceof Cachable) {
      
$attr $doc->createAttribute("lastmodified");
      
$value $doc->createTextNode($this->lastmodified);
      
$attr->appendChild($value);
      
$root->appendChild($attr);
    }
    foreach (
$this as $key => $val) {
      if (!(
eregi("^(id|schema)$"$key))) {
        
//-= Honor the visibility of each variable by skipping protected and private vars...
        
$prop= new ReflectionProperty($this->classname$key);
        if (
$prop->isPublic()) {
          
//-= TODO: access content type and add <!<CDATA tags when required..
          
if ($val instanceof DBXMLObject) {
            
$tdoc $val->toXML(ASDOM);
            
$userroot $doc->importNode($tdoc->documentElementtrue);
            
$root->appendChild($userroot);
          } elseif (!
is_array($val)) {
            
$node $doc->createElement($key);
            if (
$key == 'html') {
              
$value $doc->createCDATASection($val);
            } else {
              
$value $doc->createTextNode($val);
            }
            
$node->appendChild($value);
            
$root->appendChild($node);
          }
        } elseif (
$prop->isProtected() && $val instanceof DBXMLObject) {
          
$tdoc $val->toXML(ASDOM);
          
$userroot $doc->importNode($tdoc->documentElementtrue);
          
$root->appendChild($userroot);
        } else {
          
#-= hide protected and private vars...
        
}
      }
    }
    if (isset(
$mode) && $mode == ASDOM) {
      return 
$doc;  //-= For adding more nodes to the root when overriding...
    
} else {
      return 
$doc->saveXML($root);
    }
  }

  
//-=  Static XML Cachable functions for memcached
  
static function set($obj$as ASXML) {
    if (
true || !extension_loaded('memcache')) {
      return;
    }
    if (!
is_object($obj) && !($obj instanceof XMLBoundObject)) {
      throw new 
Exception("Attempted to set non-object to memcache"get_class($obj));
    }
    if (
$obj->id 1) {
      throw new 
Exception("Attempted to set object with no primary key memcache"get_class($obj));
    }
    if (
$obj->lastmodified 1) {
      throw new 
Exception("Attempted to set object without lastmodified timestamp to memcache"get_class($obj));
    }
    
$cache = new Memcache();
    
$cache->connect('127.0.0.1'11211);
    if (
$as == ASXML) {
      
$key md5(get_class($obj) . $obj->id);
      
#echo "$classname, $id, $as  [$key]";
      
return $cache->set($key$obj->toXML(), false);
    }
    if (
$as == ASOBJ) {
      
$key get_class($obj) ."_obj_"$obj->id;
      
#echo "$classname, $id, $as  [$key]";
      
return $cache->set($key$objfalse);
    }
  }

  static function 
get($classname$id$as ASXML) {
    
//-= Given a class and a primary key, returns an Object or DOM serilization from the memcache
    
if (true || !extension_loaded('memcache')) {
      eval(
"$"."o = new $classname('$id');");
      if (
$o instanceof XMLBoundObject) {
        
#$obj->activate(); //-= wakie wakie
      
}
      if (
$as == ASXML) {
        return 
$o->toXML(ASDOM);
      } elseif (
$as == ASOBJ) {
        return 
$o;
      } 
    }
    
$cache = new Memcache();
    
$cache->connect('127.0.0.1'11211);
    if (
$as == ASXML) {
      
$key md5($classname $id);
      
#echo "$classname, $id, $as  <$key>";
      
if ($xml $cache->get($key)) {
        
$dom = new DomDocument('1.0''utf-8');
        
$dom->loadXML($xml);
        return 
$dom;
      } else {
        eval(
"$"."o = new $classname('$id');");
        if (
$o instanceof XMLBoundObject) {
          
XMLBoundObject::set($o$as);
          return 
$o->toXML(ASDOM);
        }
      }
    }
    if (
$as == ASOBJ) {
      
$key "$classname"."_obj_"$id;
      
#echo "$classname, $id, $as  [$key]";
      
if ($obj $cache->get($key)) {
        if (
$o instanceof XMLBoundObject) {
          
$obj->activate(); //-= wakie wakie
        
}
        return 
$obj;
      } else {
        eval(
"$"."o = new $classname('$id');");
        if (
is_object($o) && $o instanceof XMLBoundObject) {
          
$o->activate(); //-= wakie wakie
        
}
        
XMLBoundObject::set($o$as);
        return 
$o;
      }   
    }
  }
}
?>