PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Engine/Comet.php

https://github.com/shopaholiccompany/shopaholic
PHP | 111 lines | 76 code | 12 blank | 23 comment | 16 complexity | b3daa7ea3ef4b077ab05669e2508aea3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Comet
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Comet.php 7244 2010-09-01 01:49:53Z john $
  10. */
  11. /**
  12. * @category Engine
  13. * @package Engine_Comet
  14. * @copyright Copyright 2006-2010 Webligo Developments
  15. * @license http://www.socialengine.net/license/
  16. */
  17. class Engine_Comet
  18. {
  19. /**
  20. * @var Engine_Comet_Backend_Abstract
  21. */
  22. protected $_backend;
  23. /**
  24. * @var Engine_Comet_Frontend_Abstract
  25. */
  26. protected $_frontent;
  27. public function __construct($options = array())
  28. {
  29. if( is_array($options) ) {
  30. $this->setOptions($options);
  31. }
  32. // Check options
  33. if( null === $this->_backend ) {
  34. throw new Engine_Comet_Exeption('No backend configured.');
  35. }
  36. if( null === $this->_frontend ) {
  37. throw new Engine_Comet_Exeption('No frontend configured.');
  38. }
  39. }
  40. public function setOptions(array $options)
  41. {
  42. foreach( $options as $key => $value ) {
  43. $method = 'set' . ucfirst($key);
  44. if( method_exists($this, $method) ) {
  45. $this->$method($value);
  46. }
  47. }
  48. return $this;
  49. }
  50. public function setBackend($backend)
  51. {
  52. if( is_array($backend) ) {
  53. $backend = $this->_loadBackend($backend);
  54. } else if( !($backend instanceof Engine_Comet_Backend_Abstract) ) {
  55. throw new Engine_Comet_Exception(sprintf('Invalid type passed to %s : %s', __METHOD__, gettype($backend)));
  56. }
  57. $this->_backend = $backend;
  58. return $this;
  59. }
  60. public function setFrontend($frontend)
  61. {
  62. if( is_array($frontend) ) {
  63. $frontend = $this->_loadFrontend($frontend);
  64. } else if( !($frontend instanceof Engine_Comet_Frontend_Abstract) ) {
  65. throw new Engine_Comet_Exception(sprintf('Invalid type passed to %s : %s', __METHOD__, gettype($frontend)));
  66. }
  67. $this->_frontend = $frontend;
  68. return $this;
  69. }
  70. // Utility
  71. protected function _loadBackend(array $options)
  72. {
  73. if( !isset($options['adapter']) || !is_string($options['adapter']) ) {
  74. throw new Engine_Comet_Exception('Adapter not set or not string.');
  75. }
  76. $adapter = $options['adapter'];
  77. unset($options['adapter']);
  78. $class = 'Engine_Comet_Backend_' . ucfirst($adapter);
  79. Engine_Loader::loadClass($class);
  80. if( !is_subclass_of($class, 'Engine_Comet_Backend_Abstract') ) {
  81. throw new Engine_Comet_Exception(sprintf('Adapter %s does not extend Engine_Comet_Backend_Abstract', $class));
  82. }
  83. return new $class($options);
  84. }
  85. protected function _loadFrontend(array $options)
  86. {
  87. if( !isset($options['adapter']) || !is_string($options['adapter']) ) {
  88. throw new Engine_Comet_Exception('Adapter not set or not string.');
  89. }
  90. $adapter = $options['adapter'];
  91. unset($options['adapter']);
  92. $class = 'Engine_Comet_Frontend_' . ucfirst($adapter);
  93. Engine_Loader::loadClass($class);
  94. if( !is_subclass_of($class, 'Engine_Comet_Frontend_Abstract') ) {
  95. throw new Engine_Comet_Exception(sprintf('Adapter %s does not extend Engine_Comet_Frontend_Abstract', $class));
  96. }
  97. return new $class($options);
  98. }
  99. };