init
This commit is contained in:
75
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php
vendored
Normal file
75
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/ColumnTypeGuesser.php
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Doctrine;
|
||||
|
||||
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
||||
|
||||
class ColumnTypeGuesser
|
||||
{
|
||||
protected $generator;
|
||||
|
||||
/**
|
||||
* @param \Faker\Generator $generator
|
||||
*/
|
||||
public function __construct(\Faker\Generator $generator)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMetadata $class
|
||||
* @return \Closure|null
|
||||
*/
|
||||
public function guessFormat($fieldName, ClassMetadata $class)
|
||||
{
|
||||
$generator = $this->generator;
|
||||
$type = $class->getTypeOfField($fieldName);
|
||||
switch ($type) {
|
||||
case 'boolean':
|
||||
return function () use ($generator) {
|
||||
return $generator->boolean;
|
||||
};
|
||||
case 'decimal':
|
||||
$size = isset($class->fieldMappings[$fieldName]['precision']) ? $class->fieldMappings[$fieldName]['precision'] : 2;
|
||||
|
||||
return function () use ($generator, $size) {
|
||||
return $generator->randomNumber($size + 2) / 100;
|
||||
};
|
||||
case 'smallint':
|
||||
return function () {
|
||||
return mt_rand(0, 65535);
|
||||
};
|
||||
case 'integer':
|
||||
return function () {
|
||||
return mt_rand(0, intval('2147483647'));
|
||||
};
|
||||
case 'bigint':
|
||||
return function () {
|
||||
return mt_rand(0, intval('18446744073709551615'));
|
||||
};
|
||||
case 'float':
|
||||
return function () {
|
||||
return mt_rand(0, intval('4294967295'))/mt_rand(1, intval('4294967295'));
|
||||
};
|
||||
case 'string':
|
||||
$size = isset($class->fieldMappings[$fieldName]['length']) ? $class->fieldMappings[$fieldName]['length'] : 255;
|
||||
|
||||
return function () use ($generator, $size) {
|
||||
return $generator->text($size);
|
||||
};
|
||||
case 'text':
|
||||
return function () use ($generator) {
|
||||
return $generator->text;
|
||||
};
|
||||
case 'datetime':
|
||||
case 'date':
|
||||
case 'time':
|
||||
return function () use ($generator) {
|
||||
return $generator->datetime;
|
||||
};
|
||||
default:
|
||||
// no smart way to guess what the user expects here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
251
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/EntityPopulator.php
vendored
Normal file
251
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/EntityPopulator.php
vendored
Normal file
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Doctrine;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* Service class for populating a table through a Doctrine Entity class.
|
||||
*/
|
||||
class EntityPopulator
|
||||
{
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
protected $class;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $columnFormatters = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $modifiers = array();
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param ClassMetadata $class
|
||||
*/
|
||||
public function __construct(ClassMetadata $class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getClass()
|
||||
{
|
||||
return $this->class->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $columnFormatters
|
||||
*/
|
||||
public function setColumnFormatters($columnFormatters)
|
||||
{
|
||||
$this->columnFormatters = $columnFormatters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getColumnFormatters()
|
||||
{
|
||||
return $this->columnFormatters;
|
||||
}
|
||||
|
||||
public function mergeColumnFormattersWith($columnFormatters)
|
||||
{
|
||||
$this->columnFormatters = array_merge($this->columnFormatters, $columnFormatters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $modifiers
|
||||
*/
|
||||
public function setModifiers(array $modifiers)
|
||||
{
|
||||
$this->modifiers = $modifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getModifiers()
|
||||
{
|
||||
return $this->modifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $modifiers
|
||||
*/
|
||||
public function mergeModifiersWith(array $modifiers)
|
||||
{
|
||||
$this->modifiers = array_merge($this->modifiers, $modifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Faker\Generator $generator
|
||||
* @return array
|
||||
*/
|
||||
public function guessColumnFormatters(\Faker\Generator $generator)
|
||||
{
|
||||
$formatters = array();
|
||||
$nameGuesser = new \Faker\Guesser\Name($generator);
|
||||
$columnTypeGuesser = new ColumnTypeGuesser($generator);
|
||||
foreach ($this->class->getFieldNames() as $fieldName) {
|
||||
if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
|
||||
if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
|
||||
$formatters[$fieldName] = $formatter;
|
||||
continue;
|
||||
}
|
||||
if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
|
||||
$formatters[$fieldName] = $formatter;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->class->getAssociationNames() as $assocName) {
|
||||
if ($this->class->isCollectionValuedAssociation($assocName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$relatedClass = $this->class->getAssociationTargetClass($assocName);
|
||||
|
||||
$unique = $optional = false;
|
||||
if ($this->class instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
|
||||
$mappings = $this->class->getAssociationMappings();
|
||||
foreach ($mappings as $mapping) {
|
||||
if ($mapping['targetEntity'] == $relatedClass) {
|
||||
if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_ONE) {
|
||||
$unique = true;
|
||||
$optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($this->class instanceof \Doctrine\ODM\MongoDB\Mapping\ClassMetadata) {
|
||||
$mappings = $this->class->associationMappings;
|
||||
foreach ($mappings as $mapping) {
|
||||
if ($mapping['targetDocument'] == $relatedClass) {
|
||||
if ($mapping['type'] == \Doctrine\ODM\MongoDB\Mapping\ClassMetadata::ONE && $mapping['association'] == \Doctrine\ODM\MongoDB\Mapping\ClassMetadata::REFERENCE_ONE) {
|
||||
$unique = true;
|
||||
$optional = isset($mapping['nullable']) ? $mapping['nullable'] : false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$index = 0;
|
||||
$formatters[$assocName] = function ($inserted) use ($relatedClass, &$index, $unique, $optional) {
|
||||
|
||||
if (isset($inserted[$relatedClass])) {
|
||||
if ($unique) {
|
||||
$related = null;
|
||||
if (isset($inserted[$relatedClass][$index]) || !$optional) {
|
||||
$related = $inserted[$relatedClass][$index];
|
||||
}
|
||||
|
||||
$index++;
|
||||
|
||||
return $related;
|
||||
}
|
||||
|
||||
return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
return $formatters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert one new record using the Entity class.
|
||||
* @param ObjectManager $manager
|
||||
* @param bool $generateId
|
||||
* @return EntityPopulator
|
||||
*/
|
||||
public function execute(ObjectManager $manager, $insertedEntities, $generateId = false)
|
||||
{
|
||||
$obj = $this->class->newInstance();
|
||||
|
||||
$this->fillColumns($obj, $insertedEntities);
|
||||
$this->callMethods($obj, $insertedEntities);
|
||||
|
||||
if ($generateId) {
|
||||
$idsName = $this->class->getIdentifier();
|
||||
foreach ($idsName as $idName) {
|
||||
$id = $this->generateId($obj, $idName, $manager);
|
||||
$this->class->reflFields[$idName]->setValue($obj, $id);
|
||||
}
|
||||
}
|
||||
|
||||
$manager->persist($obj);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
private function fillColumns($obj, $insertedEntities)
|
||||
{
|
||||
foreach ($this->columnFormatters as $field => $format) {
|
||||
if (null !== $format) {
|
||||
// Add some extended debugging information to any errors thrown by the formatter
|
||||
try {
|
||||
$value = is_callable($format) ? $format($insertedEntities, $obj) : $format;
|
||||
} catch (\InvalidArgumentException $ex) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
"Failed to generate a value for %s::%s: %s",
|
||||
get_class($obj),
|
||||
$field,
|
||||
$ex->getMessage()
|
||||
));
|
||||
}
|
||||
// Try a standard setter if it's available, otherwise fall back on reflection
|
||||
$setter = sprintf("set%s", ucfirst($field));
|
||||
if (is_callable(array($obj, $setter))) {
|
||||
$obj->$setter($value);
|
||||
} else {
|
||||
$this->class->reflFields[$field]->setValue($obj, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function callMethods($obj, $insertedEntities)
|
||||
{
|
||||
foreach ($this->getModifiers() as $modifier) {
|
||||
$modifier($obj, $insertedEntities);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return int|null
|
||||
*/
|
||||
private function generateId($obj, $column, ObjectManager $manager)
|
||||
{
|
||||
/* @var $repository \Doctrine\Common\Persistence\ObjectRepository */
|
||||
$repository = $manager->getRepository(get_class($obj));
|
||||
$result = $repository->createQueryBuilder('e')
|
||||
->select(sprintf('e.%s', $column))
|
||||
->getQuery()
|
||||
->execute();
|
||||
$ids = array_map('current', $result->toArray());
|
||||
|
||||
$id = null;
|
||||
do {
|
||||
$id = mt_rand();
|
||||
} while (in_array($id, $ids));
|
||||
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
82
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/Populator.php
vendored
Normal file
82
vendor/fzaninotto/faker/src/Faker/ORM/Doctrine/Populator.php
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Doctrine;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
/**
|
||||
* Service class for populating a database using the Doctrine ORM or ODM.
|
||||
* A Populator can populate several tables using ActiveRecord classes.
|
||||
*/
|
||||
class Populator
|
||||
{
|
||||
protected $generator;
|
||||
protected $manager;
|
||||
protected $entities = array();
|
||||
protected $quantities = array();
|
||||
protected $generateId = array();
|
||||
|
||||
/**
|
||||
* @param \Faker\Generator $generator
|
||||
* @param ObjectManager|null $manager
|
||||
*/
|
||||
public function __construct(\Faker\Generator $generator, ObjectManager $manager = null)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an order for the generation of $number records for $entity.
|
||||
*
|
||||
* @param mixed $entity A Doctrine classname, or a \Faker\ORM\Doctrine\EntityPopulator instance
|
||||
* @param int $number The number of entities to populate
|
||||
*/
|
||||
public function addEntity($entity, $number, $customColumnFormatters = array(), $customModifiers = array(), $generateId = false)
|
||||
{
|
||||
if (!$entity instanceof \Faker\ORM\Doctrine\EntityPopulator) {
|
||||
if (null === $this->manager) {
|
||||
throw new \InvalidArgumentException("No entity manager passed to Doctrine Populator.");
|
||||
}
|
||||
$entity = new \Faker\ORM\Doctrine\EntityPopulator($this->manager->getClassMetadata($entity));
|
||||
}
|
||||
$entity->setColumnFormatters($entity->guessColumnFormatters($this->generator));
|
||||
if ($customColumnFormatters) {
|
||||
$entity->mergeColumnFormattersWith($customColumnFormatters);
|
||||
}
|
||||
$entity->mergeModifiersWith($customModifiers);
|
||||
$this->generateId[$entity->getClass()] = $generateId;
|
||||
|
||||
$class = $entity->getClass();
|
||||
$this->entities[$class] = $entity;
|
||||
$this->quantities[$class] = $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the database using all the Entity classes previously added.
|
||||
*
|
||||
* @param null|EntityManager $entityManager A Doctrine connection object
|
||||
*
|
||||
* @return array A list of the inserted PKs
|
||||
*/
|
||||
public function execute($entityManager = null)
|
||||
{
|
||||
if (null === $entityManager) {
|
||||
$entityManager = $this->manager;
|
||||
}
|
||||
if (null === $entityManager) {
|
||||
throw new \InvalidArgumentException("No entity manager passed to Doctrine Populator.");
|
||||
}
|
||||
|
||||
$insertedEntities = array();
|
||||
foreach ($this->quantities as $class => $number) {
|
||||
$generateId = $this->generateId[$class];
|
||||
for ($i=0; $i < $number; $i++) {
|
||||
$insertedEntities[$class][]= $this->entities[$class]->execute($entityManager, $insertedEntities, $generateId);
|
||||
}
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $insertedEntities;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user