mirror of
https://github.com/e107inc/e107.git
synced 2025-07-13 11:06:20 +02:00
Issue #5480 Event function may now be static and located in other classes.
This commit is contained in:
@ -228,8 +228,21 @@ class e107_event
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
$tmp = new $class($eventname);
|
if (strpos($method, '::') !== false) // If $method contains "::", call it statically
|
||||||
$ret = $tmp->{$method}($data, $eventname); //let callback know what event is calling it
|
{
|
||||||
|
[$staticClass, $staticMethod] = explode('::', $method, 2);
|
||||||
|
$ret = $staticClass::$staticMethod($data, $eventname);
|
||||||
|
}
|
||||||
|
elseif(is_callable([$class, $method]))
|
||||||
|
{
|
||||||
|
$ret = $class::$method($data, $eventname); // Call statically from within the same class.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$tmp = new $class($eventname);
|
||||||
|
$ret = $tmp->{$method}($data, $eventname); // Let callback know what event is calling it
|
||||||
|
}
|
||||||
|
|
||||||
unset($tmp);
|
unset($tmp);
|
||||||
if (!empty($ret))
|
if (!empty($ret))
|
||||||
{
|
{
|
||||||
@ -238,7 +251,9 @@ class e107_event
|
|||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
e107::getLog()->add('Event Trigger failed',array('name'=>$eventname,'location'=>$location,'class'=>$class,'method'=>$method,'error'=>$e),E_LOG_WARNING,'EVENT_01');
|
$logError = array('name'=>$eventname,'location'=>$location,'class'=>$class,'method'=>$method,'error'=>$e);
|
||||||
|
e107::getLog()->add('Event Trigger failed',$logError,E_LOG_WARNING,'EVENT_01');
|
||||||
|
trigger_error('Event Trigger failed: '.print_r($logError,true), E_USER_WARNING);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -252,12 +267,14 @@ class e107_event
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
e107::getLog()->add('Event Trigger failed',array('name'=>$eventname,'location'=>$location,'function'=>$evt_func), E_LOG_WARNING,'EVENT_01');
|
$logData = array('name'=>$eventname,'location'=>$location,'function'=>$evt_func);
|
||||||
|
trigger_error('Event Trigger failed: function does not exist: '.print_r($logData,true), E_USER_WARNING);
|
||||||
|
e107::getLog()->add('Event Trigger failed',$logData, E_LOG_WARNING,'EVENT_01');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (isset($ret) ? $ret : false);
|
return ($ret ?? false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -343,7 +360,9 @@ class e107_event
|
|||||||
if(is_readable(e_PLUGIN.$hook."/e_event.php"))
|
if(is_readable(e_PLUGIN.$hook."/e_event.php"))
|
||||||
{
|
{
|
||||||
require_once(e_PLUGIN.$hook."/e_event.php");
|
require_once(e_PLUGIN.$hook."/e_event.php");
|
||||||
$name = "e_event_{$hook}";
|
|
||||||
|
$name = "e_event_$hook";
|
||||||
|
|
||||||
if(class_exists($name))
|
if(class_exists($name))
|
||||||
{
|
{
|
||||||
$class = new $name();
|
$class = new $name();
|
||||||
|
@ -15,17 +15,18 @@ class _blank_event // plugin-folder + '_event'
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure functions/methods to run when specific e107 events are triggered.
|
* Configures and returns an array of events to be handled by the system.
|
||||||
|
* Each event in the array includes a unique name and the corresponding function to handle it.
|
||||||
*
|
*
|
||||||
* For a list of core events, please visit: http://e107.org/developer-manual/classes-and-methods#events
|
* The events include:
|
||||||
|
* - Core events like "login".
|
||||||
|
* - Core plugin events like "user_forum_post_created".
|
||||||
|
* - Custom events from third-party plugins such as "customplugin_customevent".
|
||||||
|
* - Plugin-specific custom events like "_blank_customevent".
|
||||||
|
* - Class-based custom event handlers such as "_blankCustomEventClass::blankMethod".
|
||||||
*
|
*
|
||||||
* Developers can trigger their own events using: e107::getEvent()->trigger('plugin_event', $array);
|
* @return array Returns an array of event configurations, where each configuration includes
|
||||||
* Where 'plugin' is the folder of their plugin and 'event' is a unique name of the event.
|
* the event name and the associated function or class method to handle the event.
|
||||||
* Other plugins can then 'listen' to this custom event by defining it in THEIR e_event.php addon within the config() method.
|
|
||||||
*
|
|
||||||
* $array is data which is sent to the triggered function. eg. myfunction($array) in the example below.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
function config()
|
function config()
|
||||||
{
|
{
|
||||||
@ -53,22 +54,50 @@ class _blank_event // plugin-folder + '_event'
|
|||||||
// Example 4: custom event of the _blank plugin.
|
// Example 4: custom event of the _blank plugin.
|
||||||
// Listen to _blank's own plugin event, this usually does not occur but is here for illustration purposes.
|
// Listen to _blank's own plugin event, this usually does not occur but is here for illustration purposes.
|
||||||
$event[] = array(
|
$event[] = array(
|
||||||
'name' => "_blank_customevent", // "plugin_event" where 'plugin' is the plugin folder name (in this case "_blank") and "event" is a unique event name (in this case "customevent")
|
'name' => "_blank_static_event", // "plugin_event" where 'plugin' is the plugin folder name (in this case "_blank") and "event" is a unique event name (in this case "customevent")
|
||||||
'function' => "anotherfunction", // ..run this function (see below).
|
'function' => "staticfunction", // ..run this function (see below).
|
||||||
|
);
|
||||||
|
|
||||||
|
// Example 5: Custom event of the _blank plugin with a separate class and static method.
|
||||||
|
|
||||||
|
$event[] = array(
|
||||||
|
'name' => "_blank_custom_class", // "plugin_event" where 'plugin' is the plugin folder name (in this case "_blank") and "event" is a unique event name (in this case "customevent")
|
||||||
|
'function' => '_blankCustomEventClass::blankMethod', // ..run this function (see below).
|
||||||
);
|
);
|
||||||
|
|
||||||
return $event;
|
return $event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function myfunction($data, $event) // the method to run.
|
public function myfunction($data, $event) // the method to run.
|
||||||
{
|
{
|
||||||
// var_dump($data);
|
// var_dump($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function anotherfunction($data, $event) // the method to run.
|
public function anotherfunction($data, $event) // the method to run.
|
||||||
{
|
{
|
||||||
// var_dump($data);
|
// var_dump($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function staticfunction($data, $event) // the method to run.
|
||||||
|
{
|
||||||
|
return 'error in event: '.$event;
|
||||||
|
}
|
||||||
|
|
||||||
} //end class
|
} //end class
|
||||||
|
|
||||||
|
|
||||||
|
class _blankCustomEventClass
|
||||||
|
{
|
||||||
|
|
||||||
|
public static function blankMethod($data, $event)
|
||||||
|
{
|
||||||
|
|
||||||
|
return "Blocking more triggers of: ".$event. " ".json_encode($data);;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,8 @@ use Codeception\Test\Unit;
|
|||||||
cd e107_tests
|
cd e107_tests
|
||||||
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit
|
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit
|
||||||
|
|
||||||
|
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit/_blank_eventTest:testMyfunction
|
||||||
|
|
||||||
OR with debug options:
|
OR with debug options:
|
||||||
|
|
||||||
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit --steps --debug
|
vendor/bin/codecept run unit ../e107_plugins/_blank/tests/unit --steps --debug
|
||||||
|
@ -15,7 +15,7 @@ class e107_eventTest extends \Codeception\Test\Unit
|
|||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
$this->fail($e->getMessage());
|
$this::fail($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -25,10 +25,41 @@ class e107_eventTest extends \Codeception\Test\Unit
|
|||||||
e107::getEvent()->trigger('user_profile_display', ['foo'=>'bar']);
|
e107::getEvent()->trigger('user_profile_display', ['foo'=>'bar']);
|
||||||
|
|
||||||
$result = e107::getEvent()->triggered('user_profile_display');
|
$result = e107::getEvent()->triggered('user_profile_display');
|
||||||
$this->assertTrue($result);
|
$this::assertTrue($result);
|
||||||
|
|
||||||
$result = e107::getEvent()->triggered('non_event');
|
$result = e107::getEvent()->triggered('non_event');
|
||||||
$this->assertFalse($result);
|
$this::assertFalse($result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTriggerClass()
|
||||||
|
{
|
||||||
|
|
||||||
|
e107::getPlugin()->install('_blank');
|
||||||
|
e107::getEvent()->init();
|
||||||
|
|
||||||
|
$result = e107::getEvent()->trigger('_blank_custom_class', ['foo'=>'bar']);
|
||||||
|
$expected = 'Blocking more triggers of: _blank_custom_class {"foo":"bar"}'; // @see e107_plugins/_blank/e_event.php
|
||||||
|
$this::assertSame($expected, $result);
|
||||||
|
|
||||||
|
e107::getPlugin()->uninstall('_blank');
|
||||||
|
e107::getEvent()->init();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTriggerStatic()
|
||||||
|
{
|
||||||
|
e107::getPlugin()->install('_blank');
|
||||||
|
e107::getEvent()->init();
|
||||||
|
|
||||||
|
$result = e107::getEvent()->trigger('_blank_static_event', ['foo'=>'bar']);
|
||||||
|
$expected = 'error in event: _blank_static_event'; // @see e107_plugins/_blank/e_event.php
|
||||||
|
$this::assertSame($expected, $result);
|
||||||
|
|
||||||
|
e107::getPlugin()->uninstall('_blank');
|
||||||
|
e107::getEvent()->init();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user