Skip to content

Commit

Permalink
Well that was harder than it looked
Browse files Browse the repository at this point in the history
  • Loading branch information
shrikeh committed May 30, 2022
1 parent b62e520 commit c3bd044
Show file tree
Hide file tree
Showing 23 changed files with 1,557 additions and 73 deletions.
13 changes: 11 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=test
APP_ENV=prod
APP_DEBUG=true
APP_SECRET=e4fab8533e85795651f9963db77469ea

Expand All @@ -28,7 +28,16 @@ APP_SECRET=e4fab8533e85795651f9963db77469ea
###< symfony/messenger ###
APP_CODE_DIR='/cushon/app'
APP_BUILD_DIR=build
APP_CACHE_DIR=${APP_BUILD_DIR}/cache/symfony
APP_CACHE_DIR=build/cache/symfony
APP_LOG_DIR=build/logs
HOST_NGINX_PORT=9010
NULL_DEPENDENCY_NAME='A fake dependency'

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7&charset=utf8mb4"
DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
APP_ENV=test
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app export-ignore
docker export-ignore
features export-ignore
tests export-ignore
.github export-ignore
.editorconfig export-ignore
.env export-ignore
.env.test export-ignore
Expand Down
17 changes: 5 additions & 12 deletions app/config/bundles.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

declare(strict_types=1);

use Cushon\HealthBundle\CushonHealthBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;

return [
FrameworkBundle::class => ['all' => true],
FriendsOfBehatSymfonyExtensionBundle::class => [
'test' => true,
'dev' => true,
],
CushonHealthBundle::class => ['all' => true],
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle::class => ['test' => true, 'dev' => true],
Cushon\HealthBundle\CushonHealthBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
];
37 changes: 37 additions & 0 deletions app/config/packages/doctrine.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
parameters:
env(DB_READ_HOST): '127.0.0.1'
env(DB_READ_NAME): 'health_example'
env(DB_READ_USER): 'cushon_read'
env(DB_READ_VERSION): 'mariadb-10.7.3'
env(DB_READ_PORT): 3306

env(DB_WRITE_HOST): '127.0.0.1'
env(DB_WRITE_NAME): 'health_example'
env(DB_WRITE_USER): 'cushon_write'
env(DB_WRITE_VERSION): 'mariadb-10.7.3'
env(DB_WRITE_PORT): 3306

doctrine:
dbal:
default_connection: default
connections:
default:
# Connection used for read operations
host: '%env(string:DB_READ_HOST)%'
dbname: '%env(string:DB_READ_NAME)%'
user: '%env(string:DB_READ_USER)%'
password: '%env(string:DB_READ_PASSWORD)%'
server_version: '%env(string:DB_READ_VERSION)%'
port: '%env(int:DB_READ_PORT)%'
driver: 'pdo_mysql'
write:
# Connection used for write operations
host: '%env(string:DB_WRITE_HOST)%'
dbname: '%env(string:DB_WRITE_NAME)%'
user: '%env(string:DB_WRITE_USER)%'
password: '%env(string:DB_WRITE_PASSWORD)%'
server_version: '%env(string:DB_WRITE_VERSION)%'
port: '%env(int:DB_WRITE_PORT)%'
driver: 'pdo_mysql'

8 changes: 8 additions & 0 deletions app/config/packages/doctrine_migrations.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
doctrine_migrations:
connection: 'write'
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'Example\DoctrineMigrations': '%kernel.project_dir%/migrations'
enable_profiler: '%kernel.debug%'
13 changes: 7 additions & 6 deletions app/config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ framework:
#fragments: true
php_errors:
log: true

http_client:
# https://www.random.org/integers/?num=10&min=1&max=6&col=1&base=10&format=plain&rnd=new
scoped_clients:
random.client:
base_uri: 'https://www.random.org'
when@prod: &prod
framework:
http_client:
# https://www.random.org/integers/?num=10&min=1&max=6&col=1&base=10&format=plain&rnd=new
scoped_clients:
random.client:
base_uri: 'https://www.random.org'

when@test:
framework:
Expand Down
5 changes: 3 additions & 2 deletions app/config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ services:
autowire: true
autoconfigure: true

