/** * Adds definition to dispatcher. * * @param DefinitionInterface $definition * * @throws RedundantException */ public function addDefinition(DefinitionInterface $definition) { $regex = $definition->getRegex(); if (isset($this->definitions[$regex])) { throw new RedundantException($definition, $this->definitions[$regex]); } $this->definitions[$regex] = $definition; }
/** * Returns step text with colorized arguments. * * @param string $text * @param DefinitionInterface $definition * @param string $color * * @return string */ protected function colorizeDefinitionArguments($text, DefinitionInterface $definition, $color) { $regex = $definition->getRegex(); $paramColor = $color . '_param'; // If it's just a string - skip if ('/' !== substr($regex, 0, 1)) { return $text; } // Find arguments with offsets $matches = array(); preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE); array_shift($matches); // Replace arguments with colorized ones $shift = 0; $lastReplacementPosition = 0; foreach ($matches as $key => $match) { if (!is_numeric($key) || -1 === $match[1] || false !== strpos($match[0], '<')) { continue; } $offset = $match[1] + $shift; $value = $match[0]; // Skip inner matches if ($lastReplacementPosition > $offset) { continue; } $lastReplacementPosition = $offset + strlen($value); $begin = substr($text, 0, $offset); $end = substr($text, $lastReplacementPosition); $format = "{-{$color}}{+{$paramColor}}%s{-{$paramColor}}{+{$color}}"; $text = sprintf("%s{$format}%s", $begin, $value, $end); // Keep track of how many extra characters are added $shift += strlen($format) - 2; $lastReplacementPosition += strlen($format) - 2; } // Replace "<", ">" with colorized ones $text = preg_replace('/(<[^>]+>)/', "{-{$color}}{+{$paramColor}}\$1{-{$paramColor}}{+{$color}}", $text); return $text; }
/** * Initializes redundant exception. * * @param DefinitionInterface $step2 duplicate step definition * @param DefinitionInterface $step1 firstly matched step definition */ public function __construct(DefinitionInterface $step2, DefinitionInterface $step1) { $message = sprintf("Step \"%s\" is already defined in %s\n\n%s\n%s", $step2->getRegex(), $step1->getPath(), $step1->getPath(), $step2->getPath()); parent::__construct($message); }