Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use env variables instead of constants #193

Merged
merged 24 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YII_ENV=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about proving a default value here?

Suggested change
YII_ENV=
YII_ENV=dev

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is '/'. See config plugin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's usually not only config environment.

YII_DEBUG=true
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch",
"post-update-cmd": [
"App\\Installer::postUpdate"
"App\\Installer::postUpdate",
"App\\Installer::copyEnvFile"
],
"post-create-project-cmd": [
"App\\Installer::copyEnvFile"
]
},
"require": {
Expand All @@ -23,6 +27,7 @@
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"symfony/console": "^5.3",
"vlucas/phpdotenv": "^5.3",
"yiisoft/aliases": "^2.0",
"yiisoft/assets": "^1.0",
"yiisoft/cache": "^1.0",
Expand All @@ -42,12 +47,12 @@
"yiisoft/log-target-file": "^1.0",
"yiisoft/router": "@dev",
"yiisoft/router-fastroute": "@dev",
"yiisoft/view": "^2.0",
"yiisoft/view": "^3.0",
"yiisoft/yii-bulma": "@dev",
"yiisoft/yii-console": "3.0.x-dev",
"yiisoft/yii-debug": "^3.0@dev",
"yiisoft/yii-event": "^1.0",
"yiisoft/yii-view": "^2.0",
"yiisoft/yii-view": "^3.0",
"yiisoft/yii-web": "^3.0@dev"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use App\Command\Hello;
use App\ViewInjection\ContentViewInjection;
use App\ViewInjection\CommonViewInjection;
use App\ViewInjection\LayoutViewInjection;
use Yiisoft\Definitions\Reference;
use Yiisoft\Yii\View\CsrfViewInjection;
Expand Down Expand Up @@ -34,7 +34,7 @@

'yiisoft/yii-view' => [
'injections' => [
Reference::to(ContentViewInjection::class),
Reference::to(CommonViewInjection::class),
Reference::to(CsrfViewInjection::class),
Reference::to(LayoutViewInjection::class),
],
Expand Down
20 changes: 20 additions & 0 deletions preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Dotenv\Dotenv;

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

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$_ENV['YII_ENV'] = empty($_ENV['YII_ENV']) ? null : (string)$_ENV['YII_ENV'];
$_SERVER['YII_ENV'] = $_ENV['YII_ENV'];

$_ENV['YII_DEBUG'] = filter_var(
!empty($_ENV['YII_DEBUG']) ? $_ENV['YII_DEBUG'] : true,
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
) ?? true;
$_SERVER['YII_DEBUG'] = $_ENV['YII_DEBUG'];
samdark marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="src"/>
<file name="public/index.php"/>
<file name="yii"/>
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
<file name="preload.php"/>
</projectFiles>
</psalm>

42 changes: 0 additions & 42 deletions public/index-test.php

This file was deleted.

27 changes: 13 additions & 14 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

use App\Runner\WebApplicationRunner;

/**
* @psalm-var string $_SERVER['REQUEST_URI']
*/

// PHP built-in server routing.
if (PHP_SAPI === 'cli-server') {
// Serve static files as is.
/** @psalm-suppress MixedArgument */
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (is_file(__DIR__ . $path)) {
return false;
Expand All @@ -16,21 +21,15 @@
$_SERVER['SCRIPT_NAME'] = '/index.php';
}

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

/**
* Set debug value for web application runner, for default its `true` add additionally the validation of the
* container-di configurations (debug mode).
*/
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
require_once dirname(__DIR__) . '/preload.php';

/**
* Set environment value for web application runner, for default its `null`.
*
* @link https://github.com/yiisoft/config#environments
*/
define('YII_ENV', getenv('YII_ENV') ?: null);
if ($_ENV['YII_ENV'] === 'test') {
vjik marked this conversation as resolved.
Show resolved Hide resolved
$c3 = dirname(__DIR__) . '/c3.php';
if (file_exists($c3)) {
require_once $c3;
}
}

// Run web application runner
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
$runner = new WebApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
$runner->run();
7 changes: 4 additions & 3 deletions resources/views/site/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

/** @var App\ApplicationParameters $applicationParameters */

$this->params['breadcrumbs'] = '/';
/**
* @var \Yiisoft\View\WebView $this
* @var \App\ApplicationParameters $applicationParameters
*/

$this->setTitle($applicationParameters->getName());
?>
Expand Down
7 changes: 7 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ private static function chmodRecursive(string $path, int $mode): void
chmod($item, $mode);
}
}

public static function copyEnvFile(): void
{
if (!file_exists('.env')) {
copy('.env.example', '.env');
}
}
}
5 changes: 4 additions & 1 deletion src/Runner/ConsoleApplicationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
use Yiisoft\Definitions\Exception\NotFoundException;
use Yiisoft\Definitions\Exception\NotInstantiableException;
use Yiisoft\Yii\Console\Application;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;

