Skip to content

Commit

Permalink
Clean up the app
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Dec 3, 2023
1 parent b8c42ca commit e8f84a4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 144 deletions.
38 changes: 7 additions & 31 deletions app/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,15 @@

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\ErrorHandler\DebugClassLoader;

set_time_limit(0);

require __DIR__.'/../vendor/autoload.php';

if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev');
$debug = ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']);

if ($debug) {
umask(0000);
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

if (class_exists(Debug::class)) {
Debug::enable();
DebugClassLoader::disable();
}
}
return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

$kernel = new Kernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
return new Application($kernel);
};
4 changes: 3 additions & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"symfony/mailer": "^6.4 || ^7.0",
"symfony/monolog-bundle": "^3.1",
"symfony/rate-limiter": "^6.4 || ^7.0",
"symfony/runtime": "^6.4 || ^7.0",
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/translation": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
Expand All @@ -33,7 +34,8 @@
},
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": false
"composer/package-versions-deprecated": false,
"symfony/runtime": true
}
},
"autoload": {
Expand Down
12 changes: 6 additions & 6 deletions app/config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

require dirname(__DIR__).'/vendor/autoload.php';

if (!class_exists(Dotenv::class)) {
throw new LogicException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
}

// Load cached env vars if the .env.local.php file exists
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
if (is_array($env = @include dirname(__DIR__).'/.env.local.php') && ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? $env['APP_ENV']) === $env['APP_ENV']) {
foreach ($env as $k => $v) {
$_ENV[$k] = $_ENV[$k] ?? (isset($_SERVER[$k]) && !str_starts_with($k, 'HTTP_') ? $_SERVER[$k] : $v);
}
} elseif (!class_exists(Dotenv::class)) {
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
if (is_array($env = @include dirname(__DIR__).'/.env.local.php') && (!isset($env['APP_ENV']) || ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? $env['APP_ENV']) === $env['APP_ENV'])) {
(new Dotenv(false))->populate($env);
} else {
// load all the .env files
(new Dotenv(false))->loadEnv(dirname(__DIR__).'/.env');
Expand Down
19 changes: 3 additions & 16 deletions app/config/packages/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use App\Entity\User;
use App\Kernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$config = [
Expand All @@ -15,6 +14,9 @@
],
],
],
'password_hashers' => [
User::class => ['algorithm' => 'sha1'],
],
'firewalls' => [
'dev' => [
'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
Expand Down Expand Up @@ -63,20 +65,5 @@
],
];

// Symfony 5.4
if (Kernel::VERSION_ID < 60000) {
$config = array_replace_recursive($config, [
'encoders' => [
User::class => ['algorithm' => 'sha1'],
],
]);
} else {
$config = array_replace_recursive($config, [
'password_hashers' => [
User::class => ['algorithm' => 'sha1'],
],
]);
}

/** @var ContainerBuilder $container */
$container->loadFromExtension('security', $config);
4 changes: 0 additions & 4 deletions app/config/preload.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

if (file_exists(dirname(__DIR__).'/var/cache/prod/srcApp_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/srcApp_KernelProdContainer.preload.php';
}

if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
}
3 changes: 0 additions & 3 deletions app/config/routes/annotations.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions app/config/routes/attributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
controllers:
resource:
path: ../../src/Controller/
namespace: App\Controller
type: attribute

kernel:
resource: App\Kernel
type: attribute
28 changes: 4 additions & 24 deletions app/public/index.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
<?php

use App\Kernel;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\ErrorHandler\DebugClassLoader;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/config/bootstrap.php';
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

if ($_SERVER['APP_DEBUG']) {
umask(0000);

Debug::enable();
DebugClassLoader::disable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
return function (array $context) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
59 changes: 0 additions & 59 deletions app/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,9 @@
namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use function is_dir;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

private const CONFIG_EXTS = '.{php,xml,yaml,yml}';

public function getCacheDir(): string
{
return $this->getProjectDir().'/var/cache/'.$this->environment;
}

public function getLogDir(): string
{
return $this->getProjectDir().'/var/log';
}

/**
* @return iterable<object>
*/
public function registerBundles(): iterable
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (!isset($envs['all']) && !isset($envs[$this->environment])) {
continue;
}

yield new $class();
}
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->setParameter('container.autowiring.strict_mode', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir().'/config';
$loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
if (is_dir($confDir.'/packages/'.$this->environment)) {
$loader->load($confDir.'/packages/'.$this->environment.'/*'.self::CONFIG_EXTS, 'glob');
}

$loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
}

protected function configureRoutes(RoutingConfigurator $routes): void
{
$confDir = $this->getProjectDir().'/config';
if (is_dir($confDir.'/routes/')) {
$routes->import($confDir.'/routes/*'.self::CONFIG_EXTS);
}

if (is_dir($confDir.'/routes/'.$this->environment)) {
$routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS);
}

$routes->import($confDir.'/routes'.self::CONFIG_EXTS);
}
}

0 comments on commit e8f84a4

Please sign in to comment.