8000 [PoC] [ValueExporter] extract HttpKernel/DataCollector util to its own component by HeahDude · Pull Request #18450 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PoC] [ValueExporter] extract HttpKernel/DataCollector util to its own component #18450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ValueExporter] added priority to formatters
  • Loading branch information
HeahDude committed Apr 23, 2016
commit eb1a34d4b3e50d32c679208cc6b878d5b6b50fc8
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ abstract class AbstractValueExporter implements ValueExporterInterface
protected $formatterInterface = FormatterInterface::class;

/**
* An array of formatters.
* An array of arrays of formatters by priority.
*
* @var FormatterInterface[]
* @var array[]
*/
private $formatters = array();

/**
* A sorted array of formatters.
*
* @var FormatterInterface[]
*/
private $sortedFormatters;

/**
* Takes {@link FormatterInterface} as arguments.
*
Expand All @@ -49,25 +56,26 @@ final public function __construct()
/**
* {@inheritdoc}
*/
final public function addFormatters(array $appends, array $prepends = array())
final public function addFormatters(array $formatters)
{
foreach ($appends as $append) {
if (!$append instanceof $this->formatterInterface) {
throw new InvalidFormatterException(get_class($append), self::class, $this->formatterInterface);
}
if ($append instanceof ExpandedFormatter) {
$append->setExporter($this);
$this->sortedFormatters = null;

foreach ($formatters as $formatter) {
if (is_array($formatter)) {
$priority = (int) $formatter[1];
$formatter = $formatter[0];
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could actually do some unpacking here list($formatter, $priority) = $formatter;. I don't really think the int cast is necessary

$priority = 0;
}
$this->formatters[] = $append;
}
foreach (array_reverse($prepends) as $prepend) {
if (!$prepend instanceof $this->formatterInterface) {
throw new InvalidFormatterException(get_class($prepend), self::class, $this->formatterInterface);
if (!$formatter instanceof $this->formatterInterface) {
throw new InvalidFormatterException(get_class($formatter), self::class, $this->formatterInterface);
}
if ($prepend instanceof ExpandedFormatter) {
$prepend->setExporter($this);
if ($formatter instanceof ExpandedFormatter) {
$formatter->setExporter($this);
}
array_unshift($this->formatters, $prepend);

// Using the class as key prevents duplicate
$this->formatters[$priority][get_class($formatter)] = $formatter;
}
}

Expand All @@ -76,6 +84,11 @@ final public function addFormatters(array $appends, array $prepends = array())
*/
final protected function formatters()
{
return $this->formatters;
if (null === $this->sortedFormatters) {
krsort($this->formatters);
$this->sortedFormatters = call_user_func_array('array_merge', $this->formatters);
}

return $this->sortedFormatters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ interface ValueExporterInterface
public function exportValue($value, $depth, $expand);

/**
* Adds {@link FormatterInterface} that will be called in the given order.
* Adds {@link FormatterInterface} that will be called by priority.
*
* @param FormatterInterface[] $appends The formatters to execute at last
* @param FormatterInterface[] $prepends The formatters to execute first
* @param (FormatterInterface|array)[] $formatters
*
* @throws InvalidFormatterException If the exporter does not support a given formatter
*/
public function addFormatters(array $appends, array $prepends);
public function addFormatters(array $formatters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testExportValueExpanded()

public function testExportTraversable()
{
ValueExporter::appendFormatter(new TraversableToStringFormatter());
ValueExporter::addFormatters(array(new TraversableToStringFormatter()));

$value = new TraversableInstance();
$exportedValue = <<<EOT
Expand Down
40 changes: 20 additions & 20 deletions src/Symfony/Component/ValueExporter/ValueExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ValueExporter
{
private static $handler;
private static $exporter;
private static $appends = array();
private static $prepends = array();
private static $formatters = array();

public static function export($value, $depth = 1, $expand = false)
{
Expand All @@ -42,9 +41,9 @@ public static function export($value, $depth = 1, $expand = false)
new EntityToStringFormatter(),
new PhpIncompleteClassToStringFormatter()
);
$exporter->addFormatters(self::$appends, self::$prepends);
// Clear extra formatters
self::$appends = self::$prepends = array();
$exporter->addFormatters(self::$formatters);
// Clear formatters
self::$formatters = array();
self::$handler = function ($value, $depth = 1, $expand = false) use ($exporter) {
return $exporter->exportValue($value, $depth, $expand);
};
Expand All @@ -70,28 +69,29 @@ public static function setExporter(ValueExporterInterface $exporter)
{
self::$handler = null;
self::$exporter = $exporter;
self::$appends = self:: $prepends = array();
self::$formatters = array();
}

/**
* Appends a {@link FormatterInterface} to the {@link ValueExporterInterface}.
* Adds {@link FormatterInterface} to the {@link ValueExporterInterface}.
*
* @param FormatterInterface $formatter
*/
public static function appendFormatter(FormatterInterface $formatter)
{
self::$handler = null;
self::$appends[] = $formatter;
}

/**
* Prepends a {@link FormatterInterface} to the {@link ValueExporterInterface}.
* You can simple pass an instance or an array with the instance and the priority:
*
* @param FormatterInterface $formatter
* <code>
* ValueExporter::addFormatters(array(
* new AcmeFormatter,
* array(new AcmeOtherFormatter(), 10)
* );
* </code>
*
* @param mixed $formatters An array of FormatterInterface instances and/or
* arrays holding an instance and its priority
*/
public static function prependFormatter(FormatterInterface $formatter)
public static function addFormatters($formatters)
{
self::$handler = null;
self::$prepends[] = $formatter;
foreach ($formatters as $formatter) {
self::$formatters[] = $formatter;
}
}
}