Enhance Hook feature - refs BT#9092

Conflicts:
	main/inc/lib/autoload.class.php
1.10.x
Daniel Barreto 11 years ago
parent a95136fe5e
commit f90a64eae4
  1. 9
      main/admin/index.php
  2. 5
      main/inc/lib/database.constants.inc.php
  3. 32
      main/inc/lib/hook/HookAdminBlock.class.php
  4. 41
      main/inc/lib/hook/HookEvent.class.php
  5. 67
      main/inc/lib/hook/HookInterfaces.php
  6. 58
      main/inc/lib/hook/HookObserver.class.php
  7. 3
      plugin/hookmanagement/config.php
  8. 15
      plugin/hookmanagement/src/HookManagementPlugin.class.php

@ -61,6 +61,11 @@ if (isset($_GET['msg']) && isset($_GET['type'])) {
$blocks = array();
$hook = HookAdminBlock::create();
if (!empty($hook)) {
$hook->notifyAdminBlock(HOOK_TYPE_PRE);
}
/* Users */
$blocks['users']['icon'] = Display::return_icon('members.gif', get_lang('Users'), array(), ICON_SIZE_SMALL, false);
@ -338,6 +343,10 @@ if (api_is_platform_admin()) {
$blocks['version_check']['items'] = null;
$blocks['version_check']['class'] = 'block-admin-version_check';
if (!empty($hook)) {
$hook->notifyAdminBlock(HOOK_TYPE_POST);
}
}
$admin_ajax_url = api_get_path(WEB_AJAX_PATH).'admin.ajax.php';

@ -346,3 +346,8 @@ define('TABLE_TIMELINE', 'timeline');
//Gradebook model
define('TABLE_GRADE_MODEL', 'grade_model');
define('TABLE_GRADE_MODEL_COMPONENTS', 'grade_components');
// Hook tables
define('TABLE_PLUGIN_HOOK_OBSERVER', 'plugin_hook_observer');
define('TABLE_PLUGIN_HOOK_CALL', 'plugin_hook_call');
define('TABLE_PLUGIN_HOOK_EVENT', 'plugin_hook_event');

@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: dbarreto
* Date: 19/12/14
* Time: 09:45 AM
*/
class HookAdminBlock extends HookEvent implements HookAdminBlockEventInterface {
protected function __construct()
{
parent::__construct('HookAdminBlock');
}
/**
* @param int $type
* @return int
*/
public function notifyAdminBlock($type)
{
/** @var \HookAdminBlockObserverInterface $observer */
global $blocks;
$this->eventData['blocks'] = $blocks;
$this->eventData['type'] = $type;
foreach ($this->observers as $observer) {
$data = $observer->hookAdminBlock($this);
$blocks = $data['blocks'];
}
return 1;
}
}

@ -15,7 +15,7 @@ abstract class HookEvent implements HookEventInterface
*/
protected function __construct($eventName)
{
if ($this->isHookPluginActive()) {
if (self::isHookPluginActive()) {
$this->observers = new SplObjectStorage();
$this->eventName = $eventName;
$this->eventData = array();
@ -47,35 +47,36 @@ abstract class HookEvent implements HookEventInterface
}
}
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Attach an SplObserver
* Attach an HookObserver
* @link http://php.net/manual/en/splsubject.attach.php
* @param SplObserver $observer <p>
* The <b>SplObserver</b> to attach.
* @param \HookObserverInterface| $observer <p>
* The <b>HookObserver</b> to attach.
* </p>
* @return void
*/
public function attach(SplObserver $observer)
public function attach(HookObserverInterface $observer)
{
global $_hook;
$observerClass = get_class($observer);
$_hook[$this->eventName][$observerClass] = 1;
$_hook[$this->eventName][$observerClass] = array(
'class_name' => $observerClass,
'path' => $observer->getPath(),
'plugin_name' => $observer->getPluginName(),
);
$this->observers->attach($observer);
$this->plugin->insertHook($this->eventName, $observerClass, HOOK_TYPE_ALL);
}
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Detach an observer
* Detach an HookObserver
* @link http://php.net/manual/en/splsubject.detach.php
* @param SplObserver $observer <p>
* The <b>SplObserver</b> to detach.
* @param \HookObserverInterface| $observer <p>
* The <b>HookObserver</b> to detach.
* </p>
* @return void
*/
public function detach(SplObserver $observer)
public function detach(HookObserverInterface $observer)
{
global $_hook;
$observerClass = get_class($observer);
@ -135,6 +136,7 @@ abstract class HookEvent implements HookEventInterface
global $_hook;
if (isset($_hook[$this->eventName]) && is_array($_hook[$this->eventName])) {
foreach ($_hook[$this->eventName] as $hookObserver => $val) {
self::autoLoadHooks($hookObserver, $val['path']);
$hookObserverInstance = $hookObserver::create();
$this->observers->attach($hookObserverInstance);
}
@ -143,6 +145,7 @@ abstract class HookEvent implements HookEventInterface
$_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) {
self::autoLoadHooks($hookObserver, $val['path']);
$hookObserverInstance = $hookObserver::create();
$this->observers->attach($hookObserverInstance);
}
@ -180,11 +183,21 @@ abstract class HookEvent implements HookEventInterface
$isActive = false;
$appPlugin = new AppPlugin();
$pluginList = $appPlugin->getInstalledPluginListName();
var_dump($pluginList);
if (in_array(HOOK_MANAGEMENT_PLUGIN, $pluginList)) {
$isActive = true;
}
return $isActive;
}
/**
* Hook Auto Loader. Search for Hook Observers from plugins
* @param string $observerClass
* @param string $path
* @return int
*/
public static function autoLoadHooks($observerClass, $path)
{
Autoload::$map[$observerClass] = $path;
}
}

@ -4,8 +4,28 @@
/**
* Interface HookEventInterface
*/
interface HookEventInterface extends SplSubject
interface HookEventInterface
{
/**
* Attach an HookObserver
* @link http://php.net/manual/en/splsubject.attach.php
* @param \HookObserverInterface| $observer <p>
* The <b>HookObserver</b> to attach.
* </p>
* @return void
*/
public function attach(HookObserverInterface $observer);
/**
* Detach an HookObserver
* @link http://php.net/manual/en/splsubject.detach.php
* @param \HookObserverInterface| $observer <p>
* The <b>HookObserver</b> to detach.
* </p>
* @return void
*/
public function detach(HookObserverInterface $observer);
/**
* Return the singleton instance of Hook event.
* If Hook Management plugin is not enabled, will return NULL
@ -59,11 +79,36 @@ interface HookEventInterface extends SplSubject
* @return boolean
*/
public static function isHookPluginActive();
/**
* Hook Auto Loader. Search for Hook Observers from plugins
* @param string $observerClass
* @param string $path
* @return int
*/
public static function autoLoadHooks($observerClass, $path);
}
interface HookObserverInterface extends SplObserver
interface HookObserverInterface
{
/**
* Return the singleton instance of Hook observer.
* If Hook Management plugin is not enabled, will return NULL
* @return HookEventInterface|null
*/
public static function create();
/**
* Return the path from the class, needed to store location or autoload later.
* @return string
*/
public function getPath();
/**
* Return the plugin name where is the Hook Observer.
* @return string
*/
public function getPluginName();
}
interface HookManagementInterface
@ -131,6 +176,24 @@ interface HookManagementInterface
public function getHookCallId($eventName, $observerClassName, $type);
}
/**
* Interface HookPluginInterface
*/
interface HookPluginInterface
{
/**
* This method will call the Hook management insertHook to add Hook observer from this plugin
* @return int
*/
public function installHook();
/**
* This method will call the Hook management deleteHook to disable Hook observer from this plugin
* @return int
*/
public function uninstallHook();
}
/**
* Interface HookCreateUserEventInterface
*/

