-
Notifications
You must be signed in to change notification settings - Fork 978
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEXT-0000 - POC php attributes for entities
- Loading branch information
1 parent
4a2e40f
commit fbab73c
Showing
12 changed files
with
279 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Scripts\Examples; | ||
|
||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\Uuid\Uuid; | ||
|
||
require_once __DIR__ . '/examples/base-script.php'; | ||
|
||
$env = 'dev'; // by default, kernel gets booted in dev | ||
|
||
$kernel = require __DIR__ . '/boot/boot.php'; | ||
|
||
class Main extends BaseScript | ||
{ | ||
public function run() | ||
{ | ||
$definition = $this->getContainer()->get('my_entity.definition'); | ||
|
||
/** @var EntityRepository $repo */ | ||
$repo = $this->getContainer()->get('my_entity.repository'); | ||
|
||
$data = [ | ||
['id' => Uuid::randomHex(), 'name' => 'foo'] | ||
]; | ||
|
||
$repo->upsert($data, Context::createCLIContext()); | ||
|
||
$entities = $repo->search(new Criteria(), Context::createCLIContext()); | ||
} | ||
} | ||
|
||
|
||
(new Main($kernel))->run(); |
15 changes: 15 additions & 0 deletions
15
src/Core/Framework/DataAbstractionLayer/Attribute/Entity.php
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,15 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class Entity | ||
{ | ||
/** | ||
* @var class-string | ||
*/ | ||
public string $class; | ||
|
||
public function __construct(public string $name) {} | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
src/Core/Framework/DataAbstractionLayer/Attribute/Field.php
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,16 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class Field | ||
{ | ||
public bool $nullable; | ||
|
||
public function __construct( | ||
public string $type, | ||
public bool $translated = false, | ||
public string $name = '', | ||
public bool $searchable = false | ||
) {} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Core/Framework/DataAbstractionLayer/Attribute/Primary.php
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,9 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class Primary | ||
{ | ||
public function __construct(public string $type, public string $name = '') {} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Core/Framework/DataAbstractionLayer/AttributeEntityDefinition.php
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,52 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\DataAbstractionLayer; | ||
|
||
use Shopware\Core\Framework\DataAbstractionLayer\Field\Field; | ||
|
||
class AttributeEntityDefinition extends EntityDefinition | ||
{ | ||
public function __construct(private readonly array $meta = []) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function getEntityClass(): string | ||
{ | ||
return $this->meta['entity_class']; | ||
} | ||
|
||
public function getEntityName(): string | ||
{ | ||
return $this->meta['entity_name']; | ||
} | ||
|
||
public function getDefaults(): array | ||
{ | ||
// todo add defaults support | ||
return parent::getDefaults(); // TODO: Change the autogenerated stub | ||
} | ||
|
||
protected function getParentDefinitionClass(): ?string | ||
{ | ||
// todo add `entity_name` support | ||
return parent::getParentDefinitionClass(); | ||
} | ||
|
||
protected function defineFields(): FieldCollection | ||
{ | ||
$fields = []; | ||
foreach ($this->meta['fields'] as $field) { | ||
/** @var Field $instance */ | ||
$instance = new $field['class'](...$field['args']); | ||
|
||
foreach ($field['flags'] ?? [] as $flag) { | ||
$instance->addFlags(new $flag['class'](...$flag['args'] ?? [])); | ||
} | ||
|
||
$fields[] = $instance; | ||
} | ||
|
||
return new FieldCollection($fields); | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
src/Core/Framework/DependencyInjection/CompilerPass/AttributeEntityCompilerPass.php
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,85 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\DependencyInjection\CompilerPass; | ||
|
||
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Entity; | ||
use Shopware\Core\Framework\DataAbstractionLayer\AttributeEntityDefinition; | ||
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\VersionManager; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class AttributeEntityCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$services = $container->findTaggedServiceIds('shopware.entity'); | ||
|
||
foreach ($services as $class => $_) { | ||
$reflection = new \ReflectionClass($class); | ||
|
||
$collection = $reflection->getAttributes(Entity::class); | ||
|
||
/** @var Entity $instance */ | ||
$instance = $collection[0]->newInstance(); | ||
|
||
$meta = [ | ||
'entity_class' => $class, | ||
'entity_name' => $instance->name, | ||
'fields' => $this->parse($reflection) | ||
]; | ||
|
||
$definition = new Definition(AttributeEntityDefinition::class); | ||
$definition->addArgument($meta); | ||
$definition->setPublic(true); | ||
$container->setDefinition($instance->name . '.definition', $definition); | ||
|
||
$repository = new Definition( | ||
EntityRepository::class, | ||
[ | ||
new Reference($instance->name . '.definition'), | ||
new Reference(EntityReaderInterface::class), | ||
new Reference(VersionManager::class), | ||
new Reference(EntitySearcherInterface::class), | ||
new Reference(EntityAggregatorInterface::class), | ||
new Reference('event_dispatcher'), | ||
new Reference(EntityLoadedEventFactory::class), | ||
] | ||
); | ||
$repository->setPublic(true); | ||
|
||
$container->setDefinition($instance->name . '.repository', $repository); | ||
|
||
$registry = $container->getDefinition(DefinitionInstanceRegistry::class); | ||
$registry->addMethodCall('register', [new Reference($instance->name . '.definition'), $instance->name . '.definition']); | ||
} | ||
} | ||
|
||
private function parse(\ReflectionClass $reflection): array | ||
{ | ||
$fields = [ | ||
'id' => [ | ||
'class' => IdField::class, | ||
'args' => ['storageName' => 'id', 'propertyName' => 'id'], | ||
'flags' => [ | ||
['class' => PrimaryKey::class], | ||
['class' => ApiAware::class], | ||
['class' => Required::class], | ||
] | ||
] | ||
]; | ||
|
||
return $fields; | ||
} | ||
} |
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
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
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,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Migration\V6_6; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\Framework\Migration\MigrationStep; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Package('core')] | ||
class Migration1714134471MyEntity extends MigrationStep | ||
{ | ||
public function getCreationTimestamp(): int | ||
{ | ||
return 1714134471; | ||
} | ||
|
||
public function update(Connection $connection): void | ||
{ | ||
$connection->executeStatement( | ||
'CREATE TABLE IF NOT EXISTS my_entity ( | ||
id BINARY(16) NOT NULL, | ||
name VARCHAR(255) NULL, | ||
created_at DATETIME(3) NOT NULL, | ||
updated_at DATETIME(3) NULL, | ||
PRIMARY KEY (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;' | ||
); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\System\Currency; | ||
|
||
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Entity; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Field; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Primary; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Entity as EntityStruct; | ||
|
||
#[Entity(name: 'my_entity')] | ||
class MyEntity extends EntityStruct | ||
{ | ||
#[Primary(type: 'uuid')] | ||
public string $id; | ||
|
||
#[Field(type: 'string')] | ||
public string $name; | ||
} |
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