Skip to content
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions README.md

This file was deleted.

8 changes: 8 additions & 0 deletions bin/doctrine-migrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

namespace Doctrine\Migrations;

require __DIR__ . '/../vendor/doctrine/migrations/bin/doctrine-migrations.php';
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"autoload": {
"psr-4": {
"Light\\App\\": "src/App/src/",
"Light\\Page\\": "src/Page/src/"
"Light\\Page\\": "src/Page/src/",
"Light\\Book\\": "src/Book/src/"
}
},
"autoload-dev": {
Expand Down
18 changes: 18 additions & 0 deletions config/cli-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\Configuration\Migration\ConfigurationArray;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\ORM\EntityManager;

$container = require 'config/container.php';

$entityManager = $container->get(EntityManager::class);
$entityManager->getEventManager();

return DependencyFactory::fromEntityManager(
new ConfigurationArray($container->get('config')['doctrine']['migrations']),
new ExistingEntityManager($entityManager)
);
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// Default App module config
\Light\App\ConfigProvider::class,
\Light\Page\ConfigProvider::class,
\Light\Book\ConfigProvider::class,

// Load application config in a pre-defined order in such a way that local settings
// overwrite global settings. (Loaded as first to last):
Expand Down
Empty file modified data/cache/.gitignore
100755 → 100644
Empty file.
16 changes: 0 additions & 16 deletions mkdocs.yml

This file was deleted.

4 changes: 3 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard" />
<rule ref="LaminasCodingStandard" >
<exclude-pattern>src/Migrations/*</exclude-pattern>
</rule>
</ruleset>
81 changes: 72 additions & 9 deletions src/App/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,72 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Dot\Cache\Adapter\ArrayAdapter;
use Dot\Cache\Adapter\FilesystemAdapter;
use Light\App\Factory\GetIndexViewHandlerFactory;
use Light\App\Handler\GetIndexViewHandler;
use Light\App\Types\UuidType;
use Mezzio\Application;
use Ramsey\Uuid\Doctrine\UuidType;
use Roave\PsrContainerDoctrine\EntityManagerFactory;
use Symfony\Component\Cache\Adapter\AdapterInterface;

use function getcwd;

/**
* @phpstan-type ConfigType array{
* dependencies: DependenciesType,
* doctrine: DoctrineConfigType,
* }
* @phpstan-type DoctrineConfigType array{
* cache: array{
* array: array{
* class: class-string<AdapterInterface>,
* },
* filesystem: array{
* class: class-string<AdapterInterface>,
* directory: non-empty-string,
* namespace: non-empty-string,
* },
* },
* configuration: array{
* orm_default: array{
* result_cache: non-empty-string,
* metadata_cache: non-empty-string,
* query_cache: non-empty-string,
* hydration_cache: non-empty-string,
* typed_field_mapper: non-empty-string|null,
* second_level_cache: array{
* enabled: bool,
* default_lifetime: int,
* default_lock_lifetime: int,
* file_lock_region_directory: string,
* regions: string[],
* },
* },
* },
* driver: array{
* orm_default: array{
* class: class-string<MappingDriver>,
* },
* },
* migrations: array{
* migrations_paths: array<non-empty-string, non-empty-string>,
* all_or_nothing: bool,
* check_database_platform: bool,
* },
* types: array<non-empty-string, class-string>,
* }
* @phpstan-type DependenciesType array{
* factories: array<class-string|non-empty-string, class-string|non-empty-string>,
* aliases: array<class-string|non-empty-string, class-string|non-empty-string>,
* }
**/
class ConfigProvider
{
/**
@return array{
* dependencies: array<mixed>,
* templates: array<mixed>,
* }
* @return ConfigType
*/
public function __invoke(): array
{
Expand All @@ -35,10 +83,7 @@ public function __invoke(): array
}

/**
* @return array{
* delegators: array<class-string, array<class-string>>,
* factories: array<class-string, class-string>,
* }
* @return DependenciesType
*/
public function getDependencies(): array
{
Expand Down Expand Up @@ -81,6 +126,9 @@ public function getTemplates(): array
];
}

/**
* @return DoctrineConfigType
*/
private function getDoctrineConfig(): array
{
return [
Expand Down Expand Up @@ -117,6 +165,21 @@ private function getDoctrineConfig(): array
'class' => MappingDriverChain::class,
],
],
'migrations' => [
'table_storage' => [
'table_name' => 'doctrine_migration_versions',
'version_column_name' => 'version',
'version_column_length' => 191,
'executed_at_column_name' => 'executed_at',
'execution_time_column_name' => 'execution_time',
],
// Modify this line based on where you would like to have you migrations
'migrations_paths' => [
'Migrations' => 'src/Migrations',
],
'all_or_nothing' => true,
'check_database_platform' => true,
],
'types' => [
UuidType::NAME => UuidType::class,
],
Expand Down
106 changes: 106 additions & 0 deletions src/App/src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Light\App\Entity;

use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Laminas\Stdlib\ArraySerializableInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

use function is_array;
use function method_exists;
use function ucfirst;

#[ORM\MappedSuperclass]
abstract class AbstractEntity implements ArraySerializableInterface
{
#[ORM\Column(name: 'created', type: 'datetime_immutable', nullable: false)]
protected DateTimeImmutable $created;

#[ORM\Column(name: 'updated', type: 'datetime_immutable', nullable: true)]
protected ?DateTimeImmutable $updated = null;

#[ORM\Id]
#[ORM\Column(name: 'id', type: 'uuid', unique: true, nullable: false)]
protected UuidInterface $uuid;

#[ORM\PrePersist]
public function created(): void
{
$this->created = new DateTimeImmutable();
}

#[ORM\PreUpdate]
public function touch(): void
{
$this->updated = new DateTimeImmutable();
}

public function __construct()
{
$this->uuid = Uuid::uuid7();
}

public function getId(): UuidInterface
{
return $this->uuid;
}

public function setId(UuidInterface $id): static
{
$this->uuid = $id;

return $this;
}

public function getCreated(): ?DateTimeImmutable
{
return $this->created;
}

public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string
{
return $this->created->format($dateFormat);
}

public function getUpdated(): ?DateTimeImmutable
{
return $this->updated;
}

public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string
{
if ($this->updated instanceof DateTimeImmutable) {
return $this->updated->format($dateFormat);
}

return null;
}

/**
* @param array<non-empty-string, mixed> $array
*/
public function exchangeArray(array $array): void
{
foreach ($array as $property => $values) {
if (is_array($values)) {
$method = 'add' . ucfirst($property);
if (! method_exists($this, $method)) {
continue;
}
foreach ($values as $value) {
$this->$method($value);
}
} else {
$method = 'set' . ucfirst($property);
if (! method_exists($this, $method)) {
continue;
}
$this->$method($values);
}
}
}
}
21 changes: 21 additions & 0 deletions src/App/src/Entity/EntityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Light\App\Entity;

use DateTimeImmutable;
use Ramsey\Uuid\UuidInterface;

interface EntityInterface
{
public function getId(): UuidInterface;

public function getCreated(): ?DateTimeImmutable;

public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string;

public function getUpdated(): ?DateTimeImmutable;

public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string;
}
32 changes: 32 additions & 0 deletions src/App/src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Light\App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Light\App\Entity\EntityInterface;

/**
* @extends EntityRepository<object>
*/
class AbstractRepository extends EntityRepository
{
public function deleteResource(EntityInterface $resource): void
{
$this->getEntityManager()->remove($resource);
$this->getEntityManager()->flush();
}

public function getQueryBuilder(): QueryBuilder
{
return $this->getEntityManager()->createQueryBuilder();
}

public function saveResource(EntityInterface $resource): void
{
$this->getEntityManager()->persist($resource);
$this->getEntityManager()->flush();
}
}
Loading