Skip to content

Commit

Permalink
Fixing DataCollectors, removing serialization of FamilyRegistry and A…
Browse files Browse the repository at this point in the history
…ttributeTypeRegistry and using arrays of scalar data instead.
  • Loading branch information
VincentChalnot committed Jun 10, 2020
1 parent 5ac49b6 commit 387baf6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 31 deletions.
5 changes: 5 additions & 0 deletions Model/FamilyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ interface FamilyInterface
*/
public function getCode();

/**
* @return string
*/
public function getLabel();

/**
* @return string
*/
Expand Down
25 changes: 23 additions & 2 deletions Profiler/DataLoaderCollector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php declare(strict_types=1);
<?php
/*
* This file is part of the Sidus/EAVModelBundle package.
*
* Copyright (c) 2015-2019 Vincent Chalnot
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sidus\EAVModelBundle\Profiler;

Expand All @@ -9,7 +17,9 @@
use Symfony\Component\VarDumper\Cloner\Data;

/**
* @method reset()
* Collect EAV Data loaded by the EAVDataLoader
*
* @author Vincent Chalnot <vincent@sidus.fr>
*/
class DataLoaderCollector extends DataCollector
{
Expand Down Expand Up @@ -53,6 +63,17 @@ public function collect(Request $request, Response $response, \Exception $except
}
}

/**
* {@inheritdoc}
*/
public function reset()
{
$this->data = [
'nodes' => [],
'count' => null,
];
}

/**
* @return int
*/
Expand Down
118 changes: 103 additions & 15 deletions Profiler/ModelConfigurationDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

namespace Sidus\EAVModelBundle\Profiler;

use Sidus\EAVModelBundle\Model\AttributeInterface;
use Sidus\EAVModelBundle\Model\AttributeTypeInterface;
use Sidus\EAVModelBundle\Model\FamilyInterface;
use Sidus\EAVModelBundle\Registry\AttributeTypeRegistry;
use Sidus\EAVModelBundle\Registry\FamilyRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* Display model configuration in the debug toolbar
Expand All @@ -24,16 +26,26 @@
*/
class ModelConfigurationDataCollector extends DataCollector
{
/** @var array[] */
protected $data = [
'families' => [],
'attributeTypes' => [],
];

/** @var FamilyRegistry */
protected $familyRegistry;

/** @var AttributeTypeRegistry */
protected $attributeTypeRegistry;

/**
* @param FamilyRegistry $familyRegistry
* @param AttributeTypeRegistry $attributeTypeRegistry
*/
public function __construct(FamilyRegistry $familyRegistry, AttributeTypeRegistry $attributeTypeRegistry)
{
$this->data = [
'familyRegistry' => $familyRegistry,
'attributeTypeRegistry' => $attributeTypeRegistry,
];
$this->familyRegistry = $familyRegistry;
$this->attributeTypeRegistry = $attributeTypeRegistry;
}

/**
Expand All @@ -45,29 +57,39 @@ public function __construct(FamilyRegistry $familyRegistry, AttributeTypeRegistr
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
foreach ($this->familyRegistry->getFamilies() as $family) {
$this->data['families'][$family->getCode()] = $this->parseFamily($family);
}
foreach ($this->attributeTypeRegistry->getTypes() as $attributeType) {
$this->data['attributeTypes'][$attributeType->getCode()] = $this->parseAttributeType($attributeType);
}
}

/**
* {@inheritdoc}
*/
public function reset()
{
$this->data = [
'families' => [],
'attributeTypes' => [],
];
}

/**
* @return FamilyRegistry
* @return array[]
*/
public function getFamilyRegistry()
public function getFamilies()
{
return $this->data['familyRegistry'];
return $this->data['families'];
}

/**
* @return AttributeTypeRegistry
* @return array[]
*/
public function getAttributeTypeRegistry()
public function getAttributeTypes()
{
return $this->data['attributeTypeRegistry'];
return $this->data['attributeTypes'];
}

/**
Expand All @@ -81,12 +103,78 @@ public function getName()
}

/**
* @param array $data
* @param FamilyInterface|null $family
*
* @return Data|array
* @return array|null
*/
public function wrapData(array $data)
protected function parseFamily(FamilyInterface $family = null)
{
return $this->cloneVar($data);
if (null === $family) {
return null;
}

return [
'code' => $family->getCode(),
'label' => $family->getLabel(),
'instantiable' => $family->isInstantiable(),
'singleton' => $family->isSingleton(),
'parent' => $family->getParent() ? [
'code' => $family->getParent()->getCode(),
'label' => $family->getParent()->getLabel(),
] : null,
'dataClass' => $family->getDataClass(),
'valueClass' => $family->getValueClass(),
'attributeAsIdentifier' => $this->parseAttribute($family->getAttributeAsIdentifier()),
'attributeAsLabel' => $this->parseAttribute($family->getAttributeAsLabel()),
'attributes' => array_map([$this, 'parseAttribute'], $family->getAttributes()),
'data_class' => $family->getDataClass(),
];
}

/**
* @param AttributeInterface|null $attribute
*
* @return array|null
*/
protected function parseAttribute(AttributeInterface $attribute = null)
{
if (null === $attribute) {
return null;
}

return [
'code' => $attribute->getCode(),
'label' => $attribute->getLabel(),
'group' => $attribute->getGroup(),
'type' => $this->parseAttributeType($attribute->getType()),
'required' => $attribute->isRequired(),
'unique' => $attribute->isUnique(),
'multiple' => $attribute->isMultiple(),
'collection' => $attribute->isCollection(),
'contextMask' => $attribute->getContextMask(),
'validationRules' => $this->cloneVar($attribute->getValidationRules()),
'options' => $this->cloneVar($attribute->getOptions()),
'formOptions' => $this->cloneVar($attribute->getFormOptions()),
];
}

/**
* @param AttributeTypeInterface|null $attributeType
*
* @return array|null
*/
protected function parseAttributeType(AttributeTypeInterface $attributeType = null)
{
if (null === $attributeType) {
return null;
}

return [
'code' => $attributeType->getCode(),
'relation' => $attributeType->isRelation(),
'embedded' => $attributeType->isEmbedded(),
'databaseType' => $attributeType->getDatabaseType(),
'formType' => $attributeType->getFormType(),
];
}
}
26 changes: 12 additions & 14 deletions Resources/views/Profiler/sidus_eav_model.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Attribute types</b>
<span class="sf-toolbar-status">{{ collector.attributeTypeRegistry.types|length }}</span>
<span class="sf-toolbar-status">{{ collector.attributeTypes|length }}</span>
</div>
<div class="sf-toolbar-info-piece">
<b>Families</b>
<span class="sf-toolbar-status">{{ collector.familyRegistry.familyCodes|length }}</span>
<span class="sf-toolbar-status">{{ collector.families|length }}</span>
</div>
{% endset %}

