Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Refactor to new plugin structure
Browse files Browse the repository at this point in the history
  • Loading branch information
janbuecker committed Apr 9, 2019
1 parent 09e8c60 commit 428a181
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 113 deletions.
1 change: 0 additions & 1 deletion Resources/views/administration/main.js

This file was deleted.

7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
"name": "shopware AG"
}
],
"autoload": {
"psr-4": {
"ShopwareLabs\\Plugin\\SwagBundleExample\\": "src/"
}
},
"extra": {
"shopware-plugin-class": "SwagBundleExample\\SwagBundleExample",
"shopware-plugin-class": "ShopwareLabs\\Plugin\\SwagBundleExample\\SwagBundleExample",
"copyright": "(c) by shopware AG",
"label": {
"de_DE": "Beispiel für Shopware",
Expand Down
1 change: 1 addition & 0 deletions src/Administration/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './module/swag-bundle';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace SwagBundleExample\Core\Checkout\Bundle;
namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Checkout\Bundle;

use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartBehavior;
Expand All @@ -14,8 +14,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\StructCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use SwagBundleExample\Core\Content\Bundle\BundleCollection;
use SwagBundleExample\Core\Content\Bundle\BundleEntity;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\BundleCollection;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\BundleEntity;

class BundleCollector implements CollectorInterface
{
Expand All @@ -28,7 +28,6 @@ class BundleCollector implements CollectorInterface
*/
private $bundleRepository;


public function __construct(EntityRepositoryInterface $bundleRepository)
{
$this->bundleRepository = $bundleRepository;
Expand All @@ -41,7 +40,7 @@ public function prepare(StructCollection $definitions, Cart $cart, SalesChannelC
{
$bundleLineItems = $cart->getLineItems()->filterType(self::TYPE);

if($bundleLineItems->count() === 0) {
if ($bundleLineItems->count() === 0) {
return;
}

Expand All @@ -55,7 +54,7 @@ public function collect(StructCollection $fetchDefinitions, StructCollection $da
{
$bundleDefinitions = $fetchDefinitions->filterInstance(BundleFetchDefinition::class);

if($bundleDefinitions->count() === 0) {
if ($bundleDefinitions->count() === 0) {
return;
}

Expand All @@ -79,7 +78,7 @@ public function collect(StructCollection $fetchDefinitions, StructCollection $da

$bundleLineItem = $cart->get($bundle->getId());

if(!$bundleLineItem) {
if (!$bundleLineItem) {
continue;
}

Expand All @@ -95,8 +94,7 @@ public function collect(StructCollection $fetchDefinitions, StructCollection $da
$bundleLineItem->setRemovable(true)->setStackable(true);
}


$productIds = array_merge(... $productIds);
$productIds = array_merge(...$productIds);

$fetchDefinitions->add(new ProductFetchDefinition($productIds));
$data->set(self::DATA_KEY, $bundles);
Expand Down Expand Up @@ -129,7 +127,6 @@ public function enrich(StructCollection $data, Cart $cart, SalesChannelContext $

$id = $bundleLineItem->getPayload()['id'];

/** @var BundleCollection $bundles */
$bundle = $bundles->get($id);

// if (!$bundle) {
Expand Down Expand Up @@ -172,5 +169,4 @@ private function calculateBundleDiscount(LineItem $bundleLineItem, BundleEntity

return $discount;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php declare(strict_types=1);


namespace SwagBundleExample\Core\Checkout\Bundle;

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Checkout\Bundle;

use Shopware\Core\Framework\Struct\Struct;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php


namespace SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\CreatedAtField;
Expand All @@ -13,11 +11,10 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\MappingEntityDefinition;
use SwagBundleExample\Core\Content\Bundle\BundleDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\BundleDefinition;

class BundleProductDefinition extends MappingEntityDefinition
{

public static function getEntityName(): string
{
return 'swag_bundle_product';
Expand All @@ -34,4 +31,4 @@ protected static function defineFields(): FieldCollection
new ManyToOneAssociationField('product', 'product_id', ProductDefinition::class),
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?php


namespace SwagBundleExample\Core\Content\Bundle\Aggregate\BundleTranslation;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Aggregate\BundleTranslation;

use Shopware\Core\Framework\DataAbstractionLayer\EntityTranslationDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use SwagBundleExample\Core\Content\Bundle\BundleDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\BundleDefinition;

class BundleTranslationDefinition extends EntityTranslationDefinition
{

public static function getEntityName(): string
{
return 'swag_bundle_translation';
}
{
return 'swag_bundle_translation';
}

public static function getParentDefinitionClass(): string
{
return BundleDefinition::class;
}
{
return BundleDefinition::class;
}

protected static function defineFields(): FieldCollection
{
return new FieldCollection([
(new StringField('name', 'name'))->addFlags(new Required()),
]);
{
return new FieldCollection([
(new StringField('name', 'name'))->addFlags(new Required()),
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php


namespace SwagBundleExample\Core\Content\Bundle;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle;

use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;

Expand All @@ -17,9 +15,8 @@
*/
class BundleCollection extends EntityCollection
{

protected function getExpectedClass(): string
{
return BundleEntity::class;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php


namespace SwagBundleExample\Core\Content\Bundle;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
Expand All @@ -17,12 +15,11 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslationsAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\UpdatedAtField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use SwagBundleExample\Core\Content\Bundle\Aggregate\BundleTranslation\BundleTranslationDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Aggregate\BundleTranslation\BundleTranslationDefinition;

class BundleDefinition extends EntityDefinition
{

public static function getEntityName(): string
{
return 'swag_bundle';
Expand All @@ -48,7 +45,7 @@ protected static function defineFields(): FieldCollection
new CreatedAtField(),
new UpdatedAtField(),
new TranslationsAssociationField(BundleTranslationDefinition::class, 'bundle_id'),
new ManyToManyAssociationField('products', ProductDefinition::class, BundleProductDefinition::class, 'bundle_id', 'product_id')
new ManyToManyAssociationField('products', ProductDefinition::class, BundleProductDefinition::class, 'bundle_id', 'product_id'),
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php


namespace SwagBundleExample\Core\Content\Bundle;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle;

use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;

class BundleEntity extends Entity
{

use EntityIdTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -71,4 +69,4 @@ public function setProducts(ProductCollection $products): void
{
$this->products = $products;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace SwagBundleExample\Core\Content\Bundle\Command;
namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Command;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
Expand Down Expand Up @@ -45,13 +45,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$criteria->setLimit(50);
$productIds = $this->productRepository->searchIds(new Criteria(), $context)->getIds();

if(count($productIds) === 0) {
if (count($productIds) === 0) {
$io->error('Please create products before by using bin/console framework:demodata');
exit(1);
}

$data = [];
for($i = 0; $i < 10; $i++) {
for ($i = 0; $i < 10; ++$i) {
$bundleId = Uuid::randomHex();
$data[] = [
'discount' => random_int(100, 1000) / 100,
Expand All @@ -62,15 +62,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
],
'products' => [
[
'id' => $productIds[array_rand($productIds)]
'id' => $productIds[array_rand($productIds)],
],
[
'id' => $productIds[array_rand($productIds)]
'id' => $productIds[array_rand($productIds)],
],
[
'id' => $productIds[array_rand($productIds)]
]
]
'id' => $productIds[array_rand($productIds)],
],
],
];
}
$this->bundleRepository->upsert($data, $context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php


namespace SwagBundleExample\Core\Content\Product;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Product;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtensionInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Extension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use SwagBundleExample\Core\Content\Bundle\BundleDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Bundle\BundleDefinition;

class ProductExtension implements EntityExtensionInterface
{

/**
* Allows to add fields to an entity.
*
* To load fields by your own, add the \Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Deferred flag to the field.
* Added fields should have the \Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Extension which tells the ORM that this data
* To load fields by your own, add the \ShopwareLabs\Plugin\SwagBundleExample\Core\Framework\DataAbstractionLayer\Field\Flag\Deferred flag to the field.
* Added fields should have the \ShopwareLabs\Plugin\SwagBundleExample\Core\Framework\DataAbstractionLayer\Field\Flag\Extension which tells the ORM that this data
* is not include in the struct and collection classes
*/
public function extendFields(FieldCollection $collection): void
Expand All @@ -36,4 +33,4 @@ public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php


namespace SwagBundleExample\Core\Content\Product\Storefront;
<?php declare(strict_types=1);

namespace ShopwareLabs\Plugin\SwagBundleExample\Core\Content\Product\Storefront;

use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\Storefront\StorefrontProductRepository;
Expand All @@ -14,6 +12,7 @@ class StorefrontProductRepositoryDecorator extends StorefrontProductRepository
public function read(Criteria $criteria, SalesChannelContext $context): ProductCollection
{
$criteria->addAssociation('bundles', (new Criteria())->addAssociation('products'));

return parent::read($criteria, $context);
}
}
}
Loading

0 comments on commit 428a181

Please sign in to comment.