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 support for formatters FQCN instead of instances
  • Loading branch information
HeahDude committed Apr 23, 2016
commit 8f63b0786164a7f456d8b6ba5ffd10b9d69c8796
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ abstract class AbstractValueExporter implements ValueExporterInterface
protected $formatterInterface = FormatterInterface::class;

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

/**
* A sorted array of formatters.
* An array of formatters instances sorted by priority or null.
*
* @var FormatterInterface[]
* @var FormatterInterface[]|null
*/
private $sortedFormatters;

/**
* An array of cached formatters instances by class.
*
* @var FormatterInterface[]
*/
private $cachedFormatters = array();

/**
* Takes {@link FormatterInterface} as arguments.
*
Expand All @@ -72,23 +79,19 @@ final public function addFormatters(array $formatters)
foreach ($formatters as $formatter) {
if (is_array($formatter)) {
$priority = (int) $formatter[1];
$formatter = $formatter[0];
$formatterClass = $formatter[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with the list thing if you decide to change it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, my first draw used list. I end up using that notation because I find it more readable, especially when casting to int :)
Since the priority in the value used to sort with arsort I want to be sure some '10' string doesn't mess with the order, do you think that's only paranoia 😨?

} 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;
$formatterClass = $formatter;
}

$formatterClass = get_class($formatter);

if (!$formatter instanceof $this->formatterInterface) {
if (!in_array($this->formatterInterface, class_implements($formatterClass), true)) {
throw new InvalidFormatterException($formatterClass, stat 8000 ic::class, $this->formatterInterface);
}

if (in_array(ExpandedFormatterTrait::class, class_uses($formatterClass), true)) {
$formatter->setExporter($this);
}

// Using the class as key prevents duplicate
$this->formatters[$priority][$formatterClass] = $formatter;
// Using the class as key prevents duplicate and allows to
// dynamically change the priority
$this->formatters[$formatterClass] = $priority;
}
}

Expand All @@ -98,8 +101,24 @@ final public function addFormatters(array $formatters)
final protected function formatters()
{
if (null === $this->sortedFormatters) {
krsort($this->formatters);
$this->sortedFormatters = call_user_func_array('array_merge', $this->formatters);
arsort($this->formatters);

foreach (array_keys($this->formatters) as $formatterClass) {
if (isset($this->cachedFormatters[$formatterClass])) {
$this->sortedFormatters[] = $this->cachedFormatters[$formatterClass];

continue;
}

$formatter = new $formatterClass();

if (in_array(ExpandedFormatterTrait::class, class_uses($formatterClass), true)) {
/* @var ExpandedFormatterTrait $formatter */
$formatter->setExporter($this);
}

$this->sortedFormatters[] = $this->cachedFormatters[$formatterClass] = $formatter;
}
}

return $this->sortedFormatters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testExportValueExpanded()

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

$value = new TraversableInstance();
$exportedValue = <<<EOT
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/ValueExporter/ValueExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public static function export($value, $depth = 1, $expand = false)
{
if (null === self::$handler) {
$exporter = self::$exporter ?: new ValueToStringExporter(
new CallableToStringFormatter(),
new DateTimeToStringFormatter(),
new EntityToStringFormatter(),
new PhpIncompleteClassToStringFormatter()
CallableToStringFormatter::class,
DateTimeToStringFormatter::class,
EntityToStringFormatter::class,
PhpIncompleteClassToStringFormatter::class
);
$exporter->addFormatters(self::$formatters);
// Clear formatters
Expand Down
0