Inplement singleton pattern in AbstractProcessor

This commit is contained in:
Dmitriy Simushev 2014-03-05 08:58:10 +00:00
parent f467c35bd3
commit 9069570f0b
4 changed files with 57 additions and 116 deletions

View File

@ -90,6 +90,8 @@ use Mibew\RequestProcessor\Exception\AbstractProcessorException;
* } * }
* </code> * </code>
* *
* Implements Singleton pattern for children classes using Late Static Bindings.
*
* @see \Mibew\RequestProcessor\AbstractProcessor::__construct() * @see \Mibew\RequestProcessor\AbstractProcessor::__construct()
*/ */
abstract class AbstractProcessor abstract class AbstractProcessor
@ -123,42 +125,24 @@ abstract class AbstractProcessor
protected $config = array(); protected $config = array();
/** /**
* Class constructor * An instance of the AbstractProcessor class
* *
* @param type $config Configuration data. * @var \Mibew\RequestProcessor\AbstractProcessor
* It must contains following keys:
* - 'signature': Use for verification sender
* - 'trusted_signatures': array of trusted signatures. Uses for identify
* another side of interaction.
* And may contains following (if not default values will be used)
* - 'event_prefix': prefix that uses for all events triggered by the
* class. The default value is the class name with first character in
* lower case
*/ */
public function __construct($config) protected static $instance = null;
/**
* Return an instance of the AbstractProcessor class.
*
* @return \Mibew\RequestProcessor\AbstractProcessor
*/
public static function getInstance()
{ {
// Check signature if (is_null(static::$instance)) {
if (!isset($config['signature'])) { static::$instance = new static();
trigger_error("Signature is not specified", E_USER_ERROR);
} }
// Check trusted signatures return static::$instance;
if (!isset($config['trusted_signatures'])) {
trigger_error("Trusted signatures is not specified", E_USER_ERROR);
}
// Get an instance of the MibewAPI class
$this->mibewAPI = $this->getMibewAPIInstance();
// Get class name and prefix for events and etc.
$class_name_parts = explode('\\', get_class($this));
$class_name = array_pop($class_name_parts);
$this->eventPrefix = empty($config['event_prefix'])
? strtolower(substr($class_name, 0, 1)) . substr($class_name, 1)
: $config['event_prefix'];
// Store config
$this->config = $config;
} }
/** /**
@ -336,6 +320,45 @@ abstract class AbstractProcessor
return $result; return $result;
} }
/**
* Class constructor
*
* @param type $config Configuration data.
* It must contains following keys:
* - 'signature': Use for verification sender
* - 'trusted_signatures': array of trusted signatures. Uses for identify
* another side of interaction.
* And may contains following (if not default values will be used)
* - 'event_prefix': prefix that uses for all events triggered by the
* class. The default value is the class name with first character in
* lower case
*/
protected function __construct($config)
{
// Check signature
if (!isset($config['signature'])) {
trigger_error("Signature is not specified", E_USER_ERROR);
}
// Check trusted signatures
if (!isset($config['trusted_signatures'])) {
trigger_error("Trusted signatures is not specified", E_USER_ERROR);
}
// Get an instance of the MibewAPI class
$this->mibewAPI = $this->getMibewAPIInstance();
// Get class name and prefix for events and etc.
$class_name_parts = explode('\\', get_class($this));
$class_name = array_pop($class_name_parts);
$this->eventPrefix = empty($config['event_prefix'])
? strtolower(substr($class_name, 0, 1)) . substr($class_name, 1)
: $config['event_prefix'];
// Store config
$this->config = $config;
}
/** /**
* Process request * Process request
* *

View File

@ -30,41 +30,13 @@ use Mibew\RequestProcessor\Exception\InviteProcessorException;
* - inviteReceiveRequestError * - inviteReceiveRequestError
* - inviteCallError * - inviteCallError
* - inviteFunctionCall * - inviteFunctionCall
*
* Implements Singleton pattern
*/ */
class InviteProcessor extends ClientSideProcessor class InviteProcessor extends ClientSideProcessor
{ {
/**
* An instance of the InviteProcessor class
*
* @var \Mibew\RequestProcessor\InviteProcessor
*/
protected static $instance = null;
/**
* Return an instance of the InviteProcessor class.
*
* @return \Mibew\RequestProcessor\InviteProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/** /**
* Class constructor * Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\InviteProcessor::getInstance() instead!
*
* @todo Think about why the method is not protected
*/ */
public function __construct() protected function __construct()
{ {
parent::__construct(array( parent::__construct(array(
'signature' => '', 'signature' => '',

View File

@ -36,32 +36,9 @@ use Mibew\RequestProcessor\Exception\ThreadProcessorException;
* WARNING: * WARNING:
* threadResponseReceived registered but never called because of asynchronous * threadResponseReceived registered but never called because of asynchronous
* nature of Core-to-Window interaction * nature of Core-to-Window interaction
*
* Implements Singleton pattern
*/ */
class ThreadProcessor extends ClientSideProcessor class ThreadProcessor extends ClientSideProcessor
{ {
/**
* An instance of the ThreadProcessor class
*
* @var \Mibew\RequestProcessor\ThreadProcessor
*/
protected static $instance = null;
/**
* Return an instance of the ThreadProcessor class.
*
* @return \Mibew\RequestProcessor\ThreadProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/** /**
* Loads thread by id and token and checks if thread loaded * Loads thread by id and token and checks if thread loaded
* *
@ -131,11 +108,8 @@ class ThreadProcessor extends ClientSideProcessor
/** /**
* Class constructor * Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\ThreadProcessor::getInstance() instead!
*/ */
public function __construct() protected function __construct()
{ {
parent::__construct(array( parent::__construct(array(
'signature' => '', 'signature' => '',

View File

@ -38,41 +38,13 @@ use Mibew\RequestProcessor\Exception\UsersProcessorException;
* Also triggers follow events (see description of apiUpdateVisitors method): * Also triggers follow events (see description of apiUpdateVisitors method):
* - usersUpdateVisitorsLoad * - usersUpdateVisitorsLoad
* - usersUpdateVisitorsAlter * - usersUpdateVisitorsAlter
*
* Implements Singleton pattern
*/ */
class UsersProcessor extends ClientSideProcessor class UsersProcessor extends ClientSideProcessor
{ {
/**
* An instance of the UsersProcessor class
*
* @var \Mibew\RequestProcessor\UsersProcessor
*/
protected static $instance = null;
/**
* Return an instance of the UsersProcessor class.
*
* @return \Mibew\RequestProcessor\UsersProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/** /**
* Class constructor * Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\UsersProcessor::getInstance() instead!
*
* @todo Think about why the method is not protected
*/ */
public function __construct() protected function __construct()
{ {
parent::__construct(array( parent::__construct(array(
'signature' => '', 'signature' => '',