Skip to content

Commit

Permalink
Merge pull request #9 from akeneo/generate_attribute_groups
Browse files Browse the repository at this point in the history
Generate attribute groups
  • Loading branch information
BitOne committed Apr 13, 2015
2 parents 5bb7c12 + acc24de commit 13995bf
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Configuration/GeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getConfigTreeBuilder()
->scalarNode('delimiter')->defaultValue(',')->end()
->end()
->end()
->arrayNode('attribute_group')
->children()
->integerNode('count')->min(1)->isRequired()->end()
->end()
->end()
->arrayNode('attribute')
->children()
->integerNode('count')->min(1)->isRequired()->end()
Expand Down
30 changes: 22 additions & 8 deletions Generator/AttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class AttributeGenerator implements GeneratorInterface
/** @var string */
protected $attributesFile;

/** @var array */
protected $attributeGroups;

/** @var array */
protected $attributeGroupCodes;

/** @var array */
protected $locales;

Expand Down Expand Up @@ -147,6 +153,16 @@ public function getAttributeObjects()
return $attributeObjects;
}

/**
* Set attribute groups.
*
* @param array $attributeGroups
*/
public function setAttributeGroups(array $attributeGroups)
{
$this->attributeGroups = $attributeGroups;
}

/**
* Get a random non-identifier attribute type
*
Expand Down Expand Up @@ -186,22 +202,20 @@ protected function getRandomAttributeGroupCode()
}

/**
* Get all attribute groups
* Get all generated attribute groups.
*
* @return array
*/
protected function getAttributeGroupCodes()
{
if (null === $this->groupCodes) {
$this->groupCodes = [];

$groups = $this->groupRepository->findAll();
foreach ($groups as $group) {
$this->groupCodes[] = $group->getCode();
if (null === $this->attributeGroupCodes) {
$this->attributeGroupCodes = [];
foreach (array_keys($this->attributeGroups) as $code) {
$this->attributeGroupCodes[] = $code;
}
}

return $this->groupCodes;
return $this->attributeGroupCodes;
}

/**
Expand Down
146 changes: 146 additions & 0 deletions Generator/AttributeGroupGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace Pim\Bundle\DataGeneratorBundle\Generator;

use Faker;
use Pim\Bundle\CatalogBundle\Entity\AttributeGroup;
use Pim\Bundle\CatalogBundle\Entity\Locale;
use Pim\Bundle\CatalogBundle\Repository\LocaleRepositoryInterface;
use Symfony\Component\Console\Helper\ProgressHelper;
use Symfony\Component\Yaml;

/**
* Class AttributeGroupGenerator
*
* @author Damien Carcel (https://github.com/damien-carcel)
* @copyright 2015 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class AttributeGroupGenerator implements GeneratorInterface
{
/** @staticvar string */
const ATTR_GROUP_CODE_PREFIX = 'attr_gr_';

/** @staticvar string */
const ATTRIBUTE_GROUP_FILENAME = 'attribute_groups.yml';

/** @var array */
protected $attributeGroups;

/** @var string */
protected $attributeGroupsFile;

/** @var Faker\Generator */
protected $faker;

/** @var LocaleRepositoryInterface */
protected $localeRepository;

/** @var Locale[] */
protected $locales;

/**
* @param LocaleRepositoryInterface $localeRepository
*/
public function __construct(LocaleRepositoryInterface $localeRepository)
{
$this->localeRepository = $localeRepository;
}

/**
* {@inheritdoc}
*/
public function generate(array $config, $outputDir, ProgressHelper $progress, array $options = null)
{
$this->attributeGroupsFile = $outputDir . '/' . static::ATTRIBUTE_GROUP_FILENAME;

$count = (int) $config['count'];

$this->faker = Faker\Factory::create();

$this->attributeGroups = [];

for ($i = 0; $i < $count; $i++) {
$attributeGroup = [];

$attributeGroup['sortOrder'] = $this->faker->numberBetween(1, 10);
$attributeGroup['labels'] = $this->getLocalizedRandomLabels();

$this->attributeGroups[self::ATTR_GROUP_CODE_PREFIX.$i] = $attributeGroup;
$progress->advance();
}

$this->writeYamlFile(['attribute_groups' => $this->attributeGroups], $this->attributeGroupsFile);

return $this;
}

/**
* Return the generated attribute groups as AttributeGroup object.
*
* @return array
*/
public function getAttributeGroupObjects()
{
$attrGroupObjects = [];

foreach ($this->attributeGroups as $code => $attributeGroup) {
$attrGroupObject = new AttributeGroup();

$attrGroupObject->setCode($code);

$attrGroupObjects[$code] = $attrGroupObject;
}

return $attrGroupObjects;
}

/**
* Get localized random labels
*
* @return array
*/
protected function getLocalizedRandomLabels()
{
$locales = $this->getLocales();
$labels = [];

foreach ($locales as $locale) {
$labels[$locale->getCode()] = $this->faker->sentence(2);
}

return $labels;
}

/**
* Get active locales
*
* @return Locale[]
*/
protected function getLocales()
{
if (null === $this->locales) {
$this->locales = [];
$locales = $this->localeRepository->findBy(['activated' => 1]);
foreach ($locales as $locale) {
$this->locales[$locale->getCode()] = $locale;
}
}

return $this->locales;
}

/**
* Write a YAML file
*
* @param array $data
* @param string $filename
*/
protected function writeYamlFile(array $data, $filename)
{
$dumper = new Yaml\Dumper();
$yamlData = $dumper->dump($data, 5, 0, true, true);

file_put_contents($filename, $yamlData);
}
}
24 changes: 19 additions & 5 deletions Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,36 @@ class Generator implements GeneratorInterface
/** @var CategoryGenerator */
protected $categoryGenerator;

/** @var AttributeGroupGenerator */
protected $attrGroupGenerator;

/**
* @param AttributeGenerator $attributeGenerator
* @param FamilyGenerator $familyGenerator
* @param ProductGenerator $productGenerator
* @param CategoryGenerator $categoryGenerator
* @param AttributeGenerator $attributeGenerator
* @param FamilyGenerator $familyGenerator
* @param ProductGenerator $productGenerator
* @param CategoryGenerator $categoryGenerator
* @param AttributeGroupGenerator $attrGroupGenerator
*/
public function __construct(
AttributeGenerator $attributeGenerator,
FamilyGenerator $familyGenerator,
ProductGenerator $productGenerator,
CategoryGenerator $categoryGenerator
CategoryGenerator $categoryGenerator,
AttributeGroupGenerator $attrGroupGenerator
) {
$this->attributeGenerator = $attributeGenerator;
$this->familyGenerator = $familyGenerator;
$this->productGenerator = $productGenerator;
$this->categoryGenerator = $categoryGenerator;
$this->attrGroupGenerator = $attrGroupGenerator;
}

/**
* {@inheritdoc}
*/
public function generate(array $config, $outputDir, ProgressHelper $progress, array $options = null)
{
$generatedAttrGroups = [];
$generatedAttributes = [];

if (isset($config['entities']['product']) && count($config['entities']) > 1) {
Expand All @@ -62,8 +69,15 @@ public function generate(array $config, $outputDir, ProgressHelper $progress, ar
$this->categoryGenerator->generate($categoryConfig, $outputDir, $progress);
}

if (isset($config['entities']['attribute_group'])) {
$attributeGroupConfig = $config['entities']['attribute_group'];
$this->attrGroupGenerator->generate($attributeGroupConfig, $outputDir, $progress);
$generatedAttrGroups = $this->attrGroupGenerator->getAttributeGroupObjects();
}

if (isset($config['entities']['attribute'])) {
$attributeConfig = $config['entities']['attribute'];
$this->attributeGenerator->setAttributeGroups($generatedAttrGroups);
$this->attributeGenerator->generate($attributeConfig, $outputDir, $progress);
$generatedAttributes = $this->attributeGenerator->getAttributeObjects();
}
Expand Down
21 changes: 14 additions & 7 deletions Resources/config/generators.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
parameters:
pim_data_generator.generator.class : Pim\Bundle\DataGeneratorBundle\Generator\Generator
pim_data_generator.generator.product.class : Pim\Bundle\DataGeneratorBundle\Generator\ProductGenerator
pim_data_generator.generator.attribute.class : Pim\Bundle\DataGeneratorBundle\Generator\AttributeGenerator
# pim_data_generator.generator.attribute_option.class : Pim\Bundle\DataGeneratorBundle\Generator\AttributeOptionGenerator
pim_data_generator.generator.family.class : Pim\Bundle\DataGeneratorBundle\Generator\FamilyGenerator
pim_data_generator.generator.category.class : Pim\Bundle\DataGeneratorBundle\Generator\CategoryGenerator
pim_data_generator.generator.class : Pim\Bundle\DataGeneratorBundle\Generator\Generator
pim_data_generator.generator.product.class : Pim\Bundle\DataGeneratorBundle\Generator\ProductGenerator
pim_data_generator.generator.attribute.class : Pim\Bundle\DataGeneratorBundle\Generator\AttributeGenerator
# pim_data_generator.generator.attribute_option.class : Pim\Bundle\DataGeneratorBundle\Generator\AttributeOptionGenerator
pim_data_generator.generator.family.class : Pim\Bundle\DataGeneratorBundle\Generator\FamilyGenerator
pim_data_generator.generator.attribute_group.class: Pim\Bundle\DataGeneratorBundle\Generator\AttributeGroupGenerator
pim_data_generator.generator.category.class : Pim\Bundle\DataGeneratorBundle\Generator\CategoryGenerator

services:
pim_data_generator.generator:
Expand All @@ -14,6 +15,7 @@ services:
- @pim_data_generator.generator.family
- @pim_data_generator.generator.product
- @pim_data_generator.generator.category
- @pim_data_generator.generator.attribute_group

pim_data_generator.generator.category:
class: %pim_data_generator.generator.category.class%
Expand All @@ -40,7 +42,12 @@ services:
# - @pim_catalog.repository.attribute
# - @pim_catalog.repository.channel
# - @pim_catalog.repository.locale
#

pim_data_generator.generator.attribute_group:
class: %pim_data_generator.generator.attribute_group.class%
arguments:
- @pim_catalog.repository.locale

pim_data_generator.generator.product:
class: %pim_data_generator.generator.product.class%
arguments:
Expand Down
3 changes: 2 additions & 1 deletion Resources/examples/attributes.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
data_generator:
output_dir: /tmp/generated_data
entities:
attribute_group:
count: 10
attribute:
count: 100
identifier_attribute: sku
Expand All @@ -12,4 +14,3 @@ data_generator:
identifier_attribute: sku
label_attribute: name
requirements_count: 5

0 comments on commit 13995bf

Please sign in to comment.