Simplify Environment class and dependencies.
This commit is contained in:
parent
403d6c3f7d
commit
bf1e4f2591
|
@ -1,44 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Application Settings
|
||||
*/
|
||||
|
||||
return [
|
||||
// Application name
|
||||
'name' => 'AzuraCast',
|
||||
|
||||
// Subfolder for the application (if applicable)
|
||||
'base_uri' => '/',
|
||||
|
||||
// Base of the static URL.
|
||||
'static_uri' => '/static/',
|
||||
|
||||
/* Localization Settings */
|
||||
'locale' => [
|
||||
'supported' => [
|
||||
'en_US.UTF-8' => 'English (Default)',
|
||||
'cs_CZ.UTF-8' => 'čeština', // Czech
|
||||
'de_DE.UTF-8' => 'Deutsch', // German
|
||||
'es_ES.UTF-8' => 'Español', // Spanish
|
||||
'fr_FR.UTF-8' => 'Français', // French
|
||||
'el_GR.UTF-8' => 'ελληνικά', // Greek
|
||||
'it_IT.UTF-8' => 'Italiano', // Italian
|
||||
'hu_HU.UTF-8' => 'magyar', // Hungarian
|
||||
'nl_NL.UTF-8' => 'Nederlands', // Dutch
|
||||
'pl_PL.UTF-8' => 'Polski', // Polish
|
||||
'pt_PT.UTF-8' => 'Português', // Portuguese
|
||||
'pt_BR.UTF-8' => 'Português do Brasil', // Brazilian Portuguese
|
||||
'ru_RU.UTF-8' => 'Русский язык', // Russian
|
||||
'sv_SE.UTF-8' => 'Svenska', // Swedish
|
||||
'tr_TR.UTF-8' => 'Türkçe', // Turkish
|
||||
'zh_CN.UTF-8' => '簡化字', // Simplified Chinese
|
||||
],
|
||||
],
|
||||
|
||||
// PHP date() formats for locales available above.
|
||||
'time_formats' => [
|
||||
'default' => 'G:i',
|
||||
'en_US.UTF-8' => 'g:i A',
|
||||
],
|
||||
|
||||
];
|
|
@ -6,7 +6,8 @@ use App\Event;
|
|||
use App\Middleware;
|
||||
|
||||
return function (App\EventDispatcher $dispatcher) {
|
||||
$dispatcher->addListener(Event\BuildConsoleCommands::class,
|
||||
$dispatcher->addListener(
|
||||
Event\BuildConsoleCommands::class,
|
||||
function (Event\BuildConsoleCommands $event) use ($dispatcher) {
|
||||
$console = $event->getConsole();
|
||||
$di = $console->getContainer();
|
||||
|
@ -14,97 +15,102 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
/** @var Environment $environment */
|
||||
$environment = $di->get(Environment::class);
|
||||
|
||||
if ($environment->enableRedis()) {
|
||||
$console->command('cache:clear', Command\ClearCacheCommand::class)
|
||||
->setDescription('Clear all application caches.');
|
||||
}
|
||||
$console->command('cache:clear', Command\ClearCacheCommand::class)
|
||||
->setDescription('Clear all application caches.');
|
||||
|
||||
if ($environment->enableDatabase()) {
|
||||
// Doctrine ORM/DBAL
|
||||
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($console);
|
||||
// Doctrine ORM/DBAL
|
||||
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($console);
|
||||
|
||||
// Add Doctrine Migrations
|
||||
/** @var Doctrine\ORM\EntityManagerInterface $em */
|
||||
$em = $di->get(Doctrine\ORM\EntityManagerInterface::class);
|
||||
// Add Doctrine Migrations
|
||||
/** @var Doctrine\ORM\EntityManagerInterface $em */
|
||||
$em = $di->get(Doctrine\ORM\EntityManagerInterface::class);
|
||||
|
||||
$helper_set = $console->getHelperSet();
|
||||
$doctrine_helpers = Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em);
|
||||
$helper_set->set($doctrine_helpers->get('db'), 'db');
|
||||
$helper_set->set($doctrine_helpers->get('em'), 'em');
|
||||
$helper_set = $console->getHelperSet();
|
||||
$doctrine_helpers = Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em);
|
||||
$helper_set->set($doctrine_helpers->get('db'), 'db');
|
||||
$helper_set->set($doctrine_helpers->get('em'), 'em');
|
||||
|
||||
$migrationConfigurations = [
|
||||
'migrations_paths' => [
|
||||
'App\Entity\Migration' => $environment->getBaseDirectory() . '/src/Entity/Migration',
|
||||
],
|
||||
'table_storage' => [
|
||||
'table_name' => 'app_migrations',
|
||||
'version_column_length' => 191,
|
||||
],
|
||||
];
|
||||
$migrationConfigurations = [
|
||||
'migrations_paths' => [
|
||||
'App\Entity\Migration' => $environment->getBaseDirectory() . '/src/Entity/Migration',
|
||||
],
|
||||
'table_storage' => [
|
||||
'table_name' => 'app_migrations',
|
||||
'version_column_length' => 191,
|
||||
],
|
||||
];
|
||||
|
||||
$buildMigrationConfigurationsEvent = new Event\BuildMigrationConfigurationArray(
|
||||
$migrationConfigurations,
|
||||
$environment->getBaseDirectory()
|
||||
);
|
||||
$dispatcher->dispatch($buildMigrationConfigurationsEvent);
|
||||
$buildMigrationConfigurationsEvent = new Event\BuildMigrationConfigurationArray(
|
||||
$migrationConfigurations,
|
||||
$environment->getBaseDirectory()
|
||||
);
|
||||
$dispatcher->dispatch($buildMigrationConfigurationsEvent);
|
||||
|
||||
$migrationConfigurations = $buildMigrationConfigurationsEvent->getMigrationConfigurations();
|
||||
$migrationConfigurations = $buildMigrationConfigurationsEvent->getMigrationConfigurations();
|
||||
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray($migrationConfigurations);
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray($migrationConfigurations);
|
||||
|
||||
$migrateFactory = Doctrine\Migrations\DependencyFactory::fromEntityManager(
|
||||
$migrateConfig,
|
||||
new Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager($em)
|
||||
);
|
||||
Doctrine\Migrations\Tools\Console\ConsoleRunner::addCommands($console, $migrateFactory);
|
||||
}
|
||||
$migrateFactory = Doctrine\Migrations\DependencyFactory::fromEntityManager(
|
||||
$migrateConfig,
|
||||
new Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager($em)
|
||||
);
|
||||
Doctrine\Migrations\Tools\Console\ConsoleRunner::addCommands($console, $migrateFactory);
|
||||
|
||||
call_user_func(include(__DIR__ . '/cli.php'), $console);
|
||||
});
|
||||
|
||||
$dispatcher->addListener(Event\BuildRoutes::class, function (Event\BuildRoutes $event) {
|
||||
$app = $event->getApp();
|
||||
|
||||
// Load app-specific route configuration.
|
||||
$container = $app->getContainer();
|
||||
|
||||
/** @var Environment $environment */
|
||||
$environment = $container->get(Environment::class);
|
||||
|
||||
call_user_func(include(__DIR__ . '/routes.php'), $app);
|
||||
|
||||
if (file_exists(__DIR__ . '/routes.dev.php')) {
|
||||
call_user_func(include(__DIR__ . '/routes.dev.php'), $app);
|
||||
}
|
||||
);
|
||||
|
||||
$app->add(Middleware\WrapExceptionsWithRequestData::class);
|
||||
$dispatcher->addListener(
|
||||
Event\BuildRoutes::class,
|
||||
function (Event\BuildRoutes $event) {
|
||||
$app = $event->getApp();
|
||||
|
||||
$app->add(Middleware\EnforceSecurity::class);
|
||||
$app->add(Middleware\InjectAcl::class);
|
||||
$app->add(Middleware\GetCurrentUser::class);
|
||||
// Load app-specific route configuration.
|
||||
$container = $app->getContainer();
|
||||
|
||||
// Request injection middlewares.
|
||||
$app->add(Middleware\InjectRouter::class);
|
||||
$app->add(Middleware\InjectRateLimit::class);
|
||||
/** @var Environment $environment */
|
||||
$environment = $container->get(Environment::class);
|
||||
|
||||
// Re-establish database connection if multiple requests are handled by the same stack.
|
||||
$app->add(Middleware\ReopenEntityManagerMiddleware::class);
|
||||
call_user_func(include(__DIR__ . '/routes.php'), $app);
|
||||
|
||||
// System middleware for routing and body parsing.
|
||||
$app->addBodyParsingMiddleware();
|
||||
$app->addRoutingMiddleware();
|
||||
if (file_exists(__DIR__ . '/routes.dev.php')) {
|
||||
call_user_func(include(__DIR__ . '/routes.dev.php'), $app);
|
||||
}
|
||||
|
||||
// Redirects and updates that should happen before system middleware.
|
||||
$app->add(new Middleware\RemoveSlashes);
|
||||
$app->add(new Middleware\ApplyXForwardedProto);
|
||||
$app->add(Middleware\WrapExceptionsWithRequestData::class);
|
||||
|
||||
// Use PSR-7 compatible sessions.
|
||||
$app->add(Middleware\InjectSession::class);
|
||||
$app->add(Middleware\EnforceSecurity::class);
|
||||
$app->add(Middleware\InjectAcl::class);
|
||||
$app->add(Middleware\GetCurrentUser::class);
|
||||
|
||||
// Add an error handler for most in-controller/task situations.
|
||||
$errorMiddleware = $app->addErrorMiddleware(!$environment->isProduction(), true, true);
|
||||
$errorMiddleware->setDefaultErrorHandler(Slim\Interfaces\ErrorHandlerInterface::class);
|
||||
});
|
||||
// Request injection middlewares.
|
||||
$app->add(Middleware\InjectRouter::class);
|
||||
$app->add(Middleware\InjectRateLimit::class);
|
||||
|
||||
// Re-establish database connection if multiple requests are handled by the same stack.
|
||||
$app->add(Middleware\ReopenEntityManagerMiddleware::class);
|
||||
|
||||
// System middleware for routing and body parsing.
|
||||
$app->addBodyParsingMiddleware();
|
||||
$app->addRoutingMiddleware();
|
||||
|
||||
// Redirects and updates that should happen before system middleware.
|
||||
$app->add(new Middleware\RemoveSlashes);
|
||||
$app->add(new Middleware\ApplyXForwardedProto);
|
||||
|
||||
// Use PSR-7 compatible sessions.
|
||||
$app->add(Middleware\InjectSession::class);
|
||||
|
||||
// Add an error handler for most in-controller/task situations.
|
||||
$errorMiddleware = $app->addErrorMiddleware(
|
||||
!$environment->isProduction(),
|
||||
true,
|
||||
true,
|
||||
$container->get(Psr\Log\LoggerInterface::class)
|
||||
);
|
||||
$errorMiddleware->setDefaultErrorHandler(Slim\Interfaces\ErrorHandlerInterface::class);
|
||||
}
|
||||
);
|
||||
|
||||
// Build default menus
|
||||
$dispatcher->addListener(App\Event\BuildAdminMenu::class, function (App\Event\BuildAdminMenu $e) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
$locale_select = $settings['locale']['supported'];
|
||||
/** @var \App\Environment $environment */
|
||||
|
||||
$locale_select = $environment->getSupportedLocales();
|
||||
$locale_select = ['default' => __('Use Browser Default')] + $locale_select;
|
||||
|
||||
return [
|
||||
|
|
|
@ -416,7 +416,7 @@ return [
|
|||
$vueComponents = json_decode(file_get_contents($assets_file), true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
return new App\Assets($libraries, $versioned_files, $vueComponents);
|
||||
return new App\Assets($environment, $libraries, $versioned_files, $vueComponents);
|
||||
},
|
||||
|
||||
// Synchronized (Cron) Tasks
|
||||
|
|
|
@ -70,8 +70,15 @@ class AppFactory
|
|||
throw new Exception\BootstrapException('No base directory specified!');
|
||||
}
|
||||
|
||||
$isDocker = file_exists(dirname($environment[Environment::BASE_DIR]) . '/.docker');
|
||||
if ($isDocker) {
|
||||
$environment[Environment::IS_DOCKER] = file_exists(
|
||||
dirname($environment[Environment::BASE_DIR]) . '/.docker'
|
||||
);
|
||||
|
||||
$environment[Environment::TEMP_DIR] ??= dirname($environment[Environment::BASE_DIR]) . '/www_tmp';
|
||||
$environment[Environment::CONFIG_DIR] ??= $environment[Environment::BASE_DIR] . '/config';
|
||||
$environment[Environment::VIEWS_DIR] ??= $environment[Environment::BASE_DIR] . '/templates';
|
||||
|
||||
if ($environment[Environment::IS_DOCKER]) {
|
||||
$_ENV = getenv();
|
||||
} elseif (file_exists($environment[Environment::BASE_DIR] . '/env.ini')) {
|
||||
$_ENV = array_merge($_ENV, parse_ini_file($environment[Environment::BASE_DIR] . '/env.ini'));
|
||||
|
@ -79,21 +86,6 @@ class AppFactory
|
|||
|
||||
$environment = array_merge(array_filter($_ENV), $environment);
|
||||
|
||||
$environment[Environment::IS_DOCKER] = $isDocker;
|
||||
|
||||
$environment[Environment::TEMP_DIR] = dirname($environment[Environment::BASE_DIR]) . '/www_tmp';
|
||||
$environment[Environment::CONFIG_DIR] = $environment[Environment::BASE_DIR] . '/config';
|
||||
$environment[Environment::VIEWS_DIR] = $environment[Environment::BASE_DIR] . '/templates';
|
||||
$environment[Environment::TEMP_DIR] ??= dirname($environment[Environment::BASE_DIR]) . '/www_tmp';
|
||||
|
||||
if (file_exists($environment[Environment::CONFIG_DIR] . '/env.php')) {
|
||||
$envFile = require($environment[Environment::CONFIG_DIR] . '/env.php');
|
||||
|
||||
if (is_array($envFile)) {
|
||||
$environment = array_merge($environment, $envFile);
|
||||
}
|
||||
}
|
||||
|
||||
return $environment;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,19 +39,16 @@ class Assets
|
|||
/** @var ServerRequestInterface|null The current request (if it's available) */
|
||||
protected ?ServerRequestInterface $request = null;
|
||||
|
||||
/**
|
||||
* Assets constructor.
|
||||
*
|
||||
* @param array $libraries
|
||||
* @param array $versioned_files
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected Environment $environment;
|
||||
|
||||
public function __construct(
|
||||
Environment $environment,
|
||||
array $libraries = [],
|
||||
array $versioned_files = [],
|
||||
array $vueComponents = []
|
||||
) {
|
||||
$this->environment = $environment;
|
||||
|
||||
foreach ($libraries as $library_name => $library) {
|
||||
$this->addLibrary($library, $library_name);
|
||||
}
|
||||
|
@ -465,7 +462,7 @@ class Assets
|
|||
return $resource_uri;
|
||||
}
|
||||
|
||||
return '/static/' . $resource_uri;
|
||||
return $this->environment->getAssetUrl() . '/' . $resource_uri;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -82,7 +82,7 @@ class Customization
|
|||
*/
|
||||
protected function initLocale(?ServerRequestInterface $request = null): string
|
||||
{
|
||||
$supported_locales = $this->environment['locale']['supported'];
|
||||
$supported_locales = $this->environment->getSupportedLocales();
|
||||
$try_locales = [];
|
||||
|
||||
// Prefer user-based profile locale.
|
||||
|
|
|
@ -16,28 +16,25 @@ class Environment extends ArrayCollection
|
|||
public const ENV_PRODUCTION = 'production';
|
||||
|
||||
// Core settings values
|
||||
public const APP_NAME = 'name';
|
||||
public const APP_NAME = 'APP_NAME';
|
||||
public const APP_ENV = 'APPLICATION_ENV';
|
||||
|
||||
public const BASE_DIR = 'base_dir';
|
||||
public const TEMP_DIR = 'temp_dir';
|
||||
public const CONFIG_DIR = 'config_dir';
|
||||
public const VIEWS_DIR = 'views_dir';
|
||||
public const DOCTRINE_OPTIONS = 'doctrine_options';
|
||||
public const IS_DOCKER = 'is_docker';
|
||||
public const IS_CLI = 'is_cli';
|
||||
public const BASE_DIR = 'BASE_DIR';
|
||||
public const TEMP_DIR = 'TEMP_DIR';
|
||||
public const CONFIG_DIR = 'CONFIG_DIR';
|
||||
public const VIEWS_DIR = 'VIEWS_DIR';
|
||||
|
||||
public const BASE_URL = 'BASE_URL';
|
||||
public const ASSETS_URL = 'assets_url';
|
||||
public const IS_DOCKER = 'IS_DOCKER';
|
||||
public const IS_CLI = 'IS_CLI';
|
||||
|
||||
public const ENABLE_DATABASE = 'enable_database';
|
||||
public const ENABLE_REDIS = 'enable_redis';
|
||||
public const ASSET_URL = 'ASSETS_URL';
|
||||
|
||||
public const DOCKER_REVISION = 'AZURACAST_DC_REVISION';
|
||||
|
||||
public const ENABLE_ADVANCED_FEATURES = 'ENABLE_ADVANCED_FEATURES';
|
||||
|
||||
public const LANG = 'LANG';
|
||||
public const SUPPORTED_LOCALES = 'SUPPORTED_LOCALES';
|
||||
|
||||
public const RELEASE_CHANNEL = 'AZURACAST_VERSION';
|
||||
|
||||
|
@ -51,16 +48,32 @@ class Environment extends ArrayCollection
|
|||
|
||||
// Default settings
|
||||
protected array $defaults = [
|
||||
self::APP_NAME => 'Application',
|
||||
self::APP_NAME => 'AzuraCast',
|
||||
self::APP_ENV => self::ENV_PRODUCTION,
|
||||
|
||||
self::IS_DOCKER => true,
|
||||
self::IS_CLI => ('cli' === PHP_SAPI),
|
||||
|
||||
self::ASSETS_URL => '/static',
|
||||
self::ASSET_URL => '/static',
|
||||
|
||||
self::ENABLE_DATABASE => true,
|
||||
self::ENABLE_REDIS => true,
|
||||
self::SUPPORTED_LOCALES => [
|
||||
'en_US.UTF-8' => 'English (Default)',
|
||||
'cs_CZ.UTF-8' => 'čeština', // Czech
|
||||
'de_DE.UTF-8' => 'Deutsch', // German
|
||||
'es_ES.UTF-8' => 'Español', // Spanish
|
||||
'fr_FR.UTF-8' => 'Français', // French
|
||||
'el_GR.UTF-8' => 'ελληνικά', // Greek
|
||||
'it_IT.UTF-8' => 'Italiano', // Italian
|
||||
'hu_HU.UTF-8' => 'magyar', // Hungarian
|
||||
'nl_NL.UTF-8' => 'Nederlands', // Dutch
|
||||
'pl_PL.UTF-8' => 'Polski', // Polish
|
||||
'pt_PT.UTF-8' => 'Português', // Portuguese
|
||||
'pt_BR.UTF-8' => 'Português do Brasil', // Brazilian Portuguese
|
||||
'ru_RU.UTF-8' => 'Русский язык', // Russian
|
||||
'sv_SE.UTF-8' => 'Svenska', // Swedish
|
||||
'tr_TR.UTF-8' => 'Türkçe', // Turkish
|
||||
'zh_CN.UTF-8' => '簡化字', // Simplified Chinese
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(array $elements = [])
|
||||
|
@ -95,14 +108,9 @@ class Environment extends ArrayCollection
|
|||
return $this->get(self::IS_CLI) ?? ('cli' === PHP_SAPI);
|
||||
}
|
||||
|
||||
public function enableDatabase(): bool
|
||||
public function getAssetUrl(): ?string
|
||||
{
|
||||
return (bool)($this->get(self::ENABLE_DATABASE) ?? true);
|
||||
}
|
||||
|
||||
public function enableRedis(): bool
|
||||
{
|
||||
return (bool)($this->get(self::ENABLE_REDIS) ?? true);
|
||||
return $this->get(self::ASSET_URL) ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -177,6 +185,14 @@ class Environment extends ArrayCollection
|
|||
return $this->get(self::LANG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSupportedLocales(): array
|
||||
{
|
||||
return $this->get(self::SUPPORTED_LOCALES) ?? [];
|
||||
}
|
||||
|
||||
public function getReleaseChannel(): string
|
||||
{
|
||||
$channel = $this->get(self::RELEASE_CHANNEL) ?? 'latest';
|
||||
|
|
|
@ -21,7 +21,7 @@ class UserProfileForm extends EntityForm
|
|||
Environment $environment
|
||||
) {
|
||||
$form_config = $config->get('forms/profile', [
|
||||
'settings' => $environment,
|
||||
'environment' => $environment,
|
||||
]);
|
||||
parent::__construct($em, $serializer, $validator, $form_config);
|
||||
|
||||
|
|
Loading…
Reference in New Issue