mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Create a basic structure for the Push driver system
This commit is contained in:
parent
f01fb9ae42
commit
e23c693446
6 changed files with 599 additions and 0 deletions
41
common/framework/drivers/push/apns.php
Normal file
41
common/framework/drivers/push/apns.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The APNs (Apple) Push driver.
|
||||
*/
|
||||
class APNs extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array();
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @return bool
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message): bool
|
||||
{
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
}
|
||||
94
common/framework/drivers/push/base.php
Normal file
94
common/framework/drivers/push/base.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The base class for other Push drivers.
|
||||
*/
|
||||
abstract class Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* The configuration is stored here.
|
||||
*/
|
||||
protected $_config = null;
|
||||
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array();
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config): void
|
||||
{
|
||||
$this->_config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the current Push driver, using the given settings.
|
||||
*
|
||||
* @param array $config
|
||||
* @return object
|
||||
*/
|
||||
public static function getInstance(array $config): object
|
||||
{
|
||||
return new static($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return class_basename(get_called_class());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig(): array
|
||||
{
|
||||
return static::$_required_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields optionally used by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOptionalConfig(): array
|
||||
{
|
||||
return static::$_optional_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @return bool
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
60
common/framework/drivers/push/dummy.php
Normal file
60
common/framework/drivers/push/dummy.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The dummy Push driver.
|
||||
*/
|
||||
class Dummy extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* Sent messages are stored here for debugging and testing.
|
||||
*/
|
||||
protected $_sent_messages = array();
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @return bool
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message): bool
|
||||
{
|
||||
$this->_sent_messages[] = $message;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sent messages.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSentMessages(): array
|
||||
{
|
||||
return $this->_sent_messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset sent messages.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetSentMessages(): void
|
||||
{
|
||||
$this->_sent_messages = array();
|
||||
}
|
||||
}
|
||||
41
common/framework/drivers/push/fcm.php
Normal file
41
common/framework/drivers/push/fcm.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The FCM (Google) Push driver.
|
||||
*/
|
||||
class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array();
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Check if the current SMS driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @return bool
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message): bool
|
||||
{
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
}
|
||||
57
common/framework/drivers/pushinterface.php
Normal file
57
common/framework/drivers/pushinterface.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers;
|
||||
|
||||
/**
|
||||
* The Push driver interface.
|
||||
*/
|
||||
interface PushInterface
|
||||
{
|
||||
/**
|
||||
* Create a new instance of the current Push driver, using the given settings.
|
||||
*
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public static function getInstance(array $config): object;
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string;
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig(): array;
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields optionally used by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOptionalConfig(): array;
|
||||
|
||||
/**
|
||||
* Check if the current SMS driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool;
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @return bool
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message): bool;
|
||||
}
|
||||
306
common/framework/push.php
Normal file
306
common/framework/push.php
Normal file
|
|
@ -0,0 +1,306 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The Push class.
|
||||
*/
|
||||
class Push
|
||||
{
|
||||
/**
|
||||
* Instance properties.
|
||||
*/
|
||||
public $driver = null;
|
||||
protected $from = 0;
|
||||
protected $to = array();
|
||||
protected $subject = '';
|
||||
protected $content = '';
|
||||
protected $image = '';
|
||||
protected $url = '';
|
||||
protected $errors = array();
|
||||
protected $sent = false;
|
||||
|
||||
/**
|
||||
* Static properties.
|
||||
*/
|
||||
public static $custom_drivers = array();
|
||||
|
||||
/**
|
||||
* Add a custom Push driver.
|
||||
*/
|
||||
public static function addDriver(Drivers\PushInterface $driver): void
|
||||
{
|
||||
self::$custom_drivers[] = $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of supported Push drivers.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getSupportedDrivers()
|
||||
{
|
||||
$result = array();
|
||||
foreach (Storage::readDirectory(__DIR__ . '/drivers/push', false) as $filename)
|
||||
{
|
||||
$driver_name = substr($filename, 0, -4);
|
||||
$class_name = '\Rhymix\Framework\Drivers\Push\\' . $driver_name;
|
||||
if ($class_name::isSupported())
|
||||
{
|
||||
$result[$driver_name] = array(
|
||||
'name' => $class_name::getName(),
|
||||
'required' => $class_name::getRequiredConfig(),
|
||||
'optional' => $class_name::getOptionalConfig(),
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (self::$custom_drivers as $driver)
|
||||
{
|
||||
if ($driver->isSupported())
|
||||
{
|
||||
$result[strtolower(class_basename($driver))] = array(
|
||||
'name' => $driver->getName(),
|
||||
'required' => $driver->getRequiredConfig(),
|
||||
'optional' => $driver->getOptionalConfig(),
|
||||
);
|
||||
}
|
||||
}
|
||||
ksort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sender's member_srl.
|
||||
*
|
||||
* @param int $member_srl
|
||||
* @return bool
|
||||
*/
|
||||
public function setFrom(int $member_srl): bool
|
||||
{
|
||||
$this->from = $member_srl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sender's phone number.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getFrom(): ?int
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a recipient.
|
||||
*
|
||||
* @param int $member_srl
|
||||
* @return bool
|
||||
*/
|
||||
public function addTo(int $member_srl): bool
|
||||
{
|
||||
$this->to[] = $member_srl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of recipients without country codes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subject.
|
||||
*
|
||||
* @param string $subject
|
||||
* @return bool
|
||||
*/
|
||||
public function setSubject(string $subject): bool
|
||||
{
|
||||
$this->subject = utf8_trim(utf8_clean($subject));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subject.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content.
|
||||
*
|
||||
* @param string $content
|
||||
* @return bool
|
||||
*/
|
||||
public function setContent(string $content): bool
|
||||
{
|
||||
$this->content = utf8_trim(utf8_clean($content));
|
||||
$this->content = strtr($this->content, array("\r\n" => "\n"));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an image to associate with this push notification.
|
||||
*
|
||||
* @param string $image
|
||||
* @return bool
|
||||
*/
|
||||
public function setImage(string $image): bool
|
||||
{
|
||||
$this->image = utf8_trim(utf8_clean($image));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image associated with this push notification.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImage(): string
|
||||
{
|
||||
return $this->image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a URL to associate with this push notification.
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
public function setURL(string $url): bool
|
||||
{
|
||||
$this->url = $url;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL associated with this push notification.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getURL(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the message.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function send(): bool
|
||||
{
|
||||
// Get caller information.
|
||||
$backtrace = debug_backtrace(0);
|
||||
if(count($backtrace) && isset($backtrace[0]['file']))
|
||||
{
|
||||
$this->caller = $backtrace[0]['file'] . ($backtrace[0]['line'] ? (' line ' . $backtrace[0]['line']) : '');
|
||||
}
|
||||
|
||||
$output = \ModuleHandler::triggerCall('push.send', 'before', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config('push.default_force') && config('push.default_from'))
|
||||
{
|
||||
$this->setFrom(config('push.default_from'));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if ($this->driver)
|
||||
{
|
||||
$this->sent = $this->driver->send($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->errors[] = 'No Push driver selected';
|
||||
$this->sent = false;
|
||||
}
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$this->errors[] = class_basename($e) . ': ' . $e->getMessage();
|
||||
$this->sent = false;
|
||||
}
|
||||
|
||||
$output = \ModuleHandler::triggerCall('push.send', 'after', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
}
|
||||
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the message was sent.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSent(): bool
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get caller information.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCaller(): string
|
||||
{
|
||||
return $this->caller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get errors.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an error message.
|
||||
*
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
public function addError(string $message): void
|
||||
{
|
||||
$this->errors[] = $message;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue