Skip to content

Commit 8e0a466

Browse files
committed
improve helpers
1 parent 611a010 commit 8e0a466

6 files changed

Lines changed: 70 additions & 47 deletions

File tree

bootstrap/environment.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3-
use bootstrap\helpers\Dotenv;
3+
use bootstrap\helpers\DotenvInstance as Instance;
4+
use Dotenv\Dotenv;
45

5-
(new Dotenv(dirname(__DIR__)))->load();
6+
Instance::set(new Dotenv(dirname(__DIR__)))->load();

bootstrap/functions.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use bootstrap\helpers\Dotenv;
3+
use bootstrap\helpers\DotenvInstance;
44
use yii\helpers\ArrayHelper;
55

66
if (!function_exists('dotenv')) {
@@ -9,7 +9,7 @@
99
*/
1010
function dotenv()
1111
{
12-
return Dotenv::instance();
12+
return DotenvInstance::get();
1313
}
1414
}
1515

@@ -22,9 +22,9 @@ function dotenv()
2222
*/
2323
function param($key, $default = null)
2424
{
25-
$params = Yii::$app->params;
25+
$value = ArrayHelper::getValue(Yii::$app->params, $key, $default);
2626

27-
return ArrayHelper::keyExists($key, $params) ? ArrayHelper::getValue($params, $key) : $default;
27+
return value($value);
2828
}
2929
}
3030

@@ -39,7 +39,7 @@ function env($key, $default = null)
3939
{
4040
$value = getenv($key);
4141
if ($value === false) {
42-
return $default;
42+
return value($default);
4343
}
4444

4545
static $map = [
@@ -50,11 +50,22 @@ function env($key, $default = null)
5050
];
5151

5252
$key = strtolower($value);
53-
$key = ltrim($key, '(');
54-
$key = rtrim($key, ')');
53+
$key = trim($key, '()');
5554

5655
$value = isset($map[$key]) ? $map[$key] : $value;
5756

5857
return $value;
5958
}
6059
}
60+
61+
if (!function_exists('value')) {
62+
/**
63+
* @param mixed $value
64+
*
65+
* @return mixed
66+
*/
67+
function value($value)
68+
{
69+
return is_callable($value) ? call_user_func($value) : $value;
70+
}
71+
}

bootstrap/helpers/Dotenv.php

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace bootstrap\helpers;
4+
5+
use Dotenv\Dotenv;
6+
7+
/**
8+
* Class DotenvInstance
9+
*
10+
* @package bootstrap\helpers
11+
*/
12+
final class DotenvInstance
13+
{
14+
/** @var \Dotenv\Dotenv */
15+
protected static $instance;
16+
17+
/**
18+
* @return \Dotenv\Dotenv
19+
*/
20+
public static function get()
21+
{
22+
if (!self::$instance) {
23+
throw new \RuntimeException('Instance of the "Dotenv\Dotenv" has not been set.');
24+
}
25+
26+
return self::$instance;
27+
}
28+
29+
/**
30+
* @param Dotenv $instance
31+
*
32+
* @return Dotenv
33+
*/
34+
public static function set(Dotenv $instance)
35+
{
36+
self::$instance = $instance;
37+
38+
return self::$instance;
39+
}
40+
}

common/controllers/Controller.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public function behaviors()
5050
protected function verbs()
5151
{
5252
return [
53-
'create' => ['POST'],
54-
'update' => ['PUT', 'PATCH'],
55-
'delete' => ['DELETE'],
56-
'index' => ['GET', 'HEAD'],
57-
'view' => ['GET', 'HEAD'],
53+
'create' => ['post'],
54+
'update' => ['put', 'patch'],
55+
'delete' => ['delete'],
56+
'index' => ['get', 'head'],
57+
'view' => ['get', 'head'],
5858
];
5959
}
6060

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"require": {
1616
"php": ">=5.4.0",
17-
"yiisoft/yii2": ">=2.0.6",
17+
"yiisoft/yii2": ">=2.0.7",
1818
"yiisoft/yii2-swiftmailer": "*",
1919
"vlucas/phpdotenv": "~2.2"
2020
},
@@ -32,6 +32,9 @@
3232
}
3333
},
3434
"scripts": {
35+
"post-root-package-install": [
36+
"php -r \"copy('.env.example', '.env');\""
37+
],
3538
"post-create-project-cmd": [
3639
"yii\\composer\\Installer::postCreateProject"
3740
]

0 commit comments

Comments
 (0)