This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored fixture helper to use PHP7.1 and discard useless properties
- Loading branch information
1 parent
5a43c19
commit 94e991b
Showing
3 changed files
with
91 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
/* | ||
/** | ||
* This file is part of the Orbitale DoctrineTools package. | ||
* | ||
* (c) Alexandre Rock Ancelet <[email protected]> | ||
|
@@ -34,80 +34,54 @@ | |
*/ | ||
abstract class AbstractFixture extends BaseAbstractFixture implements OrderedFixtureInterface | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
/** @var ObjectManager */ | ||
private $manager; | ||
|
||
/** | ||
* @var EntityRepository | ||
*/ | ||
/** @var EntityRepository */ | ||
private $repo; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $order; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $entityClass; | ||
|
||
/** | ||
* @var PropertyAccessor | ||
*/ | ||
/** @var PropertyAccessor */ | ||
private $propertyAccessor; | ||
|
||
/** | ||
* @var null|string | ||
*/ | ||
private $referencePrefix; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $searchForMatchingIds = true; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
/** @var int */ | ||
private $totalNumberOfObjects = 0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
/** @var int */ | ||
private $numberOfIteratedObjects = 0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $flushEveryXIterations = 0; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
/** @var bool */ | ||
private $clearEMOnFlush = true; | ||
|
||
public function __construct() | ||
{ | ||
$this->order = $this->getOrder(); | ||
$this->flushEveryXIterations = $this->flushEveryXIterations(); | ||
$this->searchForMatchingIds = $this->searchForMatchingIds(); | ||
$this->entityClass = $this->getEntityClass(); | ||
$this->referencePrefix = $this->getReferencePrefix(); | ||
$this->propertyAccessor = class_exists('Symfony\Component\PropertyAccess\PropertyAccess') ? PropertyAccess::createPropertyAccessor() : null; | ||
$this->clearEMOnFlush = $this->clearEntityManagerOnFlush(); | ||
$this->clearEMOnFlush = $this->clearEntityManagerOnFlush(); | ||
if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { | ||
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the class of the entity you're managing. | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function getEntityClass(): string; | ||
|
||
/** | ||
* Returns a list of objects to insert in the database. | ||
* | ||
* @return ArrayCollection|object[] | ||
*/ | ||
abstract protected function getObjects(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(ObjectManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
|
||
if ($this->disableLogger()) { | ||
if ($this->disableLogger() && $this->manager instanceof EntityManager) { | ||
$this->manager->getConnection()->getConfiguration()->setSQLLogger(null); | ||
} | ||
|
||
|
@@ -126,8 +100,8 @@ public function load(ObjectManager $manager) | |
// Flush if we performed a "whole" fixture load, | ||
// or if we flushed with batches but have not flushed all items. | ||
if ( | ||
!$this->flushEveryXIterations | ||
|| ($this->flushEveryXIterations && $this->numberOfIteratedObjects !== $this->totalNumberOfObjects) | ||
!$this->flushEveryXIterations() | ||
|| ($this->flushEveryXIterations() && $this->numberOfIteratedObjects !== $this->totalNumberOfObjects) | ||
) { | ||
$this->manager->flush(); | ||
if ($this->clearEMOnFlush) { | ||
|
@@ -145,7 +119,7 @@ public function load(ObjectManager $manager) | |
* @param ClassMetadata $metadata | ||
* @param null $id | ||
*/ | ||
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null) | ||
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null): void | ||
{ | ||
if ($id) { | ||
$metadata->setIdGenerator(new AssignedGenerator()); | ||
|
@@ -157,7 +131,7 @@ protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null) | |
/** | ||
* Creates the object and persist it in database. | ||
* | ||
* @param array|object $data | ||
* @param object $data | ||
*/ | ||
private function fixtureObject($data) | ||
{ | ||
|
@@ -168,7 +142,7 @@ private function fixtureObject($data) | |
$identifier = $metadata->getIdentifier(); | ||
|
||
// The ID is taken in account to force its use in the database. | ||
$id = array(); | ||
$id = []; | ||
foreach ($identifier as $key) { | ||
$id[$key] = $this->getPropertyFromData($data, $key); | ||
} | ||
|
@@ -183,7 +157,7 @@ private function fixtureObject($data) | |
$addRef = false; | ||
|
||
// If the user specifies an ID and the fixture class wants it to be merged, we search for an object. | ||
if ($id && $this->searchForMatchingIds) { | ||
if ($id && $this->searchForMatchingIds()) { | ||
// Checks that the object ID exists in database. | ||
$obj = $this->repo->findOneBy($id); | ||
if ($obj) { | ||
|
@@ -203,7 +177,6 @@ private function fixtureObject($data) | |
if (is_array($data)) { | ||
$obj = $this->createNewInstance($data); | ||
foreach ($data as $field => $value) { | ||
|
||
// If the value is a callable we execute it and inject the fixture object and the manager. | ||
if ($value instanceof \Closure) { | ||
$value = $value($obj, $this, $this->manager); | ||
|
@@ -226,9 +199,9 @@ private function fixtureObject($data) | |
|
||
// If we need to flush it, then we do it too. | ||
if ( | ||
$this->flushEveryXIterations | ||
&& $this->numberOfIteratedObjects | ||
&& $this->numberOfIteratedObjects % $this->flushEveryXIterations === 0 | ||
$this->numberOfIteratedObjects > 0 | ||
&& $this->flushEveryXIterations() > 0 | ||
&& $this->numberOfIteratedObjects % $this->flushEveryXIterations() === 0 | ||
) { | ||
$this->manager->flush(); | ||
if ($this->clearEMOnFlush) { | ||
|
@@ -239,27 +212,27 @@ private function fixtureObject($data) | |
} | ||
|
||
// If we have to add a reference, we do it | ||
if ($addRef === true && $obj && $this->referencePrefix) { | ||
if ($addRef === true && $obj && $this->getReferencePrefix()) { | ||
if (!$id || !reset($id)) { | ||
// If no id was provided in the object, maybe there was one after data hydration. | ||
// Can be done maybe in entity constructor or in a property callback. | ||
// So let's try to get it. | ||
if ($this->propertyAccessor) { | ||
if ($this->propertyAccessor) { | ||
try { | ||
$id = array('id' => $this->propertyAccessor->getValue($obj, 'id')); | ||
$id = ['id' => $this->propertyAccessor->getValue($obj, 'id')]; | ||
} catch (NoSuchIndexException $e) { | ||
$id = array(); | ||
$id = []; | ||
} | ||
} | ||
} elseif (method_exists($obj, 'getId')) { | ||
$id = array('id' => $obj->getId()); | ||
$id = ['id' => $obj->getId()]; | ||
} | ||
} | ||
if (1 === count($id)) { | ||
// Only reference single identifiers. | ||
$id = reset($id); | ||
$this->addReference($this->referencePrefix.($id ?: (string) $obj), $obj); | ||
$this->addReference($this->getReferencePrefix().($id ?: (string) $obj), $obj); | ||
} elseif (count($id) > 1) { | ||
throw new \RuntimeException('Cannot add reference for composite identifiers.'); | ||
} | ||
|
@@ -274,7 +247,7 @@ private function fixtureObject($data) | |
* | ||
* @return mixed | ||
*/ | ||
private function getPropertyFromData($data, $key) | ||
private function getPropertyFromData($data, string $key) | ||
{ | ||
if (is_object($data)) { | ||
$method = 'get'.ucfirst($key); | ||
|
@@ -286,9 +259,7 @@ private function getPropertyFromData($data, $key) | |
} | ||
} | ||
|
||
if (isset($data[$key])) { | ||
return $data[$key]; | ||
} | ||
return $data[$key] ?? null; | ||
} | ||
|
||
/** | ||
|
@@ -298,9 +269,9 @@ private function getPropertyFromData($data, $key) | |
* | ||
* @return int | ||
*/ | ||
public function getOrder() | ||
public function getOrder(): int | ||
{ | ||
return $this->order; | ||
return 0; | ||
} | ||
|
||
/** | ||
|
@@ -309,7 +280,7 @@ public function getOrder() | |
* | ||
* @return bool | ||
*/ | ||
protected function disableLogger() | ||
protected function disableLogger(): bool | ||
{ | ||
return false; | ||
} | ||
|
@@ -320,35 +291,30 @@ protected function disableLogger() | |
* NOTE: To create references of an object, it must have an ID, and if not, implement __toString(), because | ||
* each object is referenced BEFORE flushing the database. | ||
* NOTE2: If you specified a "flushEveryXIterations" value, then the object will be provided with an ID every time. | ||
* | ||
* @return string|null | ||
*/ | ||
protected function getReferencePrefix() | ||
protected function getReferencePrefix(): ?string | ||
{ | ||
return $this->referencePrefix; | ||
return null; | ||
} | ||
|
||
/** | ||
* If specified, the entity manager will be flushed every X times, depending on your specified values. | ||
* Default is null, so the database is flushed only at the end of all persists. | ||
* | ||
* @return bool | ||
*/ | ||
protected function flushEveryXIterations() | ||
protected function flushEveryXIterations(): int | ||
{ | ||
return $this->flushEveryXIterations; | ||
return 0; | ||
} | ||
|
||
/** | ||
* If true and an ID is specified, will execute a $manager->find($id) in the database. | ||
* By default this var is true. | ||
* If true and an ID is specified in fixture's $data, will execute a $manager->find($id) in the database. | ||
* Be careful, if you set it to false you may have "duplicate entry" errors if your database is already populated. | ||
* | ||
* @return bool | ||
*/ | ||
protected function searchForMatchingIds() | ||
protected function searchForMatchingIds(): bool | ||
{ | ||
return $this->searchForMatchingIds; | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -357,35 +323,23 @@ protected function searchForMatchingIds() | |
* | ||
* @return bool | ||
*/ | ||
protected function clearEntityManagerOnFlush() | ||
protected function clearEntityManagerOnFlush(): bool | ||
{ | ||
return $this->clearEMOnFlush; | ||
return true; | ||
} | ||
|
||
/** | ||
* Creates a new instance of the class associated with the fixture. | ||
* Very useful if you have constructor arguments to manage. | ||
* Override this method if you have constructor arguments to manage yourself depending on input data. | ||
* | ||
* @param array $data | ||
* | ||
* @return object | ||
*/ | ||
protected function createNewInstance($data) | ||
protected function createNewInstance(array $data) | ||
{ | ||
return new $this->entityClass; | ||
} | ||
|
||
/** | ||
* Returns the class of the entity you're managing. | ||
* | ||
* @return string | ||
*/ | ||
protected abstract function getEntityClass(); | ||
$class = $this->getEntityClass(); | ||
|
||
/** | ||
* Returns a list of objects to insert in the database. | ||
* | ||
* @return ArrayCollection|object[] | ||
*/ | ||
protected abstract function getObjects(); | ||
return new $class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Orbitale DoctrineTools package. | ||
* | ||
* (c) Alexandre Rock Ancelet <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Orbitale\Component\DoctrineTools; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
|
||
class BaseEntityRepository extends EntityRepository | ||
{ | ||
|
||
} | ||
|
||
@trigger_error(sprintf('Class %s is deprecated, use the %s trait instead.', BaseEntityRepository::class, EntityRepositoryHelperTrait::class), E_USER_DEPRECATED); |
Oops, something went wrong.