Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hook management plugin and base library - refs BT#9092
- Loading branch information
Daniel Barreto
committed
Feb 5, 2015
1 parent
8e8c799
commit a95136f
Showing
19 changed files
with
1,201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/* For licensing terms, see /license.txt */ | ||
|
||
/** | ||
* Class HookCreateUser | ||
* @var \SplObjectStorage $observers | ||
*/ | ||
class HookCreateUser extends HookEvent implements HookCreateUserEventInterface | ||
{ | ||
protected function __construct() | ||
{ | ||
parent::__construct('HookCreateUser'); | ||
} | ||
|
||
/** | ||
* Update all the observers | ||
* @param int $type | ||
* @return int | ||
*/ | ||
public function notifyCreateUser($type) | ||
{ | ||
/** @var \HookCreateUserObserverInterface $observer */ | ||
$this->eventData['type'] = $type; | ||
foreach ($this->observers as $observer) { | ||
$observer->hookCreateUser($this); | ||
} | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
/* For licensing terms, see /license.txt */ | ||
|
||
|
||
abstract class HookEvent implements HookEventInterface | ||
{ | ||
public $observers; | ||
public $eventName; | ||
public $eventData; | ||
|
||
/** | ||
* Construct Method | ||
* @param $eventName | ||
* @throws Exception | ||
*/ | ||
protected function __construct($eventName) | ||
{ | ||
if ($this->isHookPluginActive()) { | ||
$this->observers = new SplObjectStorage(); | ||
$this->eventName = $eventName; | ||
$this->eventData = array(); | ||
$this->plugin = HookManagementPlugin::create(); | ||
$this->loadAttachments(); | ||
} else { | ||
throw new \Exception('Hook Management Plugin is not active'); | ||
} | ||
} | ||
|
||
/** | ||
* Return the singleton instance of Hook event. | ||
* If Hook Management plugin is not enabled, will return NULL | ||
* @return HookEventInterface|null | ||
*/ | ||
public static function create() | ||
{ | ||
static $result = null; | ||
|
||
if ($result) { | ||
return $result; | ||
} else { | ||
try { | ||
$class = get_called_class(); | ||
return new $class; | ||
} catch (Exception $e) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* (PHP 5 >= 5.1.0)<br/> | ||
* Attach an SplObserver | ||
* @link http://php.net/manual/en/splsubject.attach.php | ||
* @param SplObserver $observer <p> | ||
* The <b>SplObserver</b> to attach. | ||
* </p> | ||
* @return void | ||
*/ | ||
public function attach(SplObserver $observer) | ||
{ | ||
global $_hook; | ||
$observerClass = get_class($observer); | ||
$_hook[$this->eventName][$observerClass] = 1; | ||
$this->observers->attach($observer); | ||
$this->plugin->insertHook($this->eventName, $observerClass, HOOK_TYPE_ALL); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.1.0)<br/> | ||
* Detach an observer | ||
* @link http://php.net/manual/en/splsubject.detach.php | ||
* @param SplObserver $observer <p> | ||
* The <b>SplObserver</b> to detach. | ||
* </p> | ||
* @return void | ||
*/ | ||
public function detach(SplObserver $observer) | ||
{ | ||
global $_hook; | ||
$observerClass = get_class($observer); | ||
unset($_hook[$this->eventName][$observerClass]); | ||
$this->observers->detach($observer); | ||
$this->plugin->deleteHook($this->eventName, $observerClass, HOOK_TYPE_ALL); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.1.0)<br/> | ||
* Notify an observer | ||
* @link http://php.net/manual/en/splsubject.notify.php | ||
* @return void | ||
*/ | ||
public function notify() | ||
{ | ||
foreach ($this->observers as $observer) { | ||
$observer->update($this); | ||
} | ||
} | ||
|
||
/** | ||
* Return the event name refer to where hook is used | ||
* @return string | ||
*/ | ||
public function getEventName() | ||
{ | ||
return $this->eventName; | ||
} | ||
|
||
/** | ||
* Return an array containing all data needed by the hook observer to update | ||
* @return array | ||
*/ | ||
public function getEventData() | ||
{ | ||
return $this->eventData; | ||
} | ||
|
||
/** | ||
* Set an array with data needed by hooks | ||
* @param array $data | ||
* @return $this | ||
*/ | ||
public function setEventData(array $data) | ||
{ | ||
$this->eventData = $data; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Load all hook observer already registered from Session or Database | ||
* @return $this | ||
*/ | ||
public function loadAttachments() | ||
{ | ||
global $_hook; | ||
if (isset($_hook[$this->eventName]) && is_array($_hook[$this->eventName])) { | ||
foreach ($_hook[$this->eventName] as $hookObserver => $val) { | ||
$hookObserverInstance = $hookObserver::create(); | ||
$this->observers->attach($hookObserverInstance); | ||
} | ||
} else { | ||
// Load from Database and save into global name | ||
$_hook[$this->eventName] = $this->plugin->listHookObservers($this->eventName); | ||
if (isset($_hook[$this->eventName]) && is_array($_hook[$this->eventName])) { | ||
foreach ($_hook[$this->eventName] as $hookObserver => $val) { | ||
$hookObserverInstance = $hookObserver::create(); | ||
$this->observers->attach($hookObserverInstance); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Detach all hook observers | ||
* @return $this | ||
*/ | ||
public function detachAll() | ||
{ | ||
global $_hook; | ||
$_hook[$this->eventName] = null; | ||
$this->observers->removeAll($this->observers); | ||
} | ||
|
||
/** | ||
* Clear all hookObservers without detach them | ||
* @return mixed | ||
*/ | ||
public function clearAttachments() | ||
{ | ||
$this->observers->removeAll($this->observers); | ||
} | ||
|
||
/** | ||
* Return true if HookManagement plugin is active. Else, false. | ||
* This is needed to check if hook event can be instantiated | ||
* @return boolean | ||
*/ | ||
public static function isHookPluginActive() | ||
{ | ||
$isActive = false; | ||
$appPlugin = new AppPlugin(); | ||
$pluginList = $appPlugin->getInstalledPluginListName(); | ||
var_dump($pluginList); | ||
if (in_array(HOOK_MANAGEMENT_PLUGIN, $pluginList)) { | ||
$isActive = true; | ||
} | ||
|
||
return $isActive; | ||
} | ||
} |
Oops, something went wrong.