Skip to content

Commit

Permalink
NEXT-31522 - Allow to define entities over php attribute directly at …
Browse files Browse the repository at this point in the history
…the entity class instead of defining an definition
  • Loading branch information
OliverSkroblin committed May 21, 2024
1 parent 1d0db0f commit 18f6b42
Show file tree
Hide file tree
Showing 41 changed files with 968 additions and 53 deletions.
11 changes: 8 additions & 3 deletions custom/scripts/boot/boot.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Scripts\Boot\ScriptKernel;
use Shopware\Core\Framework\Adapter\Database\MySQLFactory;
use Shopware\Core\Framework\Adapter\Kernel\KernelFactory;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\StaticKernelPluginLoader;
use Shopware\Core\HttpKernel;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpKernel\KernelInterface;

Expand All @@ -21,11 +21,16 @@

/** @var KernelInterface $kernel */
KernelFactory::$kernelClass = ScriptKernel::class;

$kernel = KernelFactory::create(
environment: $env,
debug: true,
classLoader: $classLoader,
pluginLoader: new StaticKernelPluginLoader($classLoader)
pluginLoader: new DbalKernelPluginLoader(
classLoader: $classLoader,
pluginDir: $projectRoot . '/custom/plugins',
connection: MySQLFactory::create([])
)
);

$kernel->boot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Shopware\Core\Content\Flow\DataAbstractionLayer\FieldSerializer;

use Shopware\Core\Content\Flow\DataAbstractionLayer\Field\FlowTemplateConfigField;
use Shopware\Core\Framework\DataAbstractionLayer\DataAbstractionLayerException;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
use Shopware\Core\Framework\DataAbstractionLayer\Write\DataStack\KeyValuePair;
Expand All @@ -29,10 +27,6 @@ public function encode(
KeyValuePair $data,
WriteParameterBag $parameters
): \Generator {
if (!$field instanceof FlowTemplateConfigField) {
throw DataAbstractionLayerException::invalidSerializerField(FlowTemplateConfigField::class, $field);
}

$this->validateIfNeeded($field, $existence, $data, $parameters);

$value = $data->getValue();
Expand Down
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/CustomFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class CustomFields extends Field
{
public const TYPE = 'custom-fields';

public function __construct(public bool $translated = false)
{
parent::__construct(type: self::TYPE, translated: $this->translated, api: true);
}
}
16 changes: 16 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class Entity
{
/**
* @var class-string
*/
public string $class;

public function __construct(public string $name, public ?string $parent = null)
{
}
}
19 changes: 19 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Field
{
public bool $nullable;

/**
* @param bool|array{admin-api: bool, store-api: bool} $api
*/
public function __construct(
public string $type,
public bool $translated = false,
public bool|array $api = false
) {
}
}
24 changes: 24 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/FieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

enum FieldType: string
{
public const STRING = 'string';
public const TEXT = 'text';
public const INT = 'int';
public const FLOAT = 'float';
public const BOOL = 'bool';
public const DATETIME = 'datetime';
public const UUID = 'uuid';
public const AUTO_INCREMENT = 'auto-increment';
public const JSON = 'json';
public const DATE = 'date';
public const DATE_INTERVAL = 'date-interval';
public const TIME_ZONE = 'time-zone';

public const EMAIL = 'email';
public const LIST = 'list';
public const PASSWORD = 'password';
public const REMOTE_ADDRESS = 'remote-address';
}
16 changes: 16 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/ForeignKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ForeignKey extends Field
{
public const TYPE = 'fk';

public bool $nullable;

public function __construct(public string $entity, public bool|array $api = false)
{
parent::__construct(type: self::TYPE, api: $api);
}
}
11 changes: 11 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Inherited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Inherited
{
public function __construct(public bool $reversed = false)
{
}
}
17 changes: 17 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/ManyToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ManyToMany extends Field
{
public const TYPE = 'many-to-many';

public function __construct(
public string $entity,
public OnDelete $onDelete = OnDelete::NO_ACTION,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
18 changes: 18 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/ManyToOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ManyToOne extends Field
{
public const TYPE = 'many-to-one';

public function __construct(
public string $entity,
public OnDelete $onDelete = OnDelete::NO_ACTION,
public string $ref = 'id',
public bool|array $api = false,
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
11 changes: 11 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OnDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

enum OnDelete: string
{
case CASCADE = 'CASCADE';
case SET_NULL = 'SET NULL';
case RESTRICT = 'RESTRICT';
case NO_ACTION = 'NO ACTION';
}
18 changes: 18 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OneToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToMany extends Field
{
public const TYPE = 'one-to-many';

public function __construct(
public string $entity,
public string $ref,
public OnDelete $onDelete = OnDelete::NO_ACTION,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
19 changes: 19 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OneToOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToOne extends Field
{
public const TYPE = 'one-to-one';

public function __construct(
public string $entity,
public ?string $column = null,
public OnDelete $onDelete = OnDelete::NO_ACTION,
public string $ref = 'id',
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
11 changes: 11 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/PrimaryKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PrimaryKey
{
public function __construct()
{
}
}
17 changes: 17 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Protection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

use Shopware\Core\Framework\Context;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Protection
{
final public const SYSTEM_SCOPE = Context::SYSTEM_SCOPE;
final public const USER_SCOPE = Context::USER_SCOPE;
final public const CRUD_API_SCOPE = Context::CRUD_API_SCOPE;

public function __construct(public array $write)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ReferenceVersion extends Field
{
public const TYPE = 'reference-version';

public function __construct(public string $entity)
{
parent::__construct(type: self::TYPE, api: true);
}
}
11 changes: 11 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Required.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Required
{
public function __construct()
{
}
}
18 changes: 18 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Serialized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\StringFieldSerializer;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Serialized extends Field
{
public const TYPE = 'serialized';

public function __construct(
public string $serializer = StringFieldSerializer::class,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Translations extends Field
{
public const TYPE = 'translations';

public function __construct()
{
parent::__construct(type: self::TYPE, api: true);
}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Version extends Field
{
public const TYPE = 'version';

public function __construct()
{
parent::__construct(type: self::TYPE, api: true);
}
}
Loading

0 comments on commit 18f6b42

Please sign in to comment.