Files
archived-doctrine-website/lib/Application.php

123 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Doctrine\Website;
use Doctrine\Website\Commands\BuildAllCommand;
use Doctrine\Website\Commands\BuildDatabaseCommand;
use Doctrine\Website\Commands\BuildDocsCommand;
use Doctrine\Website\Commands\BuildWebsiteCommand;
use Doctrine\Website\Commands\ClearBuildCacheCommand;
use Doctrine\Website\Commands\SyncRepositoriesCommand;
use Doctrine\Website\Guides\DependencyInjection\ThemeCompilerPass;
use phpDocumentor\Guides\Code\DependencyInjection\CodeExtension;
use phpDocumentor\Guides\DependencyInjection\GuidesExtension;
use phpDocumentor\Guides\Markdown\DependencyInjection\MarkdownExtension;
use phpDocumentor\Guides\RestructuredText\DependencyInjection\ReStructuredTextExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use function assert;
use function date_default_timezone_set;
use function file_exists;
use function getenv;
use function is_string;
use function realpath;
use function sprintf;
final readonly class Application
{
public const string ENV_PROD = 'prod';
public const string ENV_STAGING = 'staging';
public function __construct(
private BaseApplication $application,
BuildAllCommand $buildAllCommand,
BuildDocsCommand $buildDocsCommand,
BuildWebsiteCommand $buildWebsiteCommand,
ClearBuildCacheCommand $clearBuildCacheCommand,
SyncRepositoriesCommand $syncRepositoriesCommand,
BuildDatabaseCommand $buildDatabaseCommand,
) {
$this->application->addCommand($buildAllCommand);
$this->application->addCommand($buildDocsCommand);
$this->application->addCommand($buildWebsiteCommand);
$this->application->addCommand($clearBuildCacheCommand);
$this->application->addCommand($syncRepositoriesCommand);
$this->application->addCommand($buildDatabaseCommand);
$this->application->setHelperSet(new HelperSet([
'question' => new QuestionHelper(),
]));
}
public function run(InputInterface $input): int
{
$inputOption = new InputOption(
'env',
'e',
InputOption::VALUE_REQUIRED,
'The environment.',
'dev',
);
$this->application->getDefinition()->addOption($inputOption);
return $this->application->run($input);
}
public function getConsoleApplication(): BaseApplication
{
return $this->application;
}
public static function getContainer(string $env): ContainerBuilder
{
$container = new ContainerBuilder();
$container->setParameter('doctrine.website.env', $env);
$container->setParameter('doctrine.website.debug', $env !== self::ENV_PROD);
$container->setParameter('doctrine.website.root_dir', realpath(__DIR__ . '/..'));
$container->setParameter('doctrine.website.config_dir', realpath(__DIR__ . '/../config'));
$container->setParameter('doctrine.website.cache_dir', realpath(__DIR__ . '/../cache'));
$container->setParameter('doctrine.website.github.http_token', getenv('doctrine_website_github_http_token'));
$container->setParameter('doctrine.website.algolia.admin_api_key', getenv('doctrine_website_algolia_admin_api_key') ?: '1234');
$container->setParameter('doctrine.website.stripe.secret_key', getenv('doctrine_website_stripe_secret_key') ?: '');
$container->setParameter('doctrine.website.send_grid.api_key', getenv('doctrine_website_send_grid_api_key') ?: '');
$container->setParameter('vendor_dir', realpath(__DIR__ . '/../vendor'));
$container->addCompilerPass(new ThemeCompilerPass());
foreach ([new GuidesExtension(), new ReStructuredTextExtension(), new MarkdownExtension(), new CodeExtension()] as $extension) {
$container->registerExtension($extension);
$container->loadFromExtension($extension->getAlias());
}
$xmlConfigLoader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$xmlConfigLoader->load('services.php');
$yamlConfigLoader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$yamlConfigLoader->load('routes.yml');
$yamlConfigLoader->load(sprintf('config_%s.yml', $env));
$yamlConfigLoader->load('packages/guides.yml');
$configDir = $container->getParameter('doctrine.website.config_dir');
assert(is_string($configDir));
if (file_exists($configDir . '/local.yml')) {
$yamlConfigLoader->load('local.yml');
}
$container->compile();
date_default_timezone_set('America/New_York');
return $container;
}
}