21torr/hosting
21torr/hosting is a small Symfony bundle providing essential tools for hosting Symfony-based applications, helping streamline deployment and operations with basic hosting utilities and sensible defaults.
Install the Bundle:
composer require 21torr/hosting-bundle
Ensure your config/bundles.php includes:
return [
// ...
HostingBundle\HostingBundle::class => ['all' => true],
];
Configure Basic Hosting Tier:
Update config/packages/21torr_hosting.yaml:
21torr_hosting:
tier: production # or 'staging', 'development'
build_info:
app_version: '1.0.0'
# Custom metadata (e.g., git_commit, build_timestamp)
First Use Case: Environment Awareness Access the hosting environment in controllers/services:
use HostingBundle\Environment\HostingEnvironment;
public function someAction(HostingEnvironment $env)
{
if ($env->isDebug()) {
// Enable debug-specific logic
}
return $this->json($env->getTier());
}
Trigger Hooks Manually: Run post-build/deploy hooks during CI/CD:
php bin/console hosting:hook:build
php bin/console hosting:hook:deploy
HostingEnvironment to gate features by tier/debug mode.
if ($env->getTier() === 'production') {
$this->mailer->sendTransactional(...);
}
%kernel.environment% for fallback logic:
$isProduction = $env->getTier() === 'production' || $env->isDebug() === false;
# config/packages/21torr_hosting.yaml
21torr_hosting:
hooks:
build:
- App\Hook\OptimizeAssetsHook
deploy:
- App\Hook\CacheWarmupHook
- App\Hook\SendDeploymentNotificationHook
use HostingBundle\Hook\BuildHookInterface;
class OptimizeAssetsHook implements BuildHookInterface {
public function run(): void {
// Run asset optimization (e.g., Symfony Encore)
}
}
use HostingBundle\HealthCheck\HealthCheckInterface;
class DatabaseConnectionCheck implements HealthCheckInterface {
public function check(): bool {
return $this->connection->isConnected();
}
}
config/packages/21torr_hosting.yaml:
21torr_hosting:
health_checks:
- App\HealthCheck\DatabaseConnectionCheck
BuildInfo to track deployments (e.g., in logs or metrics).
use HostingBundle\BuildInfo\BuildInfo;
public function logDeployment(BuildInfo $buildInfo)
{
$this->logger->info('Deployed version', [
'version' => $buildInfo->get('app_version'),
'built_at' => $buildInfo->get('built'),
]);
}
php bin/console hosting:build-info:set --key=git_commit --value=$(git rev-parse HEAD)
# config/packages/21torr_hosting.yaml
21torr_hosting:
tier: '%env(HOSTING_TIER)%' # Defaults to 'production'
services.yaml:
App\Service\FeatureFlag:
arguments:
$isEnabled: '%env(bool:FEATURE_FLAG_ENABLED)%'
HostingEnvironment to set log levels:
$logger->setLevel($env->isDebug() ? Logger::DEBUG : Logger::INFO);
$cache->get('key', $env->getTier() . ':prefix');
$message->setTransport($env->isDebug() ? 'sync' : 'async');
hosting:validate-app in CI:
- name: Validate App
run: php bin/console hosting:validate-app
#!/bin/bash
php bin/console doctrine:migrations:migrate --no-interaction
php bin/console hosting:hook:deploy
HostingEnvironment to isolate tenant-specific configs:
$tenantConfig = $this->configLoader->load(
sprintf('tenants/%s/config.yaml', $env->getTier())
);
Configuration Key Changes (v4+)
hosting to 21torr_hosting in v4.0.0.config/packages/21torr_hosting.yaml and clear cache:
php bin/console cache:clear
Debug Mode Confusion
HostingEnvironment::isDebug() reflects %kernel.debug%, not the hosting tier.$env->getTier() === 'development' for tier-specific logic.Build Info Caching
php bin/console hosting:build-info:generate
Doctrine Health Check Quirks
DoctrineHealthCheckListener ignores doctrine_migration_versions tables by default.21torr_hosting:
doctrine_health_check:
ignored_tables: ['migration_versions', 'custom_table']
Hook Execution Order
config/packages/21torr_hosting.yaml.services:
App\Hook\FirstHook:
tags: [hosting.hook.build]
App\Hook\SecondHook:
tags: [hosting.hook.build]
priority: 10 # Runs after FirstHook
Validate Config
Use the hosting:validate-app command to catch misconfigurations early:
php bin/console hosting:validate-app --env=prod
Inspect Build Info Dump build info to debug deployments:
php bin/console hosting:build-info:show
Health Check Debugging Enable verbose health check output:
php bin/console debug:health-check --verbose
Hook Debugging Add logging to hooks to trace execution:
use Psr\Log\LoggerInterface;
class MyHook implements BuildHookInterface {
public function __construct(private LoggerInterface $logger) {}
public function run(): void {
$this->logger->debug('Hook executed');
// ...
}
}
Custom Health Checks
Extend the HealthCheckInterface to add application-specific checks (e.g., external API availability):
class ExternalApiCheck implements HealthCheckInterface {
public function check(): bool {
return $this->httpClient->get('https://api.example.com/health')->getStatusCode() === 200;
}
}
Dynamic Tier Resolution
Override HostingEnvironment to resolve tiers dynamically (e.g., from a database):
use HostingBundle\Environment\HostingEnvironment;
class CustomHostingEnvironment extends HostingEnvironment {
public function getTier(): string {
return $this->tenantService->getCurrentTenantTier();
}
}
Register as a service:
services:
HostingBundle\Environment\HostingEnvironment: '@App\Environment\Custom
How can I help you explore Laravel packages today?