@ -2,20 +2,58 @@
/* For licensing terms, see /license.txt */
class HookObserver implements HookObserverInterface
abstract class HookObserver implements HookObserverInterface
{
public $path;
public $pluginName;
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Receive update from subject
* @link http://php.net/manual/en/splobserver.update.php
* @param SplSubject $subject <p>
* The <b>SplSubject</b> notifying the observer of an update.
* </p>
* @return void
* Construct method
* @param string $path
* @param string $pluginName
*/
public function update(SplSubject $subject)
protected function __construct($path, $pluginName)
{
// TODO: Implement update() method.
$this->path = $path;
$this->pluginName = $pluginName;
}
/**
* Return the singleton instance of Hook observer.
* 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;
}
}
}
/**
* Return the path from the class, needed to store location or autoload later.
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Return the plugin name where is the Hook Observer.
* @return string
*/
public function getPluginName()
{
return $this->pluginName;
}
}

@ -6,9 +6,6 @@
* @package chamilo.plugin.hookmanagement
*/
define('TABLE_PLUGIN_HOOK_OBSERVER', 'plugin_hook_observer');
define('TABLE_PLUGIN_HOOK_CALL', 'plugin_hook_call');
define('TABLE_PLUGIN_HOOK_EVENT', 'plugin_hook_event');
require_once __DIR__ . '/../../main/inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH) . 'plugin.class.php';
require_once api_get_path(PLUGIN_PATH) . 'hookmanagement/src/HookManagementPlugin.class.php';