# Disable the example Null Dependency Check in test
App\ApplicationHealth\DependencyCheck\NullDepdencyCheck:
# Disable the example Dependency Checks in test
App\ApplicationHealth\DependencyCheck\:
resource: '%kernel.project_dir%/src/ApplicationHealth/DependencyCheck/'
autowire: false
autoconfigure: false

Expand Down
34 changes: 34 additions & 0 deletions app/src/ApplicationHealth/DependencyCheck/DatabaseCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace App\ApplicationHealth\DependencyCheck;

use App\ApplicationHealth\DependencyCheck\DatabaseCheck\HealthDependencyRepository;
use Cushon\HealthBundle\ApplicationHealth\DependencyCheck;
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;
use Ds\Set;
use Generator;

final class DatabaseCheck implements DependencyCheck
{
private Set $dependencyRepositories;

/**
* @param HealthDependencyRepository ...$dependencyRepositories
*/
public function __construct(HealthDependencyRepository ...$dependencyRepositories)
{
$this->dependencyRepositories = new Set($dependencyRepositories);
}

/**
* @inheritDoc
*/
public function check(): Generator
{
/** @var HealthDependencyRepository $dependencyRepository */
foreach ($this->dependencyRepositories as $dependencyRepository) {
yield $dependencyRepository->checkUser();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace App\ApplicationHealth\DependencyCheck\DatabaseCheck;

use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;

interface HealthDependencyRepository
{
/**
* @return DependencyStatus
*/
public function checkUser(): DependencyStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cushon\HealthBundle\ApplicationHealth\DependencyCheck;
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;
use Generator;

final class NullDependencyCheck implements DependencyCheck
{
Expand All @@ -28,11 +29,11 @@ public function __construct(string $name, bool $health, string $info)
}

/**
* @return DependencyStatus
* @inheritDoc
*/
public function check(): DependencyStatus
public function check(): Generator
{
return new DependencyStatus\SimpleStatus(
yield new DependencyStatus\SimpleStatus(
$this->name,
$this->health,
$this->info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cushon\HealthBundle\ApplicationHealth\DependencyCheck;
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus\SimpleStatus;
use Generator;

final class RandomOrgApiCheck implements DependencyCheck
{
Expand All @@ -22,11 +23,10 @@ public function __construct(HealthDependencyRepository $healthDependencyReposito
$this->healthDependencyRepository = $healthDependencyRepository;
}


/**
* @return DependencyStatus
* @inheritDoc
*/
public function check(): DependencyStatus
public function check(): Generator
{
$health = false;

Expand All @@ -38,6 +38,6 @@ public function check(): DependencyStatus
$info = $e->getMessage();
}

return new SimpleStatus('random.org', $health, $info);
yield new SimpleStatus('random.org', $health, $info);
}
}
18 changes: 8 additions & 10 deletions app/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App;

use App\Kernel\Exception\EnvironmentVariableNotSet;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Tests\Utils\Constants;

Expand All @@ -20,14 +20,6 @@ public function getProjectDir(): string
return Constants::appDir();
}

/**
* @inheritDoc
*/
public function getCacheDir(): string
{
return $this->getEnvVar('APP_CACHE_DIR');
}

/**
* @inheritDoc
*/
Expand All @@ -42,10 +34,16 @@ public function getLogDir(): string
*/
private function getEnvVar(string $varName): string
{
if (!$var = getenv($varName)) {
$this->bootEnv();
if (!$var = $_ENV[$varName]) {
throw EnvironmentVariableNotSet::fromVarName($varName);
}

return $var;
}

private function bootEnv(): void
{
(new Dotenv())->bootEnv(Constants::rootDir() . '/.env');
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"require-dev": {
"behat/behat": "^3.6",
"brianium/paratest": "^6.4",
"doctrine/dbal": "^3.3",
"doctrine/doctrine-bundle": "^2.6",
"doctrine/doctrine-migrations-bundle": "^3.2",
"dvdoug/behat-code-coverage": "^5.2",
"ergebnis/composer-normalize": "^2.25",
"friends-of-behat/symfony-extension": "^2.3",
Expand Down

0 comments on commit c3bd044

Please sign in to comment.