/lib/YAPNS.php

https://github.com/shiki/yii-apns · PHP · 171 lines · 72 code · 27 blank · 72 comment · 12 complexity · eb5b383af28a44c30f7ce9a29bcdab0f MD5 · raw file

  1. <?php
  2. namespace YAPNS;
  3. use \YAPNS\YAPNSLogger;
  4. /**
  5. * Wraps ApnsPHP in a Yii extension.
  6. *
  7. * This takes care of setting up the autoloader for the library. This also connects to the server
  8. * when necessary so you only need to call addMessage() and send().
  9. *
  10. * @see http://code.google.com/p/apns-php/
  11. *
  12. * @package YAPNS
  13. * @author Shiki
  14. */
  15. class YAPNS extends \CApplicationComponent
  16. {
  17. const ENV_SANDBOX = 'sandbox';
  18. const ENV_PRODUCTION = 'production';
  19. /**
  20. * The path to the ApnsPHP library. If not provided, this component will try to load the library
  21. * that comes with extension package.
  22. * @var string
  23. */
  24. public $apnsPHPLibPath;
  25. /**
  26. *
  27. * @var string
  28. */
  29. public $environment = self::ENV_SANDBOX;
  30. /**
  31. * Push SSL Certificate file with key (Bundled PEM). You can get this by downloading the
  32. * certificate and key from Apple and converting them to a pem file. See here for more info:
  33. * http://code.google.com/p/apns-php/wiki/CertificateCreation
  34. *
  35. * @var string
  36. */
  37. public $providerCertificateFilePath;
  38. /**
  39. * From Apple's documentation: To establish a TLS session with APNs, an Entrust Secure CA root
  40. * certificate must be installed on the provider’s server. (http://bit.ly/hUEl4d). This might
  41. * already be on your server. If not, you can download it from Entrust's website. On the latest
  42. * Ubuntu server, this was on: /etc/ssl/certs/Entrust.net_Premium_2048_Secure_Server_CA.pem
  43. * See here for more info: http://code.google.com/p/apns-php/wiki/CertificateCreation
  44. *
  45. * @var string
  46. */
  47. public $rootCertificationAuthorityFilePath;
  48. /**
  49. *
  50. * @var \ApnsPHP_Push
  51. */
  52. protected $_pushProvider;
  53. /**
  54. * Initializes the application component.
  55. * This method is required by {@link IApplicationComponent} and is invoked by application.
  56. */
  57. public function init()
  58. {
  59. if (empty($this->providerCertificateFilePath))
  60. throw new \CException('Push SSL certificate is required.');
  61. if (!in_array($this->environment, array(self::ENV_PRODUCTION, self::ENV_SANDBOX)))
  62. throw new \CException('Environment is invalid.');
  63. $this->initAutoloader();
  64. \Yii::app()->attachEventHandler('onEndRequest', array($this, 'onApplicationEndRequest'));
  65. parent::init();
  66. }
  67. /**
  68. * @return \ApnsPHP_Push
  69. */
  70. public function getPushProvider()
  71. {
  72. if ($this->_pushProvider)
  73. return $this->_pushProvider;
  74. $push = new \ApnsPHP_Push(
  75. ($this->environment == self::ENV_PRODUCTION) ? \ApnsPHP_Push::ENVIRONMENT_PRODUCTION : \ApnsPHP_Push::ENVIRONMENT_SANDBOX,
  76. $this->providerCertificateFilePath
  77. );
  78. if ($this->rootCertificationAuthorityFilePath)
  79. $push->setRootCertificationAuthority($this->rootCertificationAuthorityFilePath);
  80. $push->setLogger(new YAPNSLogger());
  81. $push->connect();
  82. $this->_pushProvider = $push;
  83. return $this->_pushProvider;
  84. }
  85. /**
  86. * Forward method calls to push provider (e.g. calling Yii::app()->apns->add($message)
  87. * will call Yii::app()->getPushProvider()->add($message)).
  88. *
  89. * @param string $name
  90. * @param array $parameters
  91. * @return mixed
  92. */
  93. public function __call($name, $parameters)
  94. {
  95. $push = $this->getPushProvider();
  96. if (method_exists($push, $name))
  97. return call_user_func_array(array($push, $name), $parameters);
  98. return parent::__call($name, $parameters);
  99. }
  100. /**
  101. * Disconnect push provider on app exit.
  102. * @param CEvent $event
  103. */
  104. public function onApplicationEndRequest(\CEvent $event)
  105. {
  106. //echo 'disconnecting'; die();
  107. if ($this->_pushProvider)
  108. $this->_pushProvider->disconnect();
  109. }
  110. /**
  111. *
  112. */
  113. public function initAutoloader()
  114. {
  115. defined('YAPNS_LIB_PATH')
  116. or define('YAPNS_LIB_PATH', $this->apnsPHPLibPath ? $this->apnsPHPLibPath : FF_SHARED_LIB_PATH . '/ApnsPHP');
  117. \Yii::registerAutoloader(array(__CLASS__, 'autoload'));
  118. }
  119. /**
  120. * A modified version of ApnsPHP_Autoload tailored to work with Yii. There's generally no
  121. * need to register or use this directly.
  122. *
  123. * This autoloader fails if initAutoloader is not called first or YAPNS_LIB_PATH is not defined.
  124. *
  125. * @param string $className
  126. */
  127. public static function autoload($className)
  128. {
  129. //if a long name separated by `\` (i.e. with namespace). get only last part.
  130. $temp = explode('\\', $className);
  131. $className = end($temp);
  132. if (!defined('YAPNS_LIB_PATH'))
  133. return;
  134. if (empty($className) || strpos($className, 'ApnsPHP_') !== 0)
  135. return;
  136. $filePath = sprintf('%s%s%s.php',
  137. YAPNS_LIB_PATH, DIRECTORY_SEPARATOR,
  138. str_replace('_', DIRECTORY_SEPARATOR, $className)
  139. );
  140. if (!is_file($filePath) || !is_readable($filePath))
  141. return; // let Yii handle this
  142. require_once($filePath);
  143. }
  144. }