/** * @param $identifier * @param array $parameters * @param null $context * @throws MacroException * @return mixed */ public function runMacro($identifier, array $parameters = [], $context = null, array $options = []) { $options = Hash::merge(['validate' => false], $options); if (Plugin::loaded('DebugKit')) { DebugTimer::start(__d('macro', 'Macro: {0}', $identifier)); } $macroParts = explode('::', $identifier); $name = $macroParts[0]; $method = isset($macroParts[1]) ? $macroParts[1] : 'run'; $this->getMacroRegistry()->reset(); $config = []; /** @var Macro $macro */ try { $macro = $this->getMacroRegistry()->load($name, $config); if ($context) { $macro->context($context); } } catch (MacroException $missing) { if (!$options['validate']) { throw $missing; } return $missing; } $callable = [$macro, $method]; if (!is_callable($callable)) { $exception = new MissingMacroMethodException('Unknown method \'' . $method . '\' in macro ' . get_class($macro)); if (!$options['validate']) { throw $exception; } return $exception; } $result = call_user_func_array($callable, $parameters); $elapsedTime = null; if (Plugin::loaded('DebugKit')) { $elapsedTime = DebugTimer::elapsedTime(__d('macro', 'Macro: {0}', $identifier), 10) * 1000; DebugTimer::stop(__d('macro', 'Macro: {0}', $identifier)); } DebugMacro::record($identifier, $parameters, $context, $options, $result, $elapsedTime); if ($options['validate']) { return true; } return $result; }
public function data() { return ['runs' => DebugMacro::runs()]; }