1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160:
<?php
namespace Peridot\Reporter;
use Evenement\EventEmitterInterface;
use Peridot\Configuration;
use Peridot\Core\HasEventEmitterTrait;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
class ReporterFactory
{
use HasEventEmitterTrait;
protected $configuration;
protected $runner;
protected $output;
protected $reporters = array(
'spec' => ['description' => 'hierarchical spec list', 'factory' => 'Peridot\Reporter\SpecReporter']
);
public function __construct(
Configuration $configuration,
OutputInterface $output,
EventEmitterInterface $eventEmitter
) {
$this->configuration = $configuration;
$this->output = $output;
$this->eventEmitter = $eventEmitter;
}
public function create($name)
{
return $this->createWithOutput($this->output, $name);
}
public function createComposite(array $names)
{
if (empty($names)) {
throw new \InvalidArgumentException('Reporter names cannot be empty.');
}
return new CompositeReporter(
array_merge(
[$this->createWithOutput($this->output, array_shift($names))],
array_map(function ($name) {
return $this->createWithOutput(new BufferedOutput(), $name);
}, $names)
),
$this->configuration,
$this->output,
$this->eventEmitter
);
}
public function getReporterFactory($name)
{
$definition = $this->getReporterDefinition($name);
if (! isset($definition['factory'])) {
$definition['factory'] = null;
}
return $definition['factory'];
}
public function getReporterDefinition($name)
{
$definition = [];
if (isset($this->reporters[$name])) {
$definition = $this->reporters[$name];
}
return $definition;
}
public function register($name, $description, $factory)
{
$this->reporters[$name] = ['description' => $description, 'factory' => $factory];
}
public function getReporters()
{
return $this->reporters;
}
private function createWithOutput(OutputInterface $output, $name)
{
$factory = $this->getReporterFactory($name);
$isClass = is_string($factory) && class_exists($factory);
if ($isClass) {
return new $factory($this->configuration, $output, $this->eventEmitter);
}
if (is_callable($factory)) {
return new AnonymousReporter($factory, $this->configuration, $output, $this->eventEmitter);
}
throw new \RuntimeException("Reporter class could not be created");
}
}