Skip to content

Instantly share code, notes, and snippets.

@slavcodev
Created February 24, 2016 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavcodev/9323a5ad6a39db13d8d7 to your computer and use it in GitHub Desktop.
Save slavcodev/9323a5ad6a39db13d8d7 to your computer and use it in GitHub Desktop.
<?php
/**
* Slavcodev Components
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace app\common\base;
use IteratorAggregate;
use ArrayIterator;
use yii\base\Object;
use yii\helpers\ArrayHelper;
/**
* Class GlobConfiguration
*
* Will loaded next files in given priority:
* - `/common/config/main*.php`
* - `/common/config/<env>*.php`
* - `/common/config/local*.php`
* - `/<appName>/config/main*.php`
* - `/<appName>/config/<env>*.php`
* - `/<appName>/config/local*.php`
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
*/
class GlobConfiguration extends Object implements IteratorAggregate
{
public $basePath;
public $applicationName;
public $environment;
private $config = [];
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$configPath = strtr(
'<basePath>/{common,<appName>}/config/{main,<env>,local}{.,*}.php',
[
'<basePath>' => rtrim($this->basePath, '/'),
'<appName>' => $this->applicationName,
'<env>' => $this->environment,
]
);
foreach (glob($configPath, GLOB_BRACE) as $file) {
$this->config = ArrayHelper::merge($this->config, require($file));
}
}
/**
* Do not give access to config, it's immutabe. We use an iterator instead.
*
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->config);
}
}
<?php
/**
* Yii Web application bootstrap file.
*
* @author: slavcodev <slavcopost@gmail.com>
* @license http://www.opensource.org/licenses/bsd-license.php
* @link https://github.com/slavcodev/
*
* @version: 0.4
*/
/**
* Application ID. Defaults to folder name
*/
defined('APP_ID') or define('APP_ID', basename(dirname(__DIR__)));
/**
* Application environment.
*/
defined('YII_ENV') or define('YII_ENV', getenv('APP_ENV') ?: 'dev');
/**
* Debug mode enabled on given environment.
*/
defined('YII_DEBUG') or define('YII_DEBUG', in_array(YII_ENV, ['dev']));
// Setup project directories.
$rootPath = dirname(dirname(__DIR__));
chdir($rootPath);
/**
* @var \Composer\Autoload\ClassLoader $loader Registered composer autoloader.
*/
$loader = require($rootPath . '/vendor/autoload.php');
// Register vendor namespace
$loader->addPsr4('app\\', $rootPath);
// Create application and run. Have fun!
$config = new app\common\base\GlobConfiguration([
'basePath' => $rootPath,
'applicationName' => APP_ID,
'environment' => YII_ENV,
]);
$application = new yii\web\Application(iterator_to_array($config));
$application->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment