X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FSnippet%2FSnippetRegistry.php;fp=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FSnippet%2FSnippetRegistry.php;h=0724c3e4037e0a19c79dfd1220044f81898d04b2;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hp=0000000000000000000000000000000000000000;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68;p=yaffs-website diff --git a/vendor/behat/behat/src/Behat/Behat/Snippet/SnippetRegistry.php b/vendor/behat/behat/src/Behat/Behat/Snippet/SnippetRegistry.php new file mode 100644 index 000000000..0724c3e40 --- /dev/null +++ b/vendor/behat/behat/src/Behat/Behat/Snippet/SnippetRegistry.php @@ -0,0 +1,143 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Behat\Behat\Snippet; + +use Behat\Behat\Snippet\Generator\SnippetGenerator; +use Behat\Gherkin\Node\StepNode; +use Behat\Testwork\Environment\Environment; + +/** + * Acts like a snippet repository by producing snippets from registered undefined steps using snippet generators. + * + * @author Konstantin Kudryashov + */ +final class SnippetRegistry implements SnippetRepository +{ + /** + * @var SnippetGenerator[] + */ + private $generators = array(); + /** + * @var UndefinedStep[] + */ + private $undefinedSteps = array(); + /** + * @var AggregateSnippet[] + */ + private $snippets = array(); + /** + * @var Boolean + */ + private $snippetsGenerated = false; + + /** + * Registers snippet generator. + * + * @param SnippetGenerator $generator + */ + public function registerSnippetGenerator(SnippetGenerator $generator) + { + $this->generators[] = $generator; + $this->snippetsGenerated = false; + } + + /** + * Generates and registers snippet. + * + * @param Environment $environment + * @param StepNode $step + * + * @return null|Snippet + */ + public function registerUndefinedStep(Environment $environment, StepNode $step) + { + $this->undefinedSteps[] = new UndefinedStep($environment, $step); + $this->snippetsGenerated = false; + } + + /** + * Returns all generated snippets. + * + * @return AggregateSnippet[] + */ + public function getSnippets() + { + $this->generateSnippets(); + + return $this->snippets; + } + + /** + * Returns steps for which there was no snippet generated. + * + * @return UndefinedStep[] + */ + public function getUndefinedSteps() + { + $this->generateSnippets(); + + return $this->undefinedSteps; + } + + /** + * Generates snippets for undefined steps. + */ + private function generateSnippets() + { + if ($this->snippetsGenerated) { + return null; + } + + $snippetsSet = array(); + foreach ($this->undefinedSteps as $i => $undefinedStep) { + $snippet = $this->generateSnippet($undefinedStep->getEnvironment(), $undefinedStep->getStep()); + + if (!$snippet) { + continue; + } + + if (!isset($snippetsSet[$snippet->getHash()])) { + $snippetsSet[$snippet->getHash()] = array(); + } + + $snippetsSet[$snippet->getHash()][] = $snippet; + unset($this->undefinedSteps[$i]); + } + + $this->snippets = array_values( + array_map( + function (array $snippets) { + return new AggregateSnippet($snippets); + }, + $snippetsSet + ) + ); + $this->undefinedSteps = array_values($this->undefinedSteps); + $this->snippetsGenerated = true; + } + + /** + * @param Environment $environment + * @param StepNode $step + * + * @return null|Snippet + */ + private function generateSnippet(Environment $environment, StepNode $step) + { + foreach ($this->generators as $generator) { + if ($generator->supportsEnvironmentAndStep($environment, $step)) { + return $generator->generateSnippet($environment, $step); + } + } + + return null; + } +}