Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
1. Updates codebase up to PHP 8.2 using rector
2. Fixes database migrations #133
3. Runs migrations on server startup, when persistence driver `database` is used #133
4. Moves Dockerfile from `docker` -> `.docker`
5. Moves installation some PHP ext from repository `Dockerfile` to `buggregator/docker` repository to reduce time for docker image build
6. Improves `docker-compose.yaml`. Adds `mongodb`, `postgres` containers for testing connection
7. Adds missed `mongodb/mongodb` package #132
  • Loading branch information
butschster committed Apr 14, 2024
1 parent d533cba commit 2d54767
Show file tree
Hide file tree
Showing 74 changed files with 617 additions and 319 deletions.
18 changes: 1 addition & 17 deletions docker/Dockerfile → .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ RUN git clone -b $BRANCH $REPOSITORY /app
# Configure PHP project
FROM ghcr.io/buggregator/docker:latest as backend

RUN apk update

RUN rm -rf /var/lib/apt/lists/*

RUN apk --no-cache add postgresql-dev

RUN docker-php-ext-install \
pgsql pdo_pgsql pdo_mysql

COPY --from=git /app /app
COPY --from=frontend /app /app/frontend
COPY --from=rr /usr/bin/rr /app
Expand All @@ -35,22 +26,15 @@ COPY --from=centrifugo /usr/local/bin/centrifugo /app/bin
ARG APP_VERSION=v1.0
ENV COMPOSER_ALLOW_SUPERUSER=1

RUN sed -i 's/memory_limit = 128M/memory_limit = 1024M/g' "$PHP_INI_DIR/php.ini-production" && \
sed -i 's/post_max_size = 8M/post_max_size = 1024M/g' "$PHP_INI_DIR/php.ini-production" && \
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

WORKDIR /app

RUN composer config --no-plugins allow-plugins.spiral/composer-publish-plugin false
RUN composer install --no-dev

RUN docker-php-source delete \
&& apk del ${BUILD_DEPENDS}

WORKDIR /app
RUN php app.php configure

EXPOSE 8000/tcp
EXPOSE 8082/tcp

ENV APP_VERSION=$APP_VERSION
ENV RR_LOG_LEVEL=error
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-dev-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
with:
context: ./
platforms: linux/amd64,linux/arm64
file: ./docker/Dockerfile
file: ./.docker/Dockerfile
push: true
build-args: |
APP_VERSION=${{ steps.previoustag.outputs.tag }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
with:
context: ./
platforms: linux/amd64,linux/arm64
file: ./docker/Dockerfile
file: ./.docker/Dockerfile
push: true
build-args: |
APP_VERSION=${{ steps.previoustag.outputs.tag }}
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
with:
context: ./
platforms: linux/amd64,linux/arm64
file: ./docker/Dockerfile
file: ./.docker/Dockerfile
push: true
build-args:
APP_VERSION=${{ steps.previoustag.outputs.tag }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2']
['8.2', '8.3']
stability: >-
['prefer-stable']
10 changes: 4 additions & 6 deletions app/config/cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
'schema' => [
'cache' => env('CYCLE_SCHEMA_CACHE', true),
'defaults' => [
// SchemaInterface::MAPPER => \Cycle\ORM\Mapper\Mapper::class,
// SchemaInterface::REPOSITORY => \Cycle\ORM\Select\Repository::class,
// SchemaInterface::SCOPE => null,
// SchemaInterface::TYPECAST_HANDLER => [
// \Cycle\ORM\Parser\Typecast::class
// ],
\Cycle\ORM\SchemaInterface::TYPECAST_HANDLER => [
\Cycle\ORM\Parser\Typecast::class,
\App\Application\Domain\Entity\ExtendedTypecast::class,
],
],
'collections' => [
'default' => 'doctrine',
Expand Down
2 changes: 1 addition & 1 deletion app/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
connection: new Config\MySQL\TcpConnectionConfig(
database: env('DB_DATABASE', 'buggregator'),
host: env('DB_HOST', '127.0.0.1'),
port: env('DB_PORT', 5432),
port: env('DB_PORT', 3306),
user: env('DB_USERNAME', 'postgres'),
password: env('DB_PASSWORD'),
),
Expand Down
4 changes: 3 additions & 1 deletion app/config/migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*/
'table' => 'migrations',

