/** * @param string $syntax * @param array $data * @throws NoMatcherFoundException * @return array */ public function getMatcherForSyntax($syntax, array $data = array()) { ArgumentChecker::check($syntax, 'string'); $rawSyntax = $this->getRawSyntax($syntax); $endsWith = ' on error ?'; if ($this->endsWith($rawSyntax, $endsWith)) { $rawSyntax = substr($rawSyntax, 0, strlen($rawSyntax) - strlen($endsWith)); } foreach ($this->modules as $module) { $reflectionClass = new ReflectionClass($module); foreach ($reflectionClass->getMethods() as $method) { $doc = $method->getDocComment(); foreach (explode("\n", $doc) as $line) { $pos = strpos($line, '@syntax'); if ($pos !== false) { $s = new Syntax(trim(substr($line, $pos + 7)), $method->getDeclaringClass()->getName() . '::' . $method->getName()); if ($s->getRawSyntax() == $rawSyntax) { $class = $s->getClass(); $r = array('matcher' => new $class(), 'originalSyntax' => $s->getSyntax()); if ($this->endsWith($this->getRawSyntax($syntax), $endsWith)) { $r['on_error'] = $data[count($data) - 1]; } return $r; } } } } } throw new NoMatcherFoundException(array($syntax)); }
public function add(Syntax $syntax) { if (array_key_exists($syntax->getRawSyntax(), $this->syntaxes)) { throw new Exception("Syntax '" . $syntax->getSyntax() . "' already registered."); } foreach ($this->syntaxes as $s) { $s1 = $s->getRawSyntax(); $s2 = $syntax->getRawSyntax(); if ($this->isSubSyntax($s1, $s2) || $this->isSubSyntax($s2, $s1)) { throw new Exception("Syntax '{$s1}' conflicts with '{$s2}'."); } } $this->syntaxes[$syntax->getRawSyntax()] = $syntax; }