From 96350c8b8f5737aadd1b77b6421d64a603f50734 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 29 Jan 2020 10:06:42 +0100 Subject: [PATCH] Updated entities mapping config so that they return a function --- composer.json | 2 +- config/autoload/entity-manager.global.php | 1 + .../Shlinkio.Shlink.Core.Entity.Domain.php | 27 +++--- .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 97 ++++++++++--------- .../Shlinkio.Shlink.Core.Entity.Tag.php | 29 +++--- .../Shlinkio.Shlink.Core.Entity.Visit.php | 71 +++++++------- ...inkio.Shlink.Core.Entity.VisitLocation.php | 65 +++++++------ .../Shlinkio.Shlink.Rest.Entity.ApiKey.php | 39 ++++---- 8 files changed, 169 insertions(+), 162 deletions(-) diff --git a/composer.json b/composer.json index b4189a29..908b9af3 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "phly/phly-event-dispatcher": "^1.0", "predis/predis": "^1.1", "pugx/shortid-php": "^0.5", - "shlinkio/shlink-common": "^2.6.0", + "shlinkio/shlink-common": "^2.7.0", "shlinkio/shlink-event-dispatcher": "^1.3", "shlinkio/shlink-installer": "^4.0.1", "shlinkio/shlink-ip-geolocation": "^1.3.1", diff --git a/config/autoload/entity-manager.global.php b/config/autoload/entity-manager.global.php index 561579f1..c08f66f2 100644 --- a/config/autoload/entity-manager.global.php +++ b/config/autoload/entity-manager.global.php @@ -9,6 +9,7 @@ return [ 'entity_manager' => [ 'orm' => [ 'proxies_dir' => 'data/proxies', + 'load_mappings_using_functional_style' => true, ], 'connection' => [ 'user' => '', diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php index de7252ee..1c751d17 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php @@ -6,20 +6,21 @@ namespace Shlinkio\Shlink\Core; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('domains'); + $builder->setTable('domains'); -$builder->createField('id', Types::BIGINT) - ->columnName('id') - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); + $builder->createField('id', Types::BIGINT) + ->columnName('id') + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) + ->build(); -$builder->createField('authority', Types::STRING) - ->unique() - ->build(); + $builder->createField('authority', Types::STRING) + ->unique() + ->build(); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index 0d24d555..a8bcf7ea 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -6,65 +6,66 @@ namespace Shlinkio\Shlink\Core; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('short_urls') - ->setCustomRepositoryClass(Repository\ShortUrlRepository::class); + $builder->setTable('short_urls') + ->setCustomRepositoryClass(Repository\ShortUrlRepository::class); -$builder->createField('id', Types::BIGINT) - ->columnName('id') - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); + $builder->createField('id', Types::BIGINT) + ->columnName('id') + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) + ->build(); -$builder->createField('longUrl', Types::STRING) - ->columnName('original_url') - ->length(2048) - ->build(); + $builder->createField('longUrl', Types::STRING) + ->columnName('original_url') + ->length(2048) + ->build(); -$builder->createField('shortCode', Types::STRING) - ->columnName('short_code') - ->length(255) - ->build(); + $builder->createField('shortCode', Types::STRING) + ->columnName('short_code') + ->length(255) + ->build(); -$builder->createField('dateCreated', ChronosDateTimeType::CHRONOS_DATETIME) - ->columnName('date_created') - ->build(); + $builder->createField('dateCreated', ChronosDateTimeType::CHRONOS_DATETIME) + ->columnName('date_created') + ->build(); -$builder->createField('validSince', ChronosDateTimeType::CHRONOS_DATETIME) - ->columnName('valid_since') - ->nullable() - ->build(); + $builder->createField('validSince', ChronosDateTimeType::CHRONOS_DATETIME) + ->columnName('valid_since') + ->nullable() + ->build(); -$builder->createField('validUntil', ChronosDateTimeType::CHRONOS_DATETIME) - ->columnName('valid_until') - ->nullable() - ->build(); + $builder->createField('validUntil', ChronosDateTimeType::CHRONOS_DATETIME) + ->columnName('valid_until') + ->nullable() + ->build(); -$builder->createField('maxVisits', Types::INTEGER) - ->columnName('max_visits') - ->nullable() - ->build(); + $builder->createField('maxVisits', Types::INTEGER) + ->columnName('max_visits') + ->nullable() + ->build(); -$builder->createOneToMany('visits', Entity\Visit::class) - ->mappedBy('shortUrl') - ->fetchExtraLazy() - ->build(); + $builder->createOneToMany('visits', Entity\Visit::class) + ->mappedBy('shortUrl') + ->fetchExtraLazy() + ->build(); -$builder->createManyToMany('tags', Entity\Tag::class) - ->setJoinTable('short_urls_in_tags') - ->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE') - ->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE') - ->build(); + $builder->createManyToMany('tags', Entity\Tag::class) + ->setJoinTable('short_urls_in_tags') + ->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE') + ->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE') + ->build(); -$builder->createManyToOne('domain', Entity\Domain::class) - ->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT') - ->cascadePersist() - ->build(); + $builder->createManyToOne('domain', Entity\Domain::class) + ->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT') + ->cascadePersist() + ->build(); -$builder->addUniqueConstraint(['short_code', 'domain_id'], 'unique_short_code_plus_domain'); + $builder->addUniqueConstraint(['short_code', 'domain_id'], 'unique_short_code_plus_domain'); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php index 09a98151..7d310eb8 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php @@ -6,21 +6,22 @@ namespace Shlinkio\Shlink\Core; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('tags') - ->setCustomRepositoryClass(Repository\TagRepository::class); + $builder->setTable('tags') + ->setCustomRepositoryClass(Repository\TagRepository::class); -$builder->createField('id', Types::BIGINT) - ->columnName('id') - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); + $builder->createField('id', Types::BIGINT) + ->columnName('id') + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) + ->build(); -$builder->createField('name', Types::STRING) - ->unique() - ->build(); + $builder->createField('name', Types::STRING) + ->unique() + ->build(); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php index 6770f9d3..5d8f6ff4 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php @@ -6,49 +6,50 @@ namespace Shlinkio\Shlink\Core; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; use Shlinkio\Shlink\Core\Model\Visitor; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('visits') - ->setCustomRepositoryClass(Repository\VisitRepository::class); + $builder->setTable('visits') + ->setCustomRepositoryClass(Repository\VisitRepository::class); -$builder->createField('id', Types::BIGINT) - ->columnName('id') - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); + $builder->createField('id', Types::BIGINT) + ->columnName('id') + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) + ->build(); -$builder->createField('referer', Types::STRING) - ->nullable() - ->length(Visitor::REFERER_MAX_LENGTH) - ->build(); + $builder->createField('referer', Types::STRING) + ->nullable() + ->length(Visitor::REFERER_MAX_LENGTH) + ->build(); -$builder->createField('date', ChronosDateTimeType::CHRONOS_DATETIME) - ->columnName('`date`') - ->build(); + $builder->createField('date', ChronosDateTimeType::CHRONOS_DATETIME) + ->columnName('`date`') + ->build(); -$builder->createField('remoteAddr', Types::STRING) - ->columnName('remote_addr') - ->length(Visitor::REMOTE_ADDRESS_MAX_LENGTH) - ->nullable() - ->build(); + $builder->createField('remoteAddr', Types::STRING) + ->columnName('remote_addr') + ->length(Visitor::REMOTE_ADDRESS_MAX_LENGTH) + ->nullable() + ->build(); -$builder->createField('userAgent', Types::STRING) - ->columnName('user_agent') - ->length(Visitor::USER_AGENT_MAX_LENGTH) - ->nullable() - ->build(); + $builder->createField('userAgent', Types::STRING) + ->columnName('user_agent') + ->length(Visitor::USER_AGENT_MAX_LENGTH) + ->nullable() + ->build(); -$builder->createManyToOne('shortUrl', Entity\ShortUrl::class) - ->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE') - ->build(); + $builder->createManyToOne('shortUrl', Entity\ShortUrl::class) + ->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE') + ->build(); -$builder->createManyToOne('visitLocation', Entity\VisitLocation::class) - ->addJoinColumn('visit_location_id', 'id', true, false, 'Set NULL') - ->cascadePersist() - ->build(); + $builder->createManyToOne('visitLocation', Entity\VisitLocation::class) + ->addJoinColumn('visit_location_id', 'id', true, false, 'Set NULL') + ->cascadePersist() + ->build(); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php index 117c2acc..b9652a08 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php @@ -6,41 +6,42 @@ namespace Shlinkio\Shlink\Core; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('visit_locations'); + $builder->setTable('visit_locations'); -$builder->createField('id', Types::BIGINT) - ->columnName('id') - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); - -$columns = [ - 'country_code' => 'countryCode', - 'country_name' => 'countryName', - 'region_name' => 'regionName', - 'city_name' => 'cityName', - 'timezone' => 'timezone', -]; - -foreach ($columns as $columnName => $fieldName) { - $builder->createField($fieldName, Types::STRING) - ->columnName($columnName) - ->nullable() + $builder->createField('id', Types::BIGINT) + ->columnName('id') + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) ->build(); -} -$builder->createField('latitude', Types::FLOAT) - ->columnName('lat') - ->nullable(false) - ->build(); + $columns = [ + 'country_code' => 'countryCode', + 'country_name' => 'countryName', + 'region_name' => 'regionName', + 'city_name' => 'cityName', + 'timezone' => 'timezone', + ]; -$builder->createField('longitude', Types::FLOAT) - ->columnName('lon') - ->nullable(false) - ->build(); + foreach ($columns as $columnName => $fieldName) { + $builder->createField($fieldName, Types::STRING) + ->columnName($columnName) + ->nullable() + ->build(); + } + + $builder->createField('latitude', Types::FLOAT) + ->columnName('lat') + ->nullable(false) + ->build(); + + $builder->createField('longitude', Types::FLOAT) + ->columnName('lon') + ->nullable(false) + ->build(); +}; diff --git a/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php b/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php index 5bc5aec9..a79bd642 100644 --- a/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php +++ b/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php @@ -6,29 +6,30 @@ namespace Shlinkio\Shlink\Rest; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; -use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine +use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; -/** @var $metadata ClassMetadata */ // @codingStandardsIgnoreLine -$builder = new ClassMetadataBuilder($metadata); +return static function (ClassMetadata $metadata): void { + $builder = new ClassMetadataBuilder($metadata); -$builder->setTable('api_keys'); + $builder->setTable('api_keys'); -$builder->createField('id', Types::BIGINT) - ->makePrimaryKey() - ->generatedValue('IDENTITY') - ->option('unsigned', true) - ->build(); + $builder->createField('id', Types::BIGINT) + ->makePrimaryKey() + ->generatedValue('IDENTITY') + ->option('unsigned', true) + ->build(); -$builder->createField('key', Types::STRING) - ->columnName('`key`') - ->unique() - ->build(); + $builder->createField('key', Types::STRING) + ->columnName('`key`') + ->unique() + ->build(); -$builder->createField('expirationDate', ChronosDateTimeType::CHRONOS_DATETIME) - ->columnName('expiration_date') - ->nullable() - ->build(); + $builder->createField('expirationDate', ChronosDateTimeType::CHRONOS_DATETIME) + ->columnName('expiration_date') + ->nullable() + ->build(); -$builder->createField('enabled', Types::BOOLEAN) - ->build(); + $builder->createField('enabled', Types::BOOLEAN) + ->build(); +};