PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/phpdemo/application/libraries/demisauce.php

https://github.com/cccarey/demisauce
PHP | 252 lines | 207 code | 17 blank | 28 comment | 38 complexity | 1da253d136218bcd26227b534ed9a423 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * demisauce PHP Library
  4. *
  5. * A php library to the demisauce server Services
  6. *
  7. * @package demisauce
  8. * @author Aaron Raddon
  9. * @copyright Copyright (c) 2008, Aaron Raddon
  10. * @license http://code.google.com/p/demisauce/source/browse
  11. * @link http://demisauce.org
  12. */
  13. // TODO, address format=xml but html contained inside gracefully
  14. function poll_html($resource) {
  15. $ds = Demisauce::get_app();
  16. $content = $ds->get_service($resource,$service='poll',$format='xml',$app='demisauce');
  17. return $content[0]->poll->html;
  18. }
  19. function demisauce_html($service, $resource_id){
  20. $ds = Demisauce::get_app();
  21. }
  22. class DemisauceServiceBase {
  23. function __construct($props = array()){
  24. $this->initialize($props);
  25. }
  26. function initialize($setas = array()) {
  27. foreach ($setas as $key => $val) {
  28. $method = 'set_'.$key;
  29. if (method_exists($this, $method)) {
  30. $this->$method($val);
  31. } else {
  32. $this->$key = $val;
  33. }
  34. }
  35. }
  36. public function debug_info(){
  37. $html = "";
  38. $class_vars = get_class_vars(get_class($this));
  39. foreach ($class_vars as $name => $value) {
  40. $html = $html."$name : ".$this->$name."<br />";
  41. }
  42. return $html;
  43. }
  44. }
  45. class DemisauceServiceResponse extends DemisauceServiceBase
  46. {
  47. var $success = False;
  48. var $data;
  49. var $service = 'servicename';
  50. var $message = 'new response, no message yet';
  51. var $format = 'xml';
  52. var $xml;
  53. var $url = '';
  54. function __construct($props){
  55. parent::__construct($props);
  56. }
  57. }
  58. class DemisauceService extends DemisauceServiceBase {
  59. var $key_format = '{app_slug}/{class_name}/{local_key}';
  60. var $url_format = "{base_url}/api/{format}/{service}/{key}?apikey={api_key}";
  61. var $service = 'service';
  62. var $method_url; // if not same as service
  63. var $format = 'xml';
  64. var $app_slug = 'demisauce';
  65. var $cache = True;
  66. var $data = array();
  67. var $isdefined = False;
  68. //var $method_url = None
  69. //var $service_registry = None
  70. var $api_key = '';
  71. var $base_url = 'http://www.demisauce.com';
  72. function __construct($props){
  73. parent::__construct($props);
  74. }
  75. public function fetch($resource_id,$format='xml') {
  76. //echo "ARGS2: $resource_id , $format , <br/>";
  77. $ds_url = $this->url_formatter(array(
  78. 'key' => $resource_id,
  79. 'format' => $format
  80. ));
  81. //echo "ds_url. = $ds_url<br />";
  82. $response = new DemisauceServiceResponse(array(
  83. 'service' => $this->service,
  84. 'format' => $format,
  85. 'url' => $ds_url
  86. ));
  87. try {
  88. $response->data = file_get_contents((string)$ds_url);
  89. if ($response->data === FALSE) {
  90. //TODO: get http response code
  91. $response->message = 'failure, no response';
  92. return $response;
  93. }
  94. if ($format == 'xml') {
  95. $response->xml = simplexml_load_string($response->data);
  96. $response->success = true;
  97. //echo $response->data;
  98. return $response;
  99. } else {
  100. $response->success = true;
  101. return $response;
  102. }
  103. } catch (Exception $e) {
  104. // TODO: add error message from exception
  105. //echo 'CRAPOLO, bad response';
  106. $response->message = 'failure, error on response';
  107. return $response;
  108. }
  109. }
  110. public function url_formatter($suba = array()){
  111. if (is_null($this->url_format)){
  112. return $this->base_url."/".$this->method_url;
  113. } else {
  114. $url_format = $this->url_format;
  115. $sub_dict = get_class_vars(get_class($this));
  116. foreach ($sub_dict as $key => $value) {
  117. if (is_string($value)){
  118. $url_format = str_replace("{".$key."}",$this->$key,$url_format);
  119. }
  120. }
  121. foreach ($suba as $key => $value) {
  122. $url_format = str_replace("{".$key."}",$value,$url_format);
  123. }
  124. return $url_format;
  125. }
  126. }
  127. }
  128. class Demisauce extends DemisauceServiceBase{
  129. var $cache = 0;
  130. var $memcached;
  131. var $apiver = "/api/";
  132. static $api_key = "";
  133. //var $getitems = array();
  134. public static $service_app_slug = 'demisauce';
  135. public static $base_url = "http://www.demisauce.com";
  136. public static $this_app_slug = 'yourphpapp';
  137. public static $demisauce_base_url = 'http://www.demisauce.com';
  138. private $service_reg = array();
  139. public static $service_app_slug2 = 'demisauce';
  140. function __construct($props = array()) {
  141. parent::__construct();
  142. if (count($props) > 0) {
  143. $this->initialize($props);
  144. }
  145. }
  146. static private $m_demisauce_app = NULL;
  147. public static function get_app() {
  148. if (self::$m_demisauce_app == NULL) {
  149. self::$m_demisauce_app = new Demisauce();
  150. }
  151. return self::$m_demisauce_app;
  152. }
  153. private function check_cache() {
  154. }
  155. private function get_service_definition($dss,$serviceapp){
  156. // TODO: check cache
  157. $service_def = new DemisauceService(array(
  158. 'service' => 'service',
  159. 'api_key' => Demisauce::$api_key,
  160. 'base_url' => Demisauce::$demisauce_base_url,
  161. ));
  162. $response = $service_def->fetch($serviceapp);
  163. //echo $response->xml;
  164. if ($response->success){
  165. // should be one service
  166. $response->success = false; //temp
  167. foreach ($response->xml as $svc_xml) {
  168. $response->success = true;
  169. if (property_exists($svc_xml, 'base_url')) {
  170. $dss->base_url = $svc_xml->base_url;
  171. }
  172. if (property_exists($svc_xml, 'method_url') && $svc_xml->method_url != 'None') {
  173. $dss->method_url = $svc_xml->method_url;
  174. }
  175. if (property_exists($svc_xml, 'url_format') && $svc_xml->url_format != 'None') {
  176. $dss->url_format = $svc_xml->url_format;
  177. } else {
  178. $dss->url_format = null;
  179. }
  180. }
  181. // add service base to registry
  182. if ($response->success){
  183. $this->service_reg[$serviceapp] = $dss;
  184. }
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. public function get_service($resource,$service='',$format='xml',$app='demisauce'){
  191. //echo "ARGS1: $resource , $service , $format , $app <br/>";
  192. if ($app == '') {
  193. $app = $this->service_app_slug;
  194. }
  195. $serviceapp = $app."/".$service;
  196. // define the intended service
  197. $dss = new DemisauceService(array(
  198. 'service' => $service,
  199. 'api_key' => Demisauce::$api_key,
  200. 'base_url' => Demisauce::$base_url,
  201. 'app_slug' => $app,
  202. 'format' => $format
  203. ));
  204. // now get service definition
  205. if (!in_array($serviceapp,$this->service_reg)){
  206. $succeeded = $this->get_service_definition(&$dss,$serviceapp);
  207. if (!$succeeded) return null;
  208. } else {
  209. $dss = $this->service_reg[$serviceapp];
  210. }
  211. $response = $dss->fetch($resource,$format);
  212. return $this->get_response($response);
  213. }
  214. private function get_response($response){
  215. if (!is_null($response) && ($response->success == true)){
  216. if ($response->format == 'xml'){
  217. return $response->xml;
  218. } else if ($response->format == 'view'){
  219. return $response->data;
  220. } else {
  221. return $response->data;
  222. }
  223. } else {
  224. return $response->message;
  225. }
  226. }
  227. public function get_cms($resource,$format='xml') {
  228. $content = $this->get_service($resource,$service='cms',$format='xml',$app='demisauce');
  229. return $content;
  230. }
  231. public function get_poll($resource) {
  232. $content = $this->get_service($resource,$service='poll',$format='xml',$app='demisauce');
  233. return $content[0]->poll->html;
  234. }
  235. }