A generic builder for application construction based on various server environment.
MIT License
PSR-1, PSR-2, and PSR-4.
composer require "tuum/builder: ^1.0.0"
use WScore\Builder\Builder;
$builder = Builder::forge(
__DIR__ . '/config, // app directory
__DIR__ . '/var', // var directory
true // debug
);
$builder->loadEnv();
$builder->load('setup');
$builder->load('routes');
if ($builder->isEnv('local')) {
$builder->load('extra.local');
}
$app = $builder->getApp(); // <- must set an app using setApp()!
Tuum/Builder
assumes there are two directories to build an application:
APP_DIR
: directory for application settings, andVAR_DIR
: directory for files not under version control.
For instance,
+ config/ // <- APP_DIR
+ setup.php
+ routes.php
+ var/ // <- VAR_DIR
+ .env
+ cache/
Construct the application builder with two directories:
use WScore\Builder\Builder;
$builder = new Builder([
Builder::APP_DIR => __DIR__ . '/config, // app directory
Builder::VAR_DIR => __DIR__ . '/var', // var directory
Builder::DEBUG => true // debug
]);
Or, simply use forge
method as shown in the Sample Code Section.
To load configuration files under the APP_DIR
, use load
method as;
$builder->load('setup');
In setup.php
file, set up the application, such as:
/** @var Tuum\Builder\Builder $builder */
$builder->set(Builder::APP_KEY, 'ENV'); // set value
$builder->setApp(new YourApp()); // set your application
return [
'db-name' => $builder->get('DB_NAME', 'demo'),
]; // may return some value
- The builder has
has
,get
, andset
methods as expected. - There are
setApp()
andgetApp()
methods to store your application. - The returned value from the PHP files are stored in the builder
using its load name, which can be accessed by:
$builder->get('setup');
.
The get
method tries to get value from:
- environment value,
$builder
's internal value- default value.
$builder->get('DB_NAME', 'my_db');
Loads .env
file using vlucas's dotenv component.
The default location of the .env
file is at VAR_DIR
.
The .env
file contains APP_ENV
key to specify the environment as such;
APP_ENV = local
Then, you can access the environment as,
$builder->loadEnv(); // load the .env file
$builder->isEnv('local');
$builder->isEnvProd();
$env = $builder->getEnv();
The builder considers the environment as prod
if no environment is set, or no environment file to load.
To change the key string used to specify the environment,
set Builder::APP_KEY
value to the new key name, such as;
$builder->set(Builder::APP_KEY, 'ENV'); // set value