Skip to content
Browse files

refactored formatter initialization routine

  • Loading branch information...
1 parent 5653530 commit 922c2637825a50f2199f1207aa3267a5d55d0b45 @everzet everzet committed May 1, 2012
View
14 src/Behat/Behat/Console/BehatApplication.php
@@ -12,6 +12,7 @@
use Behat\Behat\DependencyInjection\BehatExtension,
Behat\Behat\DependencyInjection\Compiler\GherkinPass,
+ Behat\Behat\DependencyInjection\Compiler\FormattersPass,
Behat\Behat\DependencyInjection\Compiler\ContextReaderPass,
Behat\Behat\DependencyInjection\Compiler\EventDispatcherPass,
Behat\Behat\DependencyInjection\Compiler\CommandProcessorsPass,
@@ -71,6 +72,13 @@ public function doRun(InputInterface $input, OutputInterface $output)
$this->configureContainer($container, $input);
$this->loadExtensions($container);
+ // add core compiler passes
+ $container->addCompilerPass(new GherkinPass());
+ $container->addCompilerPass(new CommandProcessorsPass());
+ $container->addCompilerPass(new FormattersPass());
+ $container->addCompilerPass(new ContextReaderPass());
+ $container->addCompilerPass(new EventDispatcherPass());
+
// compile and freeze container
$container->compile();
@@ -144,12 +152,6 @@ protected function configureContainer(ContainerInterface $container, InputInterf
'BEHAT_CONFIG_PATH', dirname($configFile)
));
}
-
- // add compiler passes
- $container->addCompilerPass(new GherkinPass());
- $container->addCompilerPass(new ContextReaderPass());
- $container->addCompilerPass(new CommandProcessorsPass());
- $container->addCompilerPass(new EventDispatcherPass());
}
/**
View
13 src/Behat/Behat/Console/Processor/FormatProcessor.php
@@ -8,6 +8,8 @@
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface;
+use Behat\Behat\Formatter\FormatterDispatcher;
+
/*
* This file is part of the Behat.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
@@ -40,17 +42,16 @@ public function __construct(ContainerInterface $container)
*/
public function configure(Command $command)
{
- $defaultFormatters = $this->container->get('behat.format_manager')->getFormatterClasses();
+ $formatDispatchers = $this->container->get('behat.format_manager')->getDispatchers();
$command
->addOption('--format', '-f', InputOption::VALUE_REQUIRED,
"How to format features. <comment>pretty</comment> is default.\n" .
"Default formatters are:\n" .
implode("\n",
- array_map(function($name) use($defaultFormatters) {
- $class = $defaultFormatters[$name];
- return "- <comment>$name</comment>: " . $class::getDescription();
- }, array_keys($defaultFormatters))
+ array_map(function($dispatcher) {
+ return '- <comment>'.$dispatcher->getName().'</comment>: '.$dispatcher->getDescription();
+ }, $formatDispatchers)
) . "\n" .
"Can use multiple formats at once (splitted with \"<comment>,</comment>\")"
)
@@ -129,7 +130,7 @@ public function process(InputInterface $input, OutputInterface $output)
// add user-defined formatter classes to manager
foreach ($this->container->getParameter('behat.formatter.classes') as $name => $class) {
- $manager->setFormatterClass($name, $class);
+ $manager->addDispatcher(new FormatterDispatcher($name, $class));
}
// init specified for run formatters
View
41 src/Behat/Behat/DependencyInjection/Compiler/FormattersPass.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Behat\Behat\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference,
+ Symfony\Component\DependencyInjection\ContainerBuilder,
+ Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/*
+ * This file is part of the Behat.
+ *
+ * (c) Konstantin Kudryashov <ever.zet@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Formatters pass - registers all available formatters.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+class FormattersPass implements CompilerPassInterface
+{
+ /**
+ * Processes container.
+ *
+ * @param Symfony\Component\DependencyInjection\ContainerBuilder $container
+ */
+ public function process(ContainerBuilder $container)
+ {
+ if (!$container->hasDefinition('behat.format_manager')) {
+ return;
+ }
+ $manager = $container->getDefinition('behat.format_manager');
+
+ foreach ($container->findTaggedServiceIds('behat.formatter_dispatcher') as $id => $attributes) {
+ $manager->addMethodCall('addDispatcher', array(new Reference($id)));
+ }
+ }
+}
View
37 src/Behat/Behat/DependencyInjection/config/behat.xml
@@ -15,6 +15,7 @@
<parameter key="behat.formatter.name">pretty</parameter>
<parameter key="behat.formatter.parameters" type="collection"></parameter>
<parameter key="behat.formatter.classes" type="collection"></parameter>
+ <parameter key="behat.formatter_dispatcher.class">Behat\Behat\Formatter\FormatterDispatcher</parameter>
<parameter key="behat.format_manager.class">Behat\Behat\Formatter\FormatManager</parameter>
<!-- Options -->
@@ -228,6 +229,42 @@
<argument type="service" id="behat.translator" />
<argument type="service" id="behat.event_dispatcher" />
</service>
+ <service id="behat.formatter_dispatcher.pretty" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\PrettyFormatter</argument>
+ <argument>pretty</argument>
+ <argument>Prints the feature as is.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
+ <service id="behat.formatter_dispatcher.progress" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\ProgressFormatter</argument>
+ <argument>progress</argument>
+ <argument>Prints one character per step.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
+ <service id="behat.formatter_dispatcher.html" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\HtmlFormatter</argument>
+ <argument>html</argument>
+ <argument>Generates a nice looking HTML report.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
+ <service id="behat.formatter_dispatcher.junit" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\JUnitFormatter</argument>
+ <argument>junit</argument>
+ <argument>Generates a report similar to Ant+JUnit.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
+ <service id="behat.formatter_dispatcher.failed" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\FailedScenariosFormatter</argument>
+ <argument>failed</argument>
+ <argument>Prints list of failed scenarios.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
+ <service id="behat.formatter_dispatcher.snippets" class="%behat.formatter_dispatcher.class%">
+ <argument>Behat\Behat\Formatter\SnippetsFormatter</argument>
+ <argument>snippets</argument>
+ <argument>Prints only snippets for undefined steps.</argument>
+ <tag name="behat.formatter_dispatcher" />
+ </service>
<!-- PathLocator -->
<service id="behat.path_locator" class="%behat.path_locator.class%">
View
8 src/Behat/Behat/Formatter/FailedScenariosFormatter.php
@@ -26,14 +26,6 @@ class FailedScenariosFormatter extends ConsoleFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return "Prints list of failed scenarios.";
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array();
View
84 src/Behat/Behat/Formatter/FormatManager.php
@@ -20,76 +20,41 @@
*/
class FormatManager
{
- /**
- * Translator service.
- *
- * @var Symfony\Component\Translation\Translator
- */
private $translator;
- /**
- * Event dispatcher.
- *
- * @var Symfony\Component\EventDispatcher\EventDispatcher
- */
- private $dispatcher;
- /**
- * List of initialized formatters.
- *
- * @var array
- */
- private $formatters = array();
- /**
- * Default formatters.
- */
- private static $defaultFormatterClasses = array(
- 'pretty' => 'Behat\Behat\Formatter\PrettyFormatter',
- 'progress' => 'Behat\Behat\Formatter\ProgressFormatter',
- 'html' => 'Behat\Behat\Formatter\HtmlFormatter',
- 'junit' => 'Behat\Behat\Formatter\JUnitFormatter',
- 'failed' => 'Behat\Behat\Formatter\FailedScenariosFormatter',
- 'snippets' => 'Behat\Behat\Formatter\SnippetsFormatter',
- );
- /**
- * Formatter classes.
- *
- * @var array
- */
- private $formatterClasses = array();
+ private $eventDispatcher;
+ private $dispatchers = array();
+ private $formatters = array();
/**
* Initializes format manager.
*
- * @param Symfony\Component\Translation\Translator $translator
- * @param Symfony\Component\EventDispatcher\EventDispatcher $dispatcher
+ * @param Translator $translator
+ * @param EventDispatcher $eventDispatcher
*/
- public function __construct(Translator $translator, EventDispatcher $dispatcher)
+ public function __construct(Translator $translator, EventDispatcher $eventDispatcher)
{
- $this->translator = $translator;
- $this->dispatcher = $dispatcher;
-
- $this->formatterClasses = self::$defaultFormatterClasses;
+ $this->translator = $translator;
+ $this->eventDispatcher = $eventDispatcher;
}
/**
- * Returns all currently available formats with their classes.
+ * Adds formatter dispatcher to the manager.
*
- * @return array
+ * @param FormatterDispatcher $dispatcher Formatter dispatcher
*/
- public function getFormatterClasses()
+ public function addDispatcher(FormatterDispatcher $dispatcher)
{
- return $this->formatterClasses;
+ $this->dispatchers[$dispatcher->getName()] = $dispatcher;
}
/**
- * Sets formatter class to specific format.
+ * Returns registered formatter dispatchers.
*
- * @param string $name format name
- * @param string $class formatter class
+ * @return array
*/
- public function setFormatterClass($name, $class)
+ public function getDispatchers()
{
- $name = strtolower($name);
- $this->formatterClasses[$name] = $class;
+ return $this->dispatchers;
}
/**
@@ -102,25 +67,18 @@ public function initFormatter($name)
$name = strtolower($name);
if (class_exists($name)) {
- $class = $name;
- } elseif (isset($this->formatterClasses[$name])) {
- $class = $this->formatterClasses[$name];
+ $dispatcher = new FormatterDispatcher($name);
+ } elseif (isset($this->dispatchers[$name])) {
+ $dispatcher = $this->dispatchers[$name];
} else {
throw new \RuntimeException("Unknown formatter: \"$name\". " .
'Available formatters are: ' . implode(', ', array_keys($this->formatterClasses))
);
}
- $refClass = new \ReflectionClass($class);
- if (!$refClass->implementsInterface('Behat\Behat\Formatter\FormatterInterface')) {
- throw new \RuntimeException(sprintf(
- 'Formatter class "%s" should implement FormatterInterface', $class
- ));
- }
-
- $formatter = new $class();
+ $formatter = $dispatcher->createFormatter();
$formatter->setTranslator($this->translator);
- $this->dispatcher->addSubscriber($formatter, -5);
+ $this->eventDispatcher->addSubscriber($formatter, -5);
return $this->formatters[] = $formatter;
}
View
89 src/Behat/Behat/Formatter/FormatterDispatcher.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Behat\Behat\Formatter;
+
+use Symfony\Component\Translation\Translator,
+ Symfony\Component\EventDispatcher\EventDispatcher;
+
+/*
+ * This file is part of the Behat.
+ * (c) Konstantin Kudryashov <ever.zet@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Formatter dispatcher.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+class FormatterDispatcher
+{
+ private $class;
+ private $name;
+ private $description;
+
+ /**
+ * Initializes formatter dispatcher.
+ *
+ * @param string $class Formatter class
+ * @param string $name Name of the formatter
+ * @param string $description Formatter description
+ */
+ public function __construct($class, $name = null, $description = null)
+ {
+ $refClass = new \ReflectionClass($class);
+ if (!$refClass->implementsInterface('Behat\Behat\Formatter\FormatterInterface')) {
+ throw new \RuntimeException(sprintf(
+ 'Formatter class "%s" should implement FormatterInterface', $class
+ ));
+ }
+
+ $this->class = $class;
+ $this->name = null !== $name ? strtolower($name) : null;
+ $this->description = $description;
+ }
+
+ /**
+ * Returns formatter name.
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Returns formatter description.
+ *
+ * @return string
+ */
+ public function getDescription()
+ {
+ return $this->description;
+ }
+
+ /**
+ * Returns formatter class.
+ *
+ * @return string
+ */
+ public function getClass()
+ {
+ return $this->class;
+ }
+
+ /**
+ * Initializes formatter instance.
+ *
+ * @return FormatterInterface
+ */
+ public function createFormatter()
+ {
+ $class = $this->class;
+
+ return new $class();
+ }
+}
View
7 src/Behat/Behat/Formatter/FormatterInterface.php
@@ -23,13 +23,6 @@
interface FormatterInterface extends EventSubscriberInterface
{
/**
- * Returns formatter description (printed in Behat help).
- *
- * @return string
- */
- static function getDescription();
-
- /**
* Set formatter translator.
*
* @param Symfony\Component\Translation\Translator $translator
View
10 src/Behat/Behat/Formatter/HtmlFormatter.php
@@ -42,14 +42,6 @@ class HtmlFormatter extends PrettyFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return "Generates a nice looking HTML report.";
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array(
@@ -473,7 +465,7 @@ protected function printColorizedTableRow($row, $color)
*/
protected function printPathLink(DefinitionInterface $definition)
{
- $url = $this->getParameter('paths_base_url')
+ $url = $this->getParameter('paths_base_url')
. $this->relativizePathsInString($definition->getCallbackReflection()->getFileName());
$path = $this->relativizePathsInString($definition->getPath());
$this->writeln('<span class="path"><a href="' . $url . '">' . $path . '</a></span>');
View
8 src/Behat/Behat/Formatter/JUnitFormatter.php
@@ -76,14 +76,6 @@ class JUnitFormatter extends ConsoleFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return "Generates a report similar to Ant+JUnit.";
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array();
View
8 src/Behat/Behat/Formatter/PrettyFormatter.php
@@ -100,14 +100,6 @@ class PrettyFormatter extends ProgressFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return 'Prints the feature as is.';
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array();
View
8 src/Behat/Behat/Formatter/ProgressFormatter.php
@@ -39,14 +39,6 @@ class ProgressFormatter extends ConsoleFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return "Prints one character per step.";
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array();
View
8 src/Behat/Behat/Formatter/SnippetsFormatter.php
@@ -24,14 +24,6 @@ class SnippetsFormatter extends ProgressFormatter
/**
* {@inheritdoc}
*/
- public static function getDescription()
- {
- return "Prints only snippets for undefined steps.";
- }
-
- /**
- * {@inheritdoc}
- */
protected function getDefaultParameters()
{
return array();

0 comments on commit 922c263

Please sign in to comment.
Something went wrong with that request. Please try again.