Skip to content

Commit

Permalink
Merge pull request #536 from coreshop/lazy-loading-core-extensions
Browse files Browse the repository at this point in the history
[CoreBundle, ProductBundle] Lazy Loading Core Extensions
  • Loading branch information
dpfaffenbauer authored Jul 19, 2018
2 parents 2ea5a2e + 7d5df2f commit 9032877
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 17 deletions.
61 changes: 52 additions & 9 deletions src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function getGetterCode($class)
$code .= '* @return ' . $this->getPhpdocType() . "\n";
$code .= '*/' . "\n";
$code .= 'public function get' . ucfirst($key) . ' (\CoreShop\Component\Store\Model\StoreInterface $store = null) {' . "\n";
$code .= "\t".'$this->'.$key.' = $this->getClass()->getFieldDefinition("'.$key.'")->preGetData($this);'."\n";
$code .= "\t" . 'if (is_null($store)) {' . "\n";
$code .= "\t\t" . 'return $this->' . $key . ";\n";
$code .= "\t" . '}' . "\n";
Expand All @@ -196,7 +197,7 @@ public function getSetterCode($class)
$code .= '*/' . "\n";
$code .= 'public function set' . ucfirst($key) . ' ($' . $key . ', \CoreShop\Component\Store\Model\StoreInterface $store = null) {' . "\n";
$code .= "\t" . 'if (is_null($' . $key . ')) {' . "\n";
$code .= "\t\t" . '$' . $key . ' = [];' . "\n";
$code .= "\t\t" . 'return $this;' . "\n";
$code .= "\t" . '}' . "\n";
$code .= "\t" . "\n";
$code .= "\t" . 'if (!is_int($' . $key . ') && !is_array($' . $key . ')) {' . "\n";
Expand All @@ -208,31 +209,73 @@ public function getSetterCode($class)
$code .= "\t" . 'else if (!is_null($store)) {' . "\n";
$code .= "\t\t" . '$this->' . $key . '[$store->getId()] = $' . $key . ';' . "\n";
$code .= "\t" . '}' . "\n";
$code .= "\t" . '$this->' . $key . ' = ' . '$this->getClass()->getFieldDefinition("' . $key . '")->preSetData($this, $this->' . $key . ');' . "\n";
$code .= "\t" . 'return $this;' . "\n";
$code .= "}\n\n";

return $code;
}

/**
* {@inheritdoc}
*/
public function getDataFromResource($data, $object = null, $params = [])
{
return [];
}

/**
* {@inheritdoc}
*/
public function load($object)
public function preGetData($object, $params = [])
{
$prices = $this->getProductStorePriceRepository()->findForProductAndProperty($object, $this->getName());
$data = [];
$data = $object->{$this->getName()};
if (!in_array($this->getName(), $object->getO__loadedLazyFields())) {
$data = $this->load($object, ['force' => true]);

/**
* @var $price ProductStorePriceInterface
*/
foreach ($prices as $price) {
$data[$price->getStore()->getId()] = $price->getPrice();
$setter = 'set' . ucfirst($this->getName());
if (method_exists($object, $setter)) {
$object->$setter($data);
}
}

return $data;
}

/**
* {@inheritdoc}
*/
public function preSetData($object, $data, $params = [])
{
if (!in_array($this->getName(), $object->getO__loadedLazyFields())) {
$object->addO__loadedLazyField($this->getName());
}

return $data;
}

/**
* {@inheritdoc}
*/
public function load($object, $params = [])
{
if (isset($params['force']) && $params['force']) {
$prices = $this->getProductStorePriceRepository()->findForProductAndProperty($object, $this->getName());
$data = [];

/**
* @var $price ProductStorePriceInterface
*/
foreach ($prices as $price) {
$data[$price->getStore()->getId()] = $price->getPrice();
}

return $data;
}

return null;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use CoreShop\Bundle\CoreBundle\CoreExtension\StorePrice;
use CoreShop\Bundle\ProductBundle\CoreExtension\ProductSpecificPriceRules;
use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;
use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

class Version20180719151524 extends AbstractPimcoreMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$classes = new ClassDefinition\Listing();
$classes = $classes->load();

foreach ($classes as $class) {
foreach ($class->getFieldDefinitions() as $definition) {
if ($definition instanceof StorePrice || $definition instanceof ProductSpecificPriceRules) {
$class->save();
break;
}
}
}

}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,37 @@ public function preGetData($object)
{
Assert::isInstanceOf($object, ProductInterface::class);

return $this->getProductSpecificPriceRuleRepository()->findForProduct($object);
$data = $object->{$this->getName()};
if (!in_array($this->getName(), $object->getO__loadedLazyFields())) {
$data = $this->load($object, ['force' => true]);

$setter = 'set' . ucfirst($this->getName());
if (method_exists($object, $setter)) {
$object->$setter($data);
}
}

return $data;
}

/**
* {@inheritdoc}
*/
public function preSetData($object, $data, $params = [])
{
if (!in_array($this->getName(), $object->getO__loadedLazyFields())) {
$object->addO__loadedLazyField($this->getName());
}

return $data;
}

/**
* {@inheritdoc}
*/
public function getDataFromResource($data, $object = null, $params = [])
{
return [];
}

/**
Expand All @@ -119,7 +149,7 @@ public function getDataForEditmode($data, $object = null, $params = [])
];

if ($object instanceof ProductInterface) {
$prices = $this->load($object, $params);
$prices = $this->load($object, ['force' => true]);

$context = SerializationContext::create();
$context->setSerializeNull(true);
Expand Down Expand Up @@ -183,7 +213,7 @@ public function save($object, $params = [])
$getter = $this->getName();
$existingPriceRules = $object->$getter;

$all = $this->load($object, $params);
$all = $this->load($object, ['force' => true]);
$founds = [];

if (is_array($existingPriceRules)) {
Expand All @@ -210,14 +240,15 @@ public function save($object, $params = [])
}

/**
* @param $object
* @param array $params
*
* @return ProductSpecificPriceRuleInterface[]
* {@inheritdoc}
*/
public function load($object, $params = [])
{
return $this->getProductSpecificPriceRuleRepository()->findForProduct($object);
if (isset($params['force']) && $params['force']) {
return $this->getProductSpecificPriceRuleRepository()->findForProduct($object);
}

return null;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ protected function createRule()
$priceRule->setActive(true);
$priceRule->setProduct($this->product->getId());

$this->product->setSpecificPriceRules([$priceRule]);

return $priceRule;
}

Expand Down Expand Up @@ -307,6 +309,8 @@ public function testPriceRuleActionDiscountAmount()

$this->getEntityManager()->remove($rule);
$this->getEntityManager()->flush();

$this->product->setSpecificPriceRules([]);
}

/**
Expand Down Expand Up @@ -335,6 +339,8 @@ public function testPriceRuleActionDiscountPercent()

$this->getEntityManager()->remove($rule);
$this->getEntityManager()->flush();

$this->product->setSpecificPriceRules([]);
}

/**
Expand Down Expand Up @@ -364,6 +370,8 @@ public function testPriceRuleActionNewPrice()

$this->getEntityManager()->remove($rule);
$this->getEntityManager()->flush();

$this->product->setSpecificPriceRules([]);
}

/**
Expand Down Expand Up @@ -395,5 +403,7 @@ public function testPriceRuleActionDiscountPrice()

$this->getEntityManager()->remove($rule);
$this->getEntityManager()->flush();

$this->product->setSpecificPriceRules([]);
}
}

0 comments on commit 9032877

Please sign in to comment.