'strategy' => \Cycle\Schema\Generator\Migrations\Strategy\MultipleFilesStrategy::class,

/**
* When set to true no confirmation will be requested on migration run.
*/
'safe' => env('APP_ENV') === 'local',
'safe' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

use Cycle\Migrations\Migration;

class OrmDefaultA580ac210e75d2560aaa96f96a407702 extends Migration
class OrmDefault1df1e263c7c7d0a163f630de97e0b78c extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('events')
->addColumn('uuid', 'string', ['nullable' => false, 'default' => null])
->addColumn('type', 'string', ['nullable' => false, 'default' => null])
->addColumn('payload', 'longText', ['nullable' => false, 'default' => null])
->addColumn('timestamp', 'float', ['nullable' => false, 'default' => null])
->addColumn('project_id', 'integer', ['nullable' => true, 'default' => null])
->addColumn('uuid', 'string', ['nullable' => false, 'size' => 36])
->addColumn('type', 'string', ['nullable' => false, 'size' => 50])
->addColumn('payload', 'jsonb', ['nullable' => false])
->addColumn('timestamp', 'float', ['nullable' => false])
->addColumn('project_id', 'integer', ['nullable' => true])
->setPrimaryKeys(['uuid'])
->create();
}
Expand Down
4 changes: 2 additions & 2 deletions app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function __construct(
#[Column(type: 'string(36)', primary: true, typecast: 'uuid')]
private Uuid $uuid,

#[Column(type: 'string')]
#[Column(type: 'string(50)')]
private string $type,

#[Column(type: 'longText', typecast: 'json')]
#[Column(type: 'jsonb', typecast: Json::class)]
private Json $payload,

#[Column(type: 'float')]
Expand Down
12 changes: 6 additions & 6 deletions app/modules/Events/Domain/Events/EventWasReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use App\Application\Broadcasting\ShouldBroadcastInterface;
use App\Application\Domain\ValueObjects\Uuid;

final class EventWasReceived implements ShouldBroadcastInterface
final readonly class EventWasReceived implements ShouldBroadcastInterface
{
public function __construct(
public readonly Uuid $uuid,
public readonly string $type,
public readonly array $payload,
public readonly float $timestamp,
public readonly ?int $projectId = null,
public Uuid $uuid,
public string $type,
public array $payload,
public float $timestamp,
public ?int $projectId = null,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions app/modules/Events/Interfaces/Commands/ClearEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Cqrs\Attribute\CommandHandler;

final class ClearEventsHandler
final readonly class ClearEventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $events,
private readonly EventDispatcherInterface $dispatcher,
private EventRepositoryInterface $events,
private EventDispatcherInterface $dispatcher,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions app/modules/Events/Interfaces/Commands/DeleteEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Cqrs\Attribute\CommandHandler;

final class DeleteEventHandler
final readonly class DeleteEventHandler
{
public function __construct(
private readonly EventRepositoryInterface $events,
private readonly EventDispatcherInterface $dispatcher
private EventRepositoryInterface $events,
private EventDispatcherInterface $dispatcher
) {
}

Expand Down
8 changes: 4 additions & 4 deletions app/modules/Events/Interfaces/Commands/StoreEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use Spiral\Cqrs\Attribute\CommandHandler;
use Spiral\Cqrs\QueryBusInterface;

final class StoreEventHandler
final readonly class StoreEventHandler
{
public function __construct(
private readonly EventDispatcherInterface $dispatcher,
private readonly EventRepositoryInterface $events,
private readonly QueryBusInterface $queryBus,
private EventDispatcherInterface $dispatcher,
private EventRepositoryInterface $events,
private QueryBusInterface $queryBus,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
use Modules\Events\Domain\EventRepositoryInterface;
use Spiral\Cqrs\Attribute\QueryHandler;

final class FindEventByUuidHandler
final readonly class FindEventByUuidHandler
{
public function __construct(
private readonly EventRepositoryInterface $events
private EventRepositoryInterface $events
) {
}

#[QueryHandler]
public function __invoke(FindEventByUuid $query): Event
{
$event = $this->events->findByPK((string)$query->uuid);
if (!$event) {
if ($event === null) {
throw new EntityNotFoundException(
\sprintf('Event with given uuid [%s] was not found.', (string)$query->uuid)
);
Expand Down
7 changes: 4 additions & 3 deletions app/modules/Events/Interfaces/Queries/FindEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
final class FindEventsHandler extends EventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $events
private readonly EventRepositoryInterface $events,
) {
}

#[QueryHandler]
public function __invoke(FindEvents $query): iterable
{
return $this->events->findAll(
self::getScopeFromFindEvents($query),
['date' => 'desc']
scope: self::getScopeFromFindEvents($query),
orderBy: ['timestamp' => 'desc'],
limit: $query->limit,
);
}
}
6 changes: 3 additions & 3 deletions app/modules/HttpDumps/Application/HttpDumpsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ final class HttpDumpsBootloader extends Bootloader
public function defineSingletons(): array
{
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, []);
},
EventHandlerInterface::class => static fn(
ContainerInterface $container,
): EventHandlerInterface => new EventHandler($container, []),
];
}
}
7 changes: 3 additions & 4 deletions app/modules/HttpDumps/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Modules\HttpDumps;

use Modules\HttpDumps\Application;
use Psr\Container\ContainerInterface;

final class EventHandler implements Application\EventHandlerInterface
final readonly class EventHandler implements Application\EventHandlerInterface
{
/**
* @param class-string<\Modules\Sentry\Application\EventHandlerInterface>[] $handlers
*/
public function __construct(
private readonly ContainerInterface $container,
private readonly array $handlers
private ContainerInterface $container,
private array $handlers,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use Spiral\Http\Exception\ClientException;
use Spiral\Http\ResponseWrapper;

final class EventHandler implements HandlerInterface
final readonly class EventHandler implements HandlerInterface
{
public function __construct(
private readonly ResponseWrapper $responseWrapper,
private readonly CommandBusInterface $commands,
private ResponseWrapper $responseWrapper,
private CommandBusInterface $commands,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions app/modules/Monolog/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
use Spiral\RoadRunnerBridge\Tcp\Response\ResponseInterface;
use Spiral\RoadRunnerBridge\Tcp\Service\ServiceInterface;

final class Service implements ServiceInterface
final readonly class Service implements ServiceInterface
{
public function __construct(
private readonly CommandBusInterface $commandBus,
private readonly LoggerInterface $logger,
private CommandBusInterface $commandBus,
private LoggerInterface $logger,
) {
}

Expand All @@ -43,7 +43,7 @@ public function handle(Request $request): ResponseInterface
payload: $payload,
),
);
} catch (\JsonException $e) {
} catch (\JsonException) {
// Impossible to decode the message, give up.
$this->logger->error("Unable to decode log message. Should be a valid JSON.", [
'message' => $message,
Expand Down
16 changes: 8 additions & 8 deletions app/modules/Profiler/Application/ProfilerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ final class ProfilerBootloader extends Bootloader
public function defineSingletons(): array
{
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, [
PreparePeaks::class,
CalculateDiffsBetweenEdges::class,
PrepareEdges::class,
CleanupEvent::class,
]);
},
EventHandlerInterface::class => static fn(
ContainerInterface $container,
): EventHandlerInterface => new EventHandler($container, [
PreparePeaks::class,
CalculateDiffsBetweenEdges::class,
PrepareEdges::class,
CleanupEvent::class,
]),
];
}
}
8 changes: 4 additions & 4 deletions app/modules/Profiler/Interfaces/Http/Handler/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use Spiral\Cqrs\CommandBusInterface;
use Spiral\Http\ResponseWrapper;

final class EventHandler implements HandlerInterface
final readonly class EventHandler implements HandlerInterface
{
public function __construct(
private readonly ResponseWrapper $responseWrapper,
private readonly EventHandlerInterface $handler,
private readonly CommandBusInterface $commands,
private ResponseWrapper $responseWrapper,
private EventHandlerInterface $handler,
private CommandBusInterface $commands,
) {
}

Expand Down
4 changes: 2 additions & 2 deletions app/modules/Ray/Application/Handlers/MergeEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use Modules\Sentry\Application\EventHandlerInterface;
use Spiral\Cqrs\QueryBusInterface;

final class MergeEventsHandler implements EventHandlerInterface
final readonly class MergeEventsHandler implements EventHandlerInterface
{
public function __construct(
private readonly QueryBusInterface $bus,
private QueryBusInterface $bus,
) {
}

Expand Down
Loading

0 comments on commit 2d54767

Please sign in to comment.