use function dirname;

final class ConsoleApplicationRunner
{
private bool $debug;
Expand Down Expand Up @@ -61,7 +64,7 @@ public function run(): void

/** @var Application */
$application = $container->get(Application::class);
$exitCode = 1;
$exitCode = ExitCode::UNSPECIFIED_ERROR;

try {
$application->start();
Expand Down
4 changes: 1 addition & 3 deletions src/Runner/WebApplicationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ private function emit(RequestInterface $request, ResponseInterface $response): v
*/
private function registerErrorHandler(ErrorHandler $registered, ErrorHandler $unregistered = null): void
{
if ($unregistered !== null) {
$unregistered->unregister();
}
$unregistered?->unregister();

if ($this->debug) {
$registered->debug();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace App\ViewInjection;

use App\ApplicationParameters;
use Yiisoft\Yii\View\ContentParametersInjectionInterface;
use Yiisoft\Yii\View\CommonParametersInjectionInterface;

final class ContentViewInjection implements ContentParametersInjectionInterface
final class CommonViewInjection implements CommonParametersInjectionInterface
{
private ApplicationParameters $applicationParameters;

Expand All @@ -17,7 +17,7 @@ public function __construct(
$this->applicationParameters = $applicationParameters;
}

public function getContentParameters(): array
public function getCommonParameters(): array
{
return [
'applicationParameters' => $this->applicationParameters,
Expand Down
5 changes: 0 additions & 5 deletions src/ViewInjection/LayoutViewInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\ViewInjection;

use App\ApplicationParameters;
use Yiisoft\Assets\AssetManager;
use Yiisoft\I18n\Locale;
use Yiisoft\Router\UrlGeneratorInterface;
Expand All @@ -13,20 +12,17 @@

final class LayoutViewInjection implements LayoutParametersInjectionInterface
{
private ApplicationParameters $applicationParameters;
private AssetManager $assetManager;
private Locale $locale;
private UrlGeneratorInterface $urlGenerator;
private CurrentRoute $currentRoute;

public function __construct(
ApplicationParameters $applicationParameters,
AssetManager $assetManager,
Locale $locale,
UrlGeneratorInterface $urlGenerator,
CurrentRoute $currentRoute
) {
$this->applicationParameters = $applicationParameters;
$this->assetManager = $assetManager;
$this->locale = $locale;
$this->urlGenerator = $urlGenerator;
Expand All @@ -36,7 +32,6 @@ public function __construct(
public function getLayoutParameters(): array
{
return [
'applicationParameters' => $this->applicationParameters,
'assetManager' => $this->assetManager,
'locale' => $this->locale,
'urlGenerator' => $this->urlGenerator,
Expand Down
19 changes: 3 additions & 16 deletions yii
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ declare(strict_types=1);

use App\Runner\ConsoleApplicationRunner;

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

/**
* Set debug value for console application runner, for default its `true` add additionally the validation of the
* container-di configurations (debug mode).
*/
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);

/**
* Set environment value for web application runner, for default its `null`.
*
* @link https://github.com/yiisoft/config#environments
*/
define('YII_ENV', getenv('YII_ENV') ?: null);
require_once __DIR__ . '/preload.php';

// Run console application runner
$consoleRunner = new ConsoleApplicationRunner(YII_DEBUG, YII_ENV);
$consoleRunner->run();
$runner = new ConsoleApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
samdark marked this conversation as resolved.
Show resolved Hide resolved
$runner->run();