isInitialized()) { $this->onAddMacros(); } $count = preg_match_all($this->getPattern(), $text, $matches); if ($count !== false) { for ($i = 0; $i < $count; $i++) { $this->substituteMacro($matches[0][$i], $matches[1][$i], $text); } } return $text; } function dump () { echo '

Macrophage dump

'; foreach ($this->macros as $name => $value) { echo '

', $name, ' = ', $value, '

';; } } protected function getPattern () { return '/%([\w\-]+)%/'; } private function substituteMacro ($string, $macro, &$text) { if ($this->hasMacro($macro)) { $text = str_replace($string, $this->getMacro($macro), $text); } else { $text = str_replace ($string, '', $text); } } function addMacro ($macro, $body) { $this->macros[$macro] = $body; return $this; } function hasMacro ($macro) { return array_key_exists($macro, $this->macros); } function getMacros () { return $this->macros; } function getMacro ($macro) { $value = $this->macros[$macro]; if ($value instanceof Func) { $value = $value->run(); } return $value; } function exportMacroses ($macrophage) { if (!($macrophage instanceof Macrophage)) { $macrophage = $macrophage->getMacrophage(); } foreach ($this->getMacros() as $name => $value) { $macrophage->addMacro($name, $value); } } private function isInitialized () { return $this->initialized; } function onAddMacros () { } }