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

Source for file gloolink.php

Documentation is available at gloolink.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.   * GLOO Link Manager
  36.   *
  37.   * This class provides the basic functions that implements
  38.   * the PHP-Javascript linking interface.
  39.   *
  40.   * It also provides the management system of the
  41.   * permanent structure storage inside the section.
  42.   *
  43.   * @subpackage GLOO
  44.   * @version 1.0
  45.   */
  46. class GLOOLink {
  47.     
  48.     static private $ObjectStack;    
  49.     
  50.     /**
  51.       * Save the Object stack
  52.       *
  53.       * @todo The session management should be performed
  54.       */
  55.     static public function save({    
  56.         // #@# This should be implemented using GLCache::Set()
  57.         $_SESSION['DOM'self::$ObjectStack;
  58.     }
  59.  
  60.     static public function initialize($clean_start=false{    
  61.         // #@# This should be implemented using GLCache::Get()        
  62.         if ($clean_start{
  63.             self::$ObjectStack array();
  64.             $_SESSION['DOM'array();
  65.         else {
  66.             if (isset($_SESSION['DOM'])) {
  67.                 self::$ObjectStack $_SESSION['DOM'];
  68.             else {
  69.                 self::$ObjectStack array();
  70.             }        
  71.         }
  72.     }
  73.  
  74.     // =======[ PHP - JS Interaction ]================================================
  75.     
  76.     static public function get_object($linkid{
  77.         foreach (self::$ObjectStack as $info{
  78.             if ($info['id'== $linkidreturn $info['object'];
  79.         }
  80.         return false;
  81.     }
  82.  
  83.     static public function get_objects_by_name($class{
  84.         $stack array();
  85.         foreach (self::$ObjectStack as $info{
  86.             if ($info['object']->classname == $class$stack[$info['object'];
  87.         }
  88.         return $stack;
  89.     }
  90.  
  91.     static public function get_object_by_id($object_id{
  92.         foreach (self::$ObjectStack as $info{
  93.             if ($info['object']->id == $object_idreturn $info['object'];
  94.         }
  95.         return false;
  96.     }
  97.     
  98.     static public function register($class$js_class{            
  99.     
  100.         // Make sure $ObjectStack is defined (since we are static)
  101.         if (!self::$ObjectStackself::$ObjectStack=array();
  102.         
  103.         // Detect name and ID
  104.         $name get_class($class);
  105.         $id sizeof(self::$ObjectStack);
  106.         
  107.         // Register the object on stack
  108.         self::$ObjectStack[array(
  109.             'object' => &$class,
  110.             'class' => $js_class,
  111.             'id' => $id
  112.         );
  113.         
  114.         // Return the ID
  115.         // (Note that the class that called this function ($class) 
  116.         //  MUST assign this ID on the linkID property)
  117.         return $id;
  118.     }
  119.  
  120.     static public function get_object_reference($object{
  121.         return '#GL.DOM.get('.$object->linkID.')#';
  122.     }
  123.  
  124.     static public function compile_structure($structure&$delay_ref$path{        
  125.     
  126.         if (is_array($structure)) {
  127.         
  128.             foreach ($structure as $var => $value{
  129.                 
  130.                 // Calculate the variable name to use for path nesting
  131.                 $subvar '.'.$var;
  132.                 if (is_numeric($var)) $subvar='['.$var.']';
  133.                 
  134.                 // If this element is an object, do some additional processing
  135.                 if (is_object($value)) {
  136.                 
  137.                     // The object is a GLOOObject reference, return null
  138.                     // for now, but register the variable for a delay-reference
  139.                     if ($value instanceof GLOOObject{
  140.                         $delay_ref[array(
  141.                             'path' => $path.$subvar,
  142.                             'id' => $value->linkID
  143.                         );
  144.                         unset($structure[$var]);
  145.                     
  146.                     // If the object is not a GLOOObject instance, extract it's
  147.                     // variables, and act like an array
  148.                     else {
  149.                         $structure[$varself::compile_structure(get_object_vars($value)$delay_ref$path.$subvar);
  150.                     }
  151.                 
  152.                 // If this element is an array, perform a subl-evel structure analysis 
  153.                 elseif (is_array($value)) {
  154.                     $structure[$varself::compile_structure($value$delay_ref$path.'.'.$var);
  155.                 }
  156.             
  157.             }
  158.             
  159.             // Now that the array is properly formatted, return the string representation
  160.             return str_replace('"[]"''[]'json_encode($structure));
  161.         
  162.         else {
  163.             
  164.             // Elseways, use json_encode to convert the structure into 
  165.             // something that Javascript can handle
  166.             return str_replace('"[]"''[]'json_encode($structure));
  167.             
  168.         }
  169.  
  170.     }
  171.         
  172.     static public function compile_js({
  173.     
  174.         // Make sure $ObjectStack is defined (since we are static)
  175.         if (!self::$ObjectStackself::$ObjectStack=array();
  176.         
  177.         // Initialize script    
  178.         $script "/* GloryLands Web-Based MMORPG v0.6        */\r\n";
  179.         $script.= "/* Object-Oriented API v0.1 Alpha          */\r\n";
  180.         $script.= "/* (C) Copyright 2009 - John Haralampidis  */\r\n";
  181.         $script.= "// Initialize Object Structure:\r\n";        
  182.         
  183.         // Start creating the object instances, recording in the same
  184.         // time the delay-mapping that should be done in order to properly set-up
  185.         // the object references
  186.         $delay_ref array();
  187.         foreach (self::$ObjectStack as $i => $object{
  188.             
  189.             // Prepare the variables that should be used for the initialization of the JS unit
  190.             $init_vars array_merge($object['object']->_vars()array(
  191.                 'root' => $object['object']->root,
  192.                 'parent' => $object['object']->parent,
  193.                 'previous' => $object['object']->previous,
  194.                 'next' => $object['object']->next,
  195.                 'children' => $object['object']->children
  196.             ));
  197.             
  198.             // Write the code that should initialize the JS interface, and in the same
  199.             // time extract the delay references
  200.             $script.='GL.DOM.register(new '.$object['class'].'('.self::compile_structure($init_vars$delay_ref$object['id'].'#').','.$object['id'].'));'."\r\n";
  201.             
  202.             // Merk the object as synced
  203.             self::$ObjectStack[$i]['object']->synced true;
  204.         }
  205.         
  206.         // Compile the delay-mapping in order to implement
  207.         // the object references.
  208.         if (sizeof($delay_ref0{
  209.             $script.= "// Perform delay-mapping of references:\r\n";
  210.             $groups array();
  211.             foreach ($delay_ref as $ref{
  212.                 $part explode('#'$ref['path']);
  213.                 $ref['path'substr($part[1],1);
  214.                 $group = (int)$part[0];
  215.                 
  216.                 if (!isset($groups[$group])) $groups[$group]='with(GL.DOM.get('.$group.')){';
  217.                 $groups[$group].=$ref['path']."=GL.DOM.get(".$ref['id'].");";
  218.             }
  219.             $script.=implode("};\r\n"$groups)."};\r\n";
  220.         }
  221.  
  222.         // Finalize the script
  223.         $script.= "$(window).addEvent('domready',function(){GL.Core.run();});\r\n";
  224.         
  225.         // Return the compiled script
  226.         return $script;
  227.     }
  228.     
  229.     // =======[ Async I/O ]===========================================================
  230.     
  231.     static public function peek_messages({
  232.         // #@# This should be implemented using GLCache::Get()
  233.         if (!isset($_SESSION['messages'])) return array();
  234.         $msg $_SESSION['messages'];
  235.         unset($_SESSION['messages']);
  236.         return $msg;
  237.     }
  238.     
  239.     static public function store_message($linkid$call$parm{
  240.         // #@# This should be implemented using GLCache::Set()
  241.         if (!isset($_SESSION['messages'])) $_SESSION['messages'array();
  242.         $_SESSION['messages'][array(
  243.             'id' => $linkid'c' => $call'p' => $parm
  244.         );
  245.     }
  246.     
  247. }
  248.  
  249. ?>

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