Expand Down Expand Up @@ -61,7 +61,7 @@
<div class="tab">
<h3 class="tab-title">
Families
<span class="badge">{{ collector.familyRegistry.familyCodes|length }}</span>
<span class="badge">{{ collector.families|length }}</span>
</h3>
<div class="tab-content">
<table>
Expand All @@ -75,17 +75,17 @@
</tr>
</thead>
<tbody>
{% for family in collector.familyRegistry.families %}
{% for family in collector.families %}
{# @var family \Sidus\EAVModelBundle\Model\FamilyInterface #}
<tr class="{{ not family.instantiable ? 'text-muted' }}">
<td class="font-normal text-bold">
<span class="colored text-bold">{{ family.code }}</span>
<span class="text-muted newline">{{ family }}</span>
<span class="text-muted newline">{{ family.label }}</span>
</td>
<td class="font-normal text-small" nowrap="">
{% if family.parent %}
<span class="colored text-bold">{{ family.parent.code }}</span>
<span class="text-muted newline">{{ family.parent }}</span>
<span class="text-muted newline">{{ family.parent.label }}</span>
{% endif %}
</td>
<td class="font-normal text-small" nowrap="">
Expand Down Expand Up @@ -147,7 +147,7 @@
<tr>
<td class="font-normal text-bold">
<span class="colored text-bold">{{ attribute.code }}</span>
<span class="text-muted newline">{{ attribute }}</span>
<span class="text-muted newline">{{ attribute.label }}</span>
</td>
<td class="font-normal text-small" nowrap="">
{{ attribute.group }}
Expand Down Expand Up @@ -176,18 +176,16 @@
<dd>{{ attribute.contextMask|join(', ') }}</dd>
<dt>Validation Rules:</dt>
<dd>
{% for validationRule in attribute.validationRules %}
{{ profiler_dump(collector.wrapData(validationRule), maxDepth=1) }}
{% endfor %}
{{ profiler_dump(attribute.validationRules, maxDepth=2) }}
</dd>
</dl>
</td>
<td class="font-normal text-small">
<dl class="sidus-properties">
<dt>Options:</dt>
<dd>{{ profiler_dump(collector.wrapData(attribute.options), maxDepth=1) }}</dd>
<dd>{{ profiler_dump(attribute.options, maxDepth=1) }}</dd>
<dt>Form options:</dt>
<dd>{{ profiler_dump(collector.wrapData(attribute.formOptions), maxDepth=1) }}</dd>
<dd>{{ profiler_dump(attribute.formOptions, maxDepth=1) }}</dd>
</dl>
</td>
</tr>
Expand All @@ -206,7 +204,7 @@
<div class="tab">
<h3 class="tab-title">
Attribute Types
<span class="badge">{{ collector.attributeTypeRegistry.types|length }}</span>
<span class="badge">{{ collector.attributeTypes|length }}</span>
</h3>
<div class="tab-content">
<table>
Expand All @@ -219,7 +217,7 @@
</tr>
</thead>
<tbody>
{% for attributeType in collector.attributeTypeRegistry.types %}
{% for attributeType in collector.attributeTypes %}
<tr>
<td>{{ attributeType.code }}</td>
<td>{{ attributeType.databaseType }}</td>
Expand Down

0 comments on commit 387baf6

Please sign in to comment.