PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/MailWizzApi/Base.php

https://bitbucket.org/IceHalk/mailsenseysdk
PHP | 323 lines | 152 code | 26 blank | 145 comment | 28 complexity | 661b38dcbf34f9813e6d425eae4b0cd2 MD5 | raw file
  1. <?php
  2. /**
  3. * This file contains the base class for the MailWizzApi PHP-SDK.
  4. *
  5. * @author Serban George Cristian <cristian.serban@mailwizz.com>
  6. * @link http://www.mailwizz.com/
  7. * @copyright 2013-2015 http://www.mailwizz.com/
  8. */
  9. /**
  10. * MailWizzApi_Base is the base class for all the other classes used in the sdk.
  11. *
  12. * @author Serban George Cristian <cristian.serban@mailwizz.com>
  13. * @package MailWizzApi
  14. * @since 1.0
  15. */
  16. class MailWizzApi_Base
  17. {
  18. /**
  19. * Marker for before send request event
  20. */
  21. const EVENT_BEFORE_SEND_REQUEST = 'beforeSendRequest';
  22. /**
  23. * Marker for after send request event
  24. */
  25. const EVENT_AFTER_SEND_REQUEST = 'afterSendRequest';
  26. /**
  27. * @var MailWizzApi_Config the configuration object injected into the application at runtime
  28. */
  29. private static $_config;
  30. /**
  31. * @var MailWizzApi_Params the package registry that will hold various components
  32. */
  33. private static $_registry = array();
  34. /**
  35. * @var MailWizzApi_Params the registered event handlers
  36. */
  37. private static $_eventHandlers = array();
  38. /**
  39. * Inject the configuration into the sdk
  40. *
  41. * @param MailWizzApi_Config $config
  42. */
  43. public static function setConfig(MailWizzApi_Config $config)
  44. {
  45. self::$_config = $config;
  46. }
  47. /**
  48. * Returns the configuration object
  49. *
  50. * @return MailWizzApi_Config
  51. */
  52. public static function getConfig()
  53. {
  54. return self::$_config;
  55. }
  56. /**
  57. * Add a new component to the registry
  58. *
  59. * @param string $key
  60. * @param mixed $value
  61. * @return MailWizzApi_Base
  62. */
  63. public function addToRegistry($key, $value)
  64. {
  65. $this->getRegistry()->add($key, $value);
  66. return $this;
  67. }
  68. /**
  69. * Get the current registry object
  70. *
  71. * @return MailWizzApi_Params
  72. */
  73. public function getRegistry()
  74. {
  75. if (!(self::$_registry instanceof MailWizzApi_Params)) {
  76. self::$_registry = new MailWizzApi_Params(self::$_registry);
  77. }
  78. return self::$_registry;
  79. }
  80. /**
  81. * Set the components used throughout the application lifecyle.
  82. *
  83. * Each component config array needs to have a `class` key containing a class name that can be autoloaded.
  84. * For example, adding a cache component would be done like :
  85. *
  86. * <pre>
  87. * $components = array(
  88. * 'cache'=>array(
  89. * 'class' => 'MailWizzApi_Cache_Sqlite',
  90. * 'connectionString' => 'sqlite:/absolute/path/to/your/sqlite.db',
  91. * ),
  92. * );
  93. * $context->setComponents($components);
  94. * </pre>
  95. *
  96. * Please note, if a named component exists, and you assign one with the same name,
  97. * it will get overriden by the second one.
  98. *
  99. * @param array $components
  100. * @return MailWizzApi_Base
  101. */
  102. public function setComponents(array $components)
  103. {
  104. foreach ($components as $componentName => $config) {
  105. $this->setComponent($componentName, $config);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Set a single component used throughout the application lifecyle.
  111. *
  112. * The component config array needs to have a `class` key containing a class name that can be autoloaded.
  113. * For example, adding a cache component would be done like :
  114. *
  115. * <pre>
  116. * $context->setComponent('cache', array(
  117. * 'class' => 'MailWizzApi_Cache_Sqlite',
  118. * 'connectionString' => 'sqlite:/absolute/path/to/your/sqlite.db',
  119. * ));
  120. * </pre>
  121. *
  122. * Please note, if a named component exists, and you assign one with the same name,
  123. * it will get overriden by the second one.
  124. *
  125. * @param string $componentName the name of the component accessed later via $context->componentName
  126. * @param array $config the component configuration array
  127. * @return MailWizzApi_Base
  128. */
  129. public function setComponent($componentName, array $config)
  130. {
  131. if (empty($config['class'])) {
  132. throw new Exception('Please set the class property for "'.htmlspecialchars($componentName, ENT_QUOTES, $this->getConfig()->getCharset()).'" component.');
  133. }
  134. $component = new $config['class'];
  135. if ($component instanceof MailWizzApi_Base) {
  136. $component->populateFromArray($config);
  137. } else {
  138. unset($config['class']);
  139. foreach ($config as $property => $value) {
  140. if (property_exists($component, $property)) {
  141. $reflection = new ReflectionProperty($component, $property);
  142. if ($reflection->isPublic()) {
  143. $component->$property = $value;
  144. }
  145. }
  146. }
  147. }
  148. $this->addToRegistry($componentName, $component);
  149. return $this;
  150. }
  151. /**
  152. * Register one or more callbacks/event handlers for the given event(s)
  153. *
  154. * A valid registration would be:
  155. *
  156. * <pre>
  157. * $eventHandlers = array(
  158. * 'eventName1' => array($object, 'method'),
  159. * 'eventName2' => array(
  160. * array($object, 'method'),
  161. * array($object, 'otherMethod'),
  162. * )
  163. * );
  164. * </pre>
  165. *
  166. * @param array $eventHandlers
  167. * @return MailWizzApi_Base
  168. */
  169. public function setEventHandlers(array $eventHandlers)
  170. {
  171. foreach ($eventHandlers as $eventName => $callback) {
  172. if (empty($callback) || !is_array($callback)) {
  173. continue;
  174. }
  175. if (!is_array($callback[0]) && is_callable($callback)) {
  176. $this->getEventHandlers($eventName)->add(null, $callback);
  177. continue;
  178. }
  179. if (is_array($callback[0])) {
  180. foreach ($callback as $cb) {
  181. if (is_callable($cb)) {
  182. $this->getEventHandlers($eventName)->add(null, $cb);
  183. }
  184. }
  185. }
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Return a list of callbacks/event handlers for the given event
  191. *
  192. * @param string $eventName
  193. * @return MailWizzApi_Params
  194. */
  195. public function getEventHandlers($eventName)
  196. {
  197. if (!(self::$_eventHandlers instanceof MailWizzApi_Params)) {
  198. self::$_eventHandlers = new MailWizzApi_Params(self::$_eventHandlers);
  199. }
  200. if (!self::$_eventHandlers->contains($eventName) || !(self::$_eventHandlers->itemAt($eventName) instanceof MailWizzApi_Params)) {
  201. self::$_eventHandlers->add($eventName, new MailWizzApi_Params());
  202. }
  203. return self::$_eventHandlers->itemAt($eventName);
  204. }
  205. /**
  206. * Remove all the event handlers bound to the event name
  207. *
  208. * @param string $eventName
  209. * @return MailWizzApi_Base
  210. */
  211. public function removeEventHandlers($eventName)
  212. {
  213. self::$_eventHandlers->remove($eventName);
  214. return $this;
  215. }
  216. /**
  217. * Called from within a child class, will populate
  218. * all the setters matching the array keys with the array values
  219. *
  220. * @param mixed $params
  221. * @return MailWizzApi_Base
  222. */
  223. protected function populateFromArray(array $params = array())
  224. {
  225. foreach ($params as $name => $value) {
  226. $found = false;
  227. if (property_exists($this, $name)) {
  228. $param = $name;
  229. } else {
  230. $asSetterName = str_replace('_', ' ', $name);
  231. $asSetterName = ucwords($asSetterName);
  232. $asSetterName = str_replace(' ', '', $asSetterName);
  233. $asSetterName{0} = strtolower($asSetterName{0});
  234. $param = property_exists($this, $asSetterName) ? $asSetterName : null;
  235. }
  236. if ($param) {
  237. $reflection = new ReflectionProperty($this, $param);
  238. if ($reflection->isPublic()) {
  239. $this->$param = $value;
  240. $found = true;
  241. }
  242. }
  243. if (!$found) {
  244. $methodName = str_replace('_', ' ', $name);
  245. $methodName = ucwords($methodName);
  246. $methodName = str_replace(' ', '', $methodName);
  247. $methodName = 'set'.$methodName;
  248. if (method_exists($this, $methodName)) {
  249. $reflection = new ReflectionMethod($this, $methodName);
  250. if ($reflection->isPublic()) {
  251. $this->$methodName($value);
  252. }
  253. }
  254. }
  255. }
  256. return $this;
  257. }
  258. /**
  259. * Magic setter
  260. *
  261. * This method should never be called directly from outside of the class.
  262. *
  263. * @param string $name
  264. * @param mixed $value
  265. */
  266. public function __set($name, $value)
  267. {
  268. $methodName = 'set'.ucfirst($name);
  269. if (!method_exists($this, $methodName)) {
  270. $this->addToRegistry($name, $value);
  271. } else {
  272. $method = new ReflectionMethod($this, $methodName);
  273. if ($method->isPublic()) {
  274. $this->$methodName($value);
  275. }
  276. }
  277. }
  278. /**
  279. * Magic getter
  280. *
  281. * This method should never be called directly from outside of the class.
  282. *
  283. * @param string $name
  284. */
  285. public function __get($name)
  286. {
  287. $methodName = 'get'.ucfirst($name);
  288. if (!method_exists($this, $methodName) && $this->getRegistry()->contains($name)) {
  289. return $this->getRegistry()->itemAt($name);
  290. } elseif (method_exists($this, $methodName)) {
  291. $method = new ReflectionMethod($this, $methodName);
  292. if ($method->isPublic()) {
  293. return $this->$methodName();
  294. }
  295. }
  296. }
  297. }