Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 7, 2022
1 parent f0d3f68 commit 1052511
Show file tree
Hide file tree
Showing 39 changed files with 338 additions and 278 deletions.
6 changes: 3 additions & 3 deletions DOCKER.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Docker

This project relies in the following 3 application images:
This project relies on the following 3 application images:

* [php.package.health/nginx](docker/nginx.Dockerfile): Serves static assets (css, fonts etc) and routes dynamic traffic to php-fpm;
* [php.package.health/nginx](docker/nginx.Dockerfile): Serves static assets (css, fonts etc.) and routes dynamic traffic to php-fpm;
* [php.package.health/php-fpm](docker/php.Dockerfile): Serves dynamic traffic based in the application code;
* [php.package.health/php-cli](docker/php.Dockerfile): Used to perform maintenance tasks, such as database migration.

Expand All @@ -18,7 +18,7 @@ docker build --file docker/php.Dockerfile --target cli --tag php.package.health/

## Creating a network

The network will be shared by the containers so they can communicate without exposing ports to the host machine.
The network will be shared by the containers, so they can communicate without exposing ports to the host machine.

```bash
docker network create php-package-health-network
Expand Down
17 changes: 0 additions & 17 deletions app/commands.php

This file was deleted.

18 changes: 18 additions & 0 deletions app/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types = 1);

use App\Application\Console\Package\GetDataCommand;
use App\Application\Console\Package\GetListCommand;
use App\Application\Console\Package\GetUpdatesCommand;
use App\Application\Console\Package\MassImportCommand;
use DI\ContainerBuilder;
use function DI\autowire;

return static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->addDefinitions([
GetDataCommand::class => autowire(GetDataCommand::class),
GetListCommand::class => autowire(GetListCommand::class),
GetUpdatesCommand::class => autowire(GetUpdatesCommand::class),
MassImportCommand::class => autowire(MassImportCommand::class)
]);
};
15 changes: 8 additions & 7 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
use PUGX\Poser\Render\SvgPlasticRender;
use Slim\HttpCache\CacheProvider;
use Slim\Views\Twig;
use function DI\autowire;

return function (ContainerBuilder $containerBuilder): void {
return static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->addDefinitions(
[
Browser::class => function (ContainerInterface $container): Browser {
Expand All @@ -29,8 +30,8 @@
new Psr17Factory()
);
},
CacheProvider::class => \DI\autowire(CacheProvider::class),
EventEmitter::class => \DI\autowire(EventEmitter::class),
CacheProvider::class => autowire(CacheProvider::class),
EventEmitter::class => autowire(EventEmitter::class),
LoggerInterface::class => function (ContainerInterface $container): LoggerInterface {
$settings = $container->get(SettingsInterface::class);

Expand Down Expand Up @@ -74,10 +75,10 @@
]
);
},
SvgFlatRender::class => \DI\autowire(SvgFlatRender::class),
SvgFlatSquareRender::class => \DI\autowire(SvgFlatSquareRender::class),
SvgPlasticRender::class => \DI\autowire(SvgPlasticRender::class),
VersionParser::class => \DI\autowire(VersionParser::class),
SvgFlatRender::class => autowire(SvgFlatRender::class),
SvgFlatSquareRender::class => autowire(SvgFlatSquareRender::class),
SvgPlasticRender::class => autowire(SvgPlasticRender::class),
VersionParser::class => autowire(VersionParser::class),
Twig::class => function (ContainerInterface $container): Twig {
$settings = $container->get(SettingsInterface::class);
$cache = false;
Expand Down
2 changes: 1 addition & 1 deletion app/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Evenement\EventEmitter;
use Psr\Container\ContainerInterface;

return function (ContainerInterface $container): void {
return static function (ContainerInterface $container): void {
$eventEmitter = $container->get(EventEmitter::class);

/* DEPENDENCY EVENTS */
Expand Down
11 changes: 6 additions & 5 deletions app/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use App\Application\Listeners\StatsListener;
use App\Application\Listeners\VersionListener;
use DI\ContainerBuilder;
use function DI\autowire;

return function (ContainerBuilder $containerBuilder): void {
return static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->addDefinitions([
DependencyListener::class => \DI\autowire(DependencyListener::class),
PackageListener::class => \DI\autowire(PackageListener::class),
StatsListener::class => \DI\autowire(StatsListener::class),
VersionListener::class => \DI\autowire(VersionListener::class)
DependencyListener::class => autowire(DependencyListener::class),
PackageListener::class => autowire(PackageListener::class),
StatsListener::class => autowire(StatsListener::class),
VersionListener::class => autowire(VersionListener::class)
]);
};
2 changes: 1 addition & 1 deletion app/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

return function (App $app): void {
return static function (App $app): void {
$container = $app->getContainer();

$app->add(new ContentLengthMiddleware());
Expand Down
19 changes: 10 additions & 9 deletions app/repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
use App\Domain\Package\PackageRepositoryInterface;
use App\Domain\Stats\StatsRepositoryInterface;
use App\Domain\Version\VersionRepositoryInterface;
use App\Infrastructure\Persistence\Dependency\SqlDependencyRepository;
use App\Infrastructure\Persistence\Package\SqlPackageRepository;
use App\Infrastructure\Persistence\Stats\SqlStatsRepository;
use App\Infrastructure\Persistence\Version\SqlVersionRepository;
use App\Infrastructure\Persistence\Dependency\PdoDependencyRepository;
use App\Infrastructure\Persistence\Package\PdoPackageRepository;
use App\Infrastructure\Persistence\Stats\PdoStatsRepository;
use App\Infrastructure\Persistence\Version\PdoVersionRepository;
use DI\ContainerBuilder;
use function DI\autowire;

return function (ContainerBuilder $containerBuilder): void {
return static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->addDefinitions([
DependencyRepositoryInterface::class => \DI\autowire(SqlDependencyRepository::class),
PackageRepositoryInterface::class => \DI\autowire(SqlPackageRepository::class),
StatsRepositoryInterface::class => \DI\autowire(SqlStatsRepository::class),
VersionRepositoryInterface::class => \DI\autowire(SqlVersionRepository::class)
DependencyRepositoryInterface::class => autowire(PdoDependencyRepository::class),
PackageRepositoryInterface::class => autowire(PdoPackageRepository::class),
StatsRepositoryInterface::class => autowire(PdoStatsRepository::class),
VersionRepositoryInterface::class => autowire(PdoVersionRepository::class)
]);
};
4 changes: 2 additions & 2 deletions app/routes.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
declare(strict_types = 1);

use App\Application\Actions\Maintenance\HealthAction;
use App\Application\Actions\Package\ListPackagesAction;
use App\Application\Actions\Package\RedirectListPackagesAction;
use App\Application\Actions\Package\RedirectPackageAction;
use App\Application\Actions\Package\RedirectPackageBadgeAction;
use App\Application\Actions\Package\ViewPackageAction;
use App\Application\Actions\Package\ViewPackageBadgeAction;
use App\Application\Actions\Maintenance\HealthAction;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

return function (App $app): void {
return static function (App $app): void {
$app->options('/{routes:.*}', function (ServerRequestInterface $request, ResponseInterface $response) {
// CORS Pre-Flight OPTIONS Request Handler
return $response;
Expand Down
2 changes: 1 addition & 1 deletion app/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder): void {
return static function (ContainerBuilder $containerBuilder): void {
// Global Settings Object
$containerBuilder->addDefinitions([
SettingsInterface::class => function () {
Expand Down
17 changes: 8 additions & 9 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Application\Console\Package\GetListCommand;
use App\Application\Console\Package\GetUpdatesCommand;
use App\Application\Console\Package\MassImportCommand;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
Expand All @@ -30,7 +29,7 @@

if (isset($_ENV['PHP_ENV']) && $_ENV['PHP_ENV'] === 'production') {
// workaround for https://github.com/PHP-DI/PHP-DI/issues/791
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
if (PHP_VERSION_ID < 80100) {
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}
}
Expand All @@ -47,9 +46,9 @@
$repositories = require __DIR__ . '/../app/repositories.php';
$repositories($containerBuilder);

// Set up commands
$commands = require __DIR__ . '/../app/commands.php';
$commands($containerBuilder);
// Set up console commands
$console = require __DIR__ . '/../app/console.php';
$console($containerBuilder);

// Set up listeners
$listeners = require __DIR__ . '/../app/listeners.php';
Expand All @@ -66,16 +65,16 @@

$commandLoader = new FactoryCommandLoader(
[
GetDataCommand::getDefaultName() => function () use ($container): GetDataCommand {
GetDataCommand::getDefaultName() => static function () use ($container): GetDataCommand {
return $container->get(GetDataCommand::class);
},
GetListCommand::getDefaultName() => function () use ($container): GetListCommand {
GetListCommand::getDefaultName() => static function () use ($container): GetListCommand {
return $container->get(GetListCommand::class);
},
GetUpdatesCommand::getDefaultName() => function () use ($container): GetUpdatesCommand {
GetUpdatesCommand::getDefaultName() => static function () use ($container): GetUpdatesCommand {
return $container->get(GetUpdatesCommand::class);
},
MassImportCommand::getDefaultName() => function () use ($container): MassImportCommand {
MassImportCommand::getDefaultName() => static function () use ($container): MassImportCommand {
return $container->get(MassImportCommand::class);
}
]
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php": ">=8.1",
"ext-pdo": "*",
"ext-posix": "*",
"badges/poser": "^2.3",
"composer/metadata-minifier": "^1.0",
"composer/semver": "^3.2",
Expand Down
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

if (isset($_ENV['PHP_ENV']) && $_ENV['PHP_ENV'] === 'production') {
// workaround for https://github.com/PHP-DI/PHP-DI/issues/791
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
if (PHP_VERSION_ID < 80100) {
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}
}
Expand Down
5 changes: 3 additions & 2 deletions public/sprites/brand.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions public/sprites/free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion resources/views/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tr>
<td><a href="{{ url_for('redirectPackage', {vendor: package.vendor, project: package.project}) }}">{{ package.name }}</a></td>
<td class="has-text-grey">{{ package.description }}</td>
<td class="has-text-right"><img src="{{ url_for('redirectPackageBadge', {vendor: package.vendor, project: package.project}) }}"></td>
<td class="has-text-right"><img src="{{ url_for('redirectPackageBadge', {vendor: package.vendor, project: package.project}) }}" alt="Dependency Badge"></td>
</tr>
{% endfor %}
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/package.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

{% block hero_title %}<a href="{{ package.url }}"><span class="icon-text"><span class="icon"><i class="cib-github" alt="GitHub"></i></span>&nbsp;<span>{{ package.name }} {{ version.number }}</span></span></a>{% endblock %}

{% block hero_subtitle %}<img src="{{ url_for('viewPackageBadge', {vendor: package.vendor, project: package.project, version: version.number}) }}">{% endblock %}
{% block hero_subtitle %}<img src="{{ url_for('viewPackageBadge', {vendor: package.vendor, project: package.project, version: version.number}) }}" alt="Dependency Badge">{% endblock %}

{% block hero_footer %}[![dependency status]({{ full_url_for('viewPackageBadge', {vendor: package.vendor, project: package.project, version: version.number}) }})]({{ full_url_for('viewPackage', {vendor: package.vendor, project: package.project, version: version.number}) }}){% endblock %}

{% block content %}
{% if notification %}
{% if notification is defined %}
<div class="notification {{ notification.type }}">
<p>{{ notification.message }}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/template.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
16 changes: 10 additions & 6 deletions src/Application/Actions/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace App\Application\Actions;

use App\Domain\DomainException\DomainRecordNotFoundException;
use App\Domain\Exception\DomainRecordNotFoundException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -31,7 +31,11 @@ public function __construct(LoggerInterface $logger, CacheProvider $cacheProvide
* @throws \Slim\Exception\HttpNotFoundException
* @throws \Slim\Exception\HttpBadRequestException
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface {
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
$this->request = $request;
$this->response = $response;
$this->args = $args;
Expand Down Expand Up @@ -63,7 +67,7 @@ protected function getFormData() {
*/
protected function resolveArg(string $name) {
if (!isset($this->args[$name])) {
throw new HttpBadRequestException($this->request, "Could not resolve argument '{$name}'.");
throw new HttpBadRequestException($this->request, "Could not resolve argument '${name}'.");
}

return $this->args[$name];
Expand All @@ -82,7 +86,7 @@ protected function respondWithHtml(string $content, int $statusCode = 200): Resp
}

protected function respondWithJson(array $content, int $statusCode = 200): ResponseInterface {
return $this->respondWith('application/json', json_encode($content), $statusCode);
return $this->respondWith('application/json', json_encode($content, JSON_THROW_ON_ERROR), $statusCode);
}

protected function respondWithRedirect(string $url, int $statusCode = 302): ResponseInterface {
Expand All @@ -94,14 +98,14 @@ protected function respondWithRedirect(string $url, int $statusCode = 302): Resp
/**
* @param array|object|null $data
*/
protected function respondWithData($data = null, int $statusCode = 200): ResponseInterface {
protected function respondWithData(mixed $data = null, int $statusCode = 200): ResponseInterface {
$payload = new ActionPayload($statusCode, $data);

return $this->respond($payload);
}

protected function respond(ActionPayload $payload): ResponseInterface {
$json = json_encode($payload, JSON_PRETTY_PRINT);
$json = json_encode($payload, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
$this->response->getBody()->write($json);

return $this->response
Expand Down
1 change: 0 additions & 1 deletion src/Application/Actions/Package/ListPackagesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace App\Application\Actions\Package;

use App\Domain\Package\Package;
use Psr\Http\Message\ResponseInterface;
use Slim\Views\Twig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace App\Application\Actions\Package;

use App\Domain\Package\Package;
use Psr\Http\Message\ResponseInterface;
use Slim\Routing\RouteContext;

Expand Down
Loading

0 comments on commit 1052511

Please sign in to comment.