/src/HessianFactory.php

http://hessianphp.googlecode.com/ · PHP · 219 lines · 155 code · 20 blank · 44 comment · 28 complexity · 0d998ceed7ca6b67fc5d0ff42fec3dc7 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the HessianPHP package.
  4. * (c) 2004-2011 Manuel Gómez
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. include_once 'HessianInterfaces.php';
  10. include_once 'HessianExceptions.php';
  11. include_once 'HessianParsing.php';
  12. include_once 'HessianOptions.php';
  13. include_once 'HessianUtils.php';
  14. include_once 'HessianCallbackHandler.php';
  15. include_once 'HessianReferenceMap.php';
  16. include_once 'HessianTypeMap.php';
  17. include_once 'HessianStream.php';
  18. define('HESSIAN_PHP_VERSION', '2.0.4');
  19. /**
  20. * Default implementation of an object factory
  21. */
  22. class HessianObjectFactory implements IHessianObjectFactory{
  23. var $options;
  24. public function setOptions(HessianOptions $options){
  25. $this->options = $options;
  26. }
  27. public function getObject($type){
  28. if(!class_exists($type)) {
  29. if(isset($this->options->strictType) && $this->options->strictType)
  30. throw new Exception("Type $type cannot be found for object instantiation, check your type mappings");
  31. $obj = new stdClass();
  32. $obj->__type = $type;
  33. return $obj;
  34. }
  35. return new $type();
  36. }
  37. }
  38. /**
  39. * Default Datetime adapter that works with the built-in Datetime class of PHP5
  40. * @author vsayajin
  41. */
  42. class HessianDatetimeAdapter{
  43. public static function toObject($ts, $parser){
  44. $date = date('c', $ts);
  45. //$date = gmdate('c', $ts);
  46. return new Datetime($date);
  47. }
  48. public static function writeTime($date, $writer){
  49. $ts = $date->format('U');
  50. $stream = $writer->writeDate($ts);
  51. return new HessianStreamResult($stream);
  52. }
  53. }
  54. /**
  55. * Handles que creation of components for assembling Hessian clients and servers
  56. * It contains the basic assembly configuration for these components.
  57. * @author vsayajin
  58. *
  59. */
  60. class HessianFactory{
  61. var $protocols = array();
  62. var $transports = array();
  63. static $cacheRules = array();
  64. function __construct(){
  65. $this->protocols = array(
  66. '2'=>array($this,'loadVersion2'), '1'=>array($this,'loadVersion1')
  67. );
  68. $this->transports = array(
  69. 'CURL' => 'HessianCURLTransport',
  70. 'curl' => 'HessianCURLTransport',
  71. 'http' => 'HessianHttpStreamTransport'
  72. );
  73. }
  74. /**
  75. * Returns a specialized HessianParser object based on the options object
  76. * @param HessianStream $stream input stream
  77. * @param HessianOptions $options configuration options
  78. */
  79. function getParser($stream, $options){
  80. $version = $options->version;
  81. if($options->detectVersion && $stream)
  82. $version = $this->detectVersion($stream, $options);
  83. $callback = $this->getConfig($version);
  84. $parser = call_user_func_array($callback, array('parser', $stream, $options));
  85. if($options->objectFactory instanceof IHessianObjectFactory){
  86. $parser->objectFactory = $options->objectFactory;
  87. } else {
  88. $parser->objectFactory = new HessianObjectFactory();
  89. }
  90. return $parser;
  91. }
  92. /**
  93. * Returns a specialized HessianWriter object based on the options object
  94. * @param HessianStream $stream output stream
  95. * @param HessianOptions $options configuration options
  96. */
  97. function getWriter($stream, $options){
  98. $version = $options->version;
  99. if($options->detectVersion && $stream)
  100. $version = $this->detectVersion($stream, $options);
  101. $callback = $this->getConfig($version);
  102. $writer = call_user_func_array($callback, array('writer', $stream, $options));
  103. return $writer;
  104. }
  105. /**
  106. * Creates a parsing helper object (rules resolver) that uses a protocol
  107. * rule file to parse the incomin stream. It caches the rules for further
  108. * use.
  109. * @param Integer $version Protocol version
  110. * @param array $config local component configuration
  111. */
  112. public function getRulesResolver($version, $rulesPath=''){
  113. if(isset(self::$cacheRules[$version]))
  114. return self::$cacheRules[$version];
  115. $resolver = new HessianRuleResolver($rulesPath);
  116. self::$cacheRules[$version] = $resolver;
  117. return $resolver;
  118. }
  119. /**
  120. * Receives a stream and iterates over que registered protocol handlers
  121. * in order to detect which version of Hessian is it
  122. * @param HessianStream $stream
  123. * @return integer Protocol version detected
  124. */
  125. function detectVersion($stream, $options){
  126. foreach($this->protocols as $version => $callback){
  127. $res = call_user_func_array($callback, array('detect', $stream, $options));
  128. if($res)
  129. return $version;
  130. }
  131. throw new Exception("Cannot detect protocol version on stream");
  132. }
  133. function getConfig($version){
  134. if(!isset($this->protocols[$version]))
  135. throw new Exception("No configuration for version $version protocol");
  136. return $this->protocols[$version];
  137. }
  138. function getTransport(HessianOptions $options){
  139. $type = $options->transport;
  140. if(is_object($type))
  141. return $type;
  142. if(!isset($this->transports[$type]))
  143. throw new HessianException("The transport of type $type cannot be found");
  144. $class = $this->transports[$type];
  145. $trans = new $class();
  146. $trans->testAvailable();
  147. return $trans;
  148. }
  149. function loadVersion2($mode, $stream, $options){
  150. if($mode == 'parser'){
  151. include_once 'Hessian2/Hessian2ServiceParser.php';
  152. $resolver = $this->getRulesResolver(2, 'Hessian2/hessian2rules.php');
  153. $parser = new Hessian2ServiceParser($resolver, $stream, $options);
  154. $filters['date'] = array('HessianDatetimeAdapter','toObject');
  155. $filters = array_merge($filters, $options->parseFilters);
  156. $parser->setFilters(new HessianCallbackHandler($filters));
  157. return $parser;
  158. }
  159. if($mode == 'writer'){
  160. include_once 'Hessian2/Hessian2ServiceWriter.php';
  161. include_once 'Hessian2/Hessian2IteratorWriter.php';
  162. $writer = new Hessian2ServiceWriter($options);
  163. $filters['@DateTime'] = array('HessianDatetimeAdapter','writeTime');
  164. $filters['@Iterator'] = array( new Hessian2IteratorWriter(), 'write');
  165. $filters = array_merge($filters, $options->writeFilters);
  166. $writer->setFilters(new HessianCallbackHandler($filters));
  167. return $writer;
  168. }
  169. if($mode == 'detect'){
  170. include_once 'Hessian2/Hessian2ServiceParser.php';
  171. return Hessian2ServiceParser::detectVersion($stream);
  172. }
  173. }
  174. function loadVersion1($mode, $stream, $options){
  175. if($mode == 'parser'){
  176. include_once 'Hessian1/Hessian1ServiceParser.php';
  177. $resolver = $this->getRulesResolver(1, 'Hessian1/hessian1rules.php');
  178. $parser = new Hessian1ServiceParser($resolver, $stream, $options);
  179. $filters['date'] = array('HessianDatetimeAdapter','toObject');
  180. $filters = array_merge($filters, $options->parseFilters);
  181. $parser->setFilters(new HessianCallbackHandler($filters));
  182. return $parser;
  183. }
  184. if($mode == 'writer'){
  185. include_once 'Hessian1/Hessian1ServiceWriter.php';
  186. include_once 'Hessian1/Hessian1IteratorWriter.php';
  187. $writer = new Hessian1ServiceWriter($options);
  188. $filters['@DateTime'] = array('HessianDatetimeAdapter','writeTime');
  189. $filters['@Iterator'] = array( new Hessian1IteratorWriter(), 'write');
  190. $filters = array_merge($filters, $options->writeFilters);
  191. $writer->setFilters(new HessianCallbackHandler($filters));
  192. return $writer;
  193. }
  194. if($mode == 'detect'){
  195. include_once 'Hessian1/Hessian1ServiceParser.php';
  196. return Hessian1ServiceParser::detectVersion($stream);
  197. }
  198. }
  199. }