Skip to content

Commit

Permalink
improve helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
frostealth committed Feb 20, 2016
1 parent 611a010 commit 8e0a466
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 47 deletions.
5 changes: 3 additions & 2 deletions bootstrap/environment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use bootstrap\helpers\Dotenv;
use bootstrap\helpers\DotenvInstance as Instance;
use Dotenv\Dotenv;

(new Dotenv(dirname(__DIR__)))->load();
Instance::set(new Dotenv(dirname(__DIR__)))->load();
25 changes: 18 additions & 7 deletions bootstrap/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use bootstrap\helpers\Dotenv;
use bootstrap\helpers\DotenvInstance;
use yii\helpers\ArrayHelper;

if (!function_exists('dotenv')) {
Expand All @@ -9,7 +9,7 @@
*/
function dotenv()
{
return Dotenv::instance();
return DotenvInstance::get();
}
}

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

return ArrayHelper::keyExists($key, $params) ? ArrayHelper::getValue($params, $key) : $default;
return value($value);
}
}

Expand All @@ -39,7 +39,7 @@ function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return $default;
return value($default);
}

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

$key = strtolower($value);
$key = ltrim($key, '(');
$key = rtrim($key, ')');
$key = trim($key, '()');

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

return $value;
}
}

if (!function_exists('value')) {
/**
* @param mixed $value
*
* @return mixed
*/
function value($value)
{
return is_callable($value) ? call_user_func($value) : $value;
}
}
32 changes: 0 additions & 32 deletions bootstrap/helpers/Dotenv.php

This file was deleted.

40 changes: 40 additions & 0 deletions bootstrap/helpers/DotenvInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace bootstrap\helpers;

use Dotenv\Dotenv;

/**
* Class DotenvInstance
*
* @package bootstrap\helpers
*/
final class DotenvInstance
{
/** @var \Dotenv\Dotenv */
protected static $instance;

/**
* @return \Dotenv\Dotenv
*/
public static function get()
{
if (!self::$instance) {
throw new \RuntimeException('Instance of the "Dotenv\Dotenv" has not been set.');
}

return self::$instance;
}

/**
* @param Dotenv $instance
*
* @return Dotenv
*/
public static function set(Dotenv $instance)
{
self::$instance = $instance;

return self::$instance;
}
}
10 changes: 5 additions & 5 deletions common/controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public function behaviors()
protected function verbs()
{
return [
'create' => ['POST'],
'update' => ['PUT', 'PATCH'],
'delete' => ['DELETE'],
'index' => ['GET', 'HEAD'],
'view' => ['GET', 'HEAD'],
'create' => ['post'],
'update' => ['put', 'patch'],
'delete' => ['delete'],
'index' => ['get', 'head'],
'view' => ['get', 'head'],
];
}

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": ">=2.0.6",
"yiisoft/yii2": ">=2.0.7",
"yiisoft/yii2-swiftmailer": "*",
"vlucas/phpdotenv": "~2.2"
},
Expand All @@ -32,6 +32,9 @@
}
},
"scripts": {
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"yii\\composer\\Installer::postCreateProject"
]
Expand Down

0 comments on commit 8e0a466

Please sign in to comment.