phpDocumentor GloryLands
GLOO
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file gloobject.php

Documentation is available at gloobject.php

  1. <?php
  2. /**
  3.   * GloryLands OO System
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage GLOO
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2009, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * Basic DOM Element
  36.   *
  37.   * This class provides the basic class that implements
  38.   * the DOM element.
  39.   *
  40.   * @example sample_proxy.php An example of how to extend a GLOOObject
  41.   * @package GloryLands
  42.   * @subpackage GLOO
  43.   * @version 1.0
  44.   */
  45. class GLOOObject extends GLDOMElement {
  46.  
  47.     /**
  48.       * A reference to the root module object
  49.       * @var GLModule 
  50.       */
  51.     protected $module;
  52.     
  53.     /**
  54.       * The Link synchronization number between PHP and Javscript object
  55.       * @var int 
  56.       */
  57.     public $linkID;
  58.     
  59.     /**
  60.       * The Unit configuration, as obtained from the config.xml
  61.       * @var array 
  62.       */
  63.     protected $config;
  64.  
  65.     /**
  66.       * A flag that defines that objec variables are synced with the JS
  67.       * @var bool 
  68.       */
  69.     public $synced;
  70.     
  71.     public function __sleep({
  72.         // Remove the reference to the $module. We don't need it and
  73.         // it also causes trouble on serialization
  74.         $vars get_object_vars($this);
  75.         unset($vars['module']);
  76.         $vars array_keys($vars);
  77.         return $vars;        
  78.     }
  79.     
  80.     public function __wakeup({
  81.         // Re-load the $module reference
  82.         $this->module = GLModules::get_module_for($this);
  83.     }
  84.         
  85.     public function __construct($id=NULL{
  86.     
  87.         // Initialize my variables
  88.         $this->vars = array();
  89.         $this->linkID = false;
  90.         $this->synced = false;
  91.     
  92.         // Detect my GLModule 
  93.         // (Instead of using $v = GLMOdule::fetch(Class) we can directly instance this class, using $v = new Class()
  94.         //  and let the GLModule::mine() function to do the detection of the appropriate module.
  95.         $this->module = GLModules::get_module_for($this);
  96.         $this->config = $this->module->get_config_for($this);                
  97.                 
  98.         // Initialize the GLDOMElement, using this class name for the DOM class name.
  99.         // You can also provide an optional ID by the constructor
  100.         parent::__construct(get_class($this)$id);
  101.         
  102.         // If we have client script, make the PHP-JS binding by allocating
  103.         // a link ID and registering this class to the GLOOLink structure.
  104.         if (isset($this->config['client'])) {
  105.             $client_class $this->config['client'][0]['class'];
  106.             $this->linkID = GLOOLink::register($this$client_class);            
  107.         else {
  108.             $this->linkID = false;
  109.         }
  110.         
  111.     }
  112.  
  113.     private function encode_structure($struct{
  114.         if (is_array($struct)) {
  115.             foreach ($struct as $k => $v{
  116.                 $struct[$k$this->flatten_structure($v);
  117.             }
  118.             return $struct;
  119.         elseif (is_object($struct)) {
  120.             if ($struct instanceof GLOOObject{
  121.                 return GLOOLink::get_object_reference($struct);
  122.             else {
  123.                 return encode_structure(get_object_vars($struct));
  124.             }
  125.         else {
  126.             return $struct;
  127.         }
  128.     }
  129.  
  130.     /** 
  131.       * An inheritable function to notify a variable update
  132.       * [INHERITED FROM GLDataset]
  133.       *
  134.       * @param    string    $name    The variable name that was updated
  135.       * @param    mixed    $value    The variable value
  136.       */
  137.     protected function var_changed($name&$value{
  138.         if ($this->synced{
  139.             $this->_call('__set'$name$this->encode_structure($value));
  140.         }
  141.     }
  142.  
  143.     /** 
  144.       * An inheritable function to notify a variable removal
  145.       * [INHERITED FROM GLDataset]
  146.       *
  147.       * @param    string    $name    The variable name that was removed
  148.       */
  149.     protected function var_removed($name{
  150.         if ($this->synced{
  151.             $this->_call('__unset'$name);
  152.         }
  153.     }
  154.  
  155.     /** 
  156.       * An inheritable function to notify an object attachment
  157.       * [INHERITED FROM GLDataset]
  158.       *
  159.       * @param    string    $name    The variable name that was updated
  160.       * @param    object    $object    The object that was added
  161.       */
  162.     protected function object_added($name&$object{
  163.         if ($object instanceof GLDOMElement{
  164.             $this->add_child($object);
  165.         }
  166.     }
  167.  
  168.     /** 
  169.       * An inheritable function to notify an object detachment
  170.       * [INHERITED FROM GLDataset]
  171.       *
  172.       * @param    string    $name    The variable name that was updated
  173.       * @param    object    $object    The object that was removed
  174.       */
  175.     protected function object_removed($name&$object{
  176.         if ($object instanceof GLDOMElement{
  177.             $this->remove_child($object);
  178.         }
  179.     }
  180.     
  181.     /**
  182.       * Prepare the variables that will be sent to the JS interface
  183.       */
  184.     public function _vars({
  185.         return $this->vars;
  186.     }
  187.  
  188.     /**
  189.       * Bind the object into the rendering template
  190.       */
  191.     public function _bind($template{
  192.         
  193.     }
  194.  
  195.     /**
  196.       * Perform a call to the JS interface
  197.       */
  198.     public final function _call($call{
  199.         if ($this->linkID === falsereturn;
  200.         $vars func_get_args();
  201.         array_shift($vars);
  202.         GLOOLink::store_message($this->linkID$call$vars);
  203.     }
  204. }
  205.  
  206. ?>

Documentation generated on Tue, 13 Oct 2009 23:49:06 +0300 by phpDocumentor 1.4.1