@ -67,6 +67,7 @@ class HookManagementPlugin extends Plugin implements HookManagementInterface
$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . '( ' .
'id int UNSIGNED NOT NULL AUTO_INCREMENT, ' .
'class_name varchar(255) UNIQUE, ' .
'path varchar(255) NOT NULL, ' .
'plugin_name varchar(255) NULL, ' .
'PRIMARY KEY PK_hook_management_hook_observer (id) ' .
'); ';
@ -157,8 +158,11 @@ class HookManagementPlugin extends Plugin implements HookManagementInterface
// Insert hook observer
foreach ($hookObservers as $hookObserver => $v) {
$object = $hookObserver::create();
$attributes = array(
'class_name' => $hookObserver,
'path' => $object->getPath(),
'plugin_name' => $object->getPluginName(),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_OBSERVER], $attributes);
// store hook observer into property
@ -310,12 +314,12 @@ class HookManagementPlugin extends Plugin implements HookManagementInterface
' ON hc.hook_event_id = he.id ' .
' INNER JOIN ' . $this->tables[TABLE_PLUGIN_HOOK_OBSERVER] . ' ho ' .
' ON hc.hook_observer_id = ho.id ';
$columns = 'ho.class_name, hc.enabled';
$columns = 'ho.class_name, ho.path, ho.plugin_name, hc.enabled';
$where = array('where' => array('he.class_name = ? ' => $eventName, 'AND hc.enabled = ? ' => 1));
$rows = Database::select($columns, $joinTable, $where);
foreach ($rows as $row) {
$array[$row['class_name']] = $row['enabled'];
$array[$row['class_name']] = $row;
}
return $array;
@ -336,16 +340,19 @@ class HookManagementPlugin extends Plugin implements HookManagementInterface
'class_name' => $eventName,
'description' => get_plugin_lang('HookDescription' . $eventName, 'HookManagementPlugin'),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_EVENT], $attributes);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_EVENT], $attributes, true);
$this->hookEvents[$eventName] = $id;
}
// Check if exists hook observer
if (isset($observerClassName) && !isset($this->hookObservers[$observerClassName])){
$object = $observerClassName::create();
$attributes = array(
'class_name' => $observerClassName,
'path' => $object->getPath(),
'plugin_name' => $object->getPluginName(),
);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_OBSERVER], $attributes);
$id = Database::insert($this->tables[TABLE_PLUGIN_HOOK_OBSERVER], $attributes, true);
$this->hookObservers[$observerClassName] = $id;
}

Loading…
Cancel
Save