Created
April 18, 2016 20:42
-
-
Save kanduvisla/b7dd4037f1302dcfc28bc9e64c0a4fa3 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Giel | |
* Date: 18/04/16 | |
* Time: 22:15 | |
*/ | |
namespace Gielberkers\Example\Plugin\Magento\Framework\Event; | |
class Manager | |
{ | |
private $events; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->events = []; | |
} | |
/** | |
* Destructor | |
*/ | |
public function __destruct() | |
{ | |
$data = ''; | |
foreach ($this->events as $event) { | |
$data .= sprintf(' | |
<event> | |
<name>%1$s</name> | |
<file>%4$s</file> | |
<class>%2$s</class> | |
<function>%3$s</function> | |
</event>', | |
$event['name'], | |
$event['class'], | |
$event['function'], | |
$event['file'] | |
); | |
} | |
$output = new \SimpleXMLElement('<events>' . $data . '</events>'); | |
$output->asXML('/var/www/events.xml'); | |
} | |
/** | |
* @param \Magento\Framework\Event\Manager $manager | |
* @param string $event | |
* @param array $data | |
*/ | |
public function beforeDispatch( | |
\Magento\Framework\Event\Manager $manager, string $eventName, array $data | |
) | |
{ | |
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | |
$found = false; | |
foreach ($backtrace as $idx => $bt) { | |
if (!$found) { | |
if ($bt['function'] === 'dispatch') { | |
$found = true; | |
} | |
} else { | |
if ($bt['function'] !== 'dispatch') { | |
// Gotcha: | |
$this->events[] = [ | |
'name' => $eventName, | |
'file' => isset($backtrace[$idx]['file']) ? $backtrace[$idx]['file'] . ':' . $backtrace[$idx]['line'] : '', | |
'function' => $backtrace[$idx]['function'], | |
'class' => isset($backtrace[$idx]['class']) ? $backtrace[$idx]['class'] : '' | |
]; | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment