Difference between #1 and #2 of Yiinitializr: The Library

Yiinitializr: The Library
yii, yiinitializr, application, configuration
For those who have created a project boilerplate with <a
target="_blank">Yiinitializr site</a>, you will find that is
not just a project boilerplate what you just downloaded. Within the boilerplate
comes a library named: <a href="http://yiinitializr.2amigos.us/"
target="_blank">Yiinitializr</a> (not very original I know).
The classes within this tiny library are simple, yet powerful, and will help you
build your own project setup, the one that really fits your needs for your <a
href="http://www.yiiframework.com" target="_blank">Yii
#The<h1>The Classes
The <a href="http://yiinitializr.2amigos.us/"
target="_blank">Yiinitializr</a> classes will help boost
your application installation with ease and also to run <a
target="_blank">Yii</a> applications from its bootstrap
files on a much cleaner way that the framework currently proposes. Let's have a
look at the following example:
// if installed via composer (pretty neat right?)

Yiinitializr\Helpers\Initializer::create('./../', 'frontend', array(
    // will override frontend configuration file
    __DIR__ .'/../../common/config/main.php', // merged with
    __DIR__ .'/../../common/config/env.php', // merged with
    __DIR__ .'/../../common/config/local.php' // merged with
##Yiinitializr\Cli namespace<h2>Yiinitializr\Cli
This namespace brings utilities to interact with the terminal or 'cli'. <a
href="https://github.com/nramenta/clio">A free beer goes to mr
<strong>nramenta</strong> for them</a>.
The Console class helps you to display information to the console. You probably,
think that it as easy as to 'echo' from any of your Command components that are
runed by the Yiic command and is true, but this class comes with some magic:
Console::output('this is %rcolored%n and %Bstyled%n');
$db_host = Console::prompt('database host', ['default' => 'localhost']);
$opt = Console::select('Do you love Yii?',
    ['y' => 'yes', 'n' => 'no', 'a' => 'all']
$sure = Console::confirm('are you sure?');
Console::stdout('Working ... ');
Console::work(function($socket) { // $socket is optional, defaults to a spinner
    $n = 100;
    for ($i = 1; $i <= $n; $i++) {
        // do whatever it is you need to do
        socket_write($socket, "[$i/$n]\n");
        sleep(1); // sleep is good for you
The Daemon class provides helpers for starting and killing daemonized processes.

use Yiinitializr\Cli\Daemon;

if (Daemon::isRunning('/path/to/process.pid')) {
    echo "daemon is running.\n";
} else {
    echo "daemon is not running.\n";
##Yiinitializr\Composer namespace<h2>Yiinitializr\Composer
I should just call this namespace <strong>Tobias Schmunk</strong>
as he was the master guru behind this class, the only thing I did was to read
the e-book of <strong><a
href="http://getcomposer.org">Composer</a></strong> and
studied every single piece of his fabulous <a
application boilerplate. To modify the class to become of more general usage out
of <strong><a
href="http://www.phundament.com/">Phundament</a> </strong>has
no much credit.

Here, we find the Callback class, a super useful class that will allow you to
execute commands prior, post <strong>Composer</strong> extensions
installations or updates. It makes use of the scripts configuration feature and
even though for the current templates found at <strong><a
Site</a></strong> the only thing it does is to call
<strong>Yiic migrate</strong> commands but I am sure that you are
already thinking of its possibilities.
##Yiinitializr\Helpers namespace<h2>Yiinitializr\Helpers
This class includes cool functions to work with arrays. It contains one cloned
from <strong>CMap</strong>, the
<strong>mergeArray</strong> function, as we require this method in
order to combine the different configuration files before the Yii application is
even included. A clear example of it you find it on
<strong>Yiinitializr/config/console</strong>, which is the file that
where you set the configuration of your console application to run Composer
callbacks. It has also
It reads the configuration file and provides an interface to access the
settings. It also, when working with different environments, writes the
"<em>env.lock</em>" file. By creating this file, it tells
the <strong>Initializr</strong> helper class to not recreate the
env.php configuration file of the environment selected. If anybody wishes to
recreate them, in order to make use of other environment configuration file (ie.
stage vs local), the coder just needs to delete the
"<em>env.lock</em>" file from the
<strong>Yiinitializr/config</strong> folder.
<h4>The configuration file</h4>
As with Yii, you need to go through a bit of configuration settings if you wish
to handle your project structure initializing setup
with <code>Yiinitializr</code>. But don't worry, is not going to be
too hard, the following is an example configuration file:

\\ where am i?
$dirname = dirname(__FILE__);
\\ where is the application folder?
$app = $dirname . '/../../..';
\\ where is the root?
$root = $app . '/..';

return array(
    // yii configurations
    'yii' => array(
        // where is the path of the yii framework?
        // On this example we have installed yii with composer
        // and as it is used after composer installation, we
        // can safely point to the vendor folder.
        'path' => $app . '/lib/vendor/yiisoft/yii/framework'
    // yiinitializr specific settings
    'yiinitializr' => array(
        // config folders
        'config' => array(
            // we just need the console settings
            // On this example, and due that I used environments
            // i created a custom console.php app for
            // Yiinitializr\Composer\Callbak class
            'console' => $dirname . '/console.php'
        // application structure settings
        'app' => array(
            // where is the root?
            'root' => $root,
            // directories setup
            'directories' => array(
                // where are the different configuration files settings?
                'config' => array(
                    // 'key' is the configuration name (see above init example)
                    'main' => $app . '/config',
                    'console' => $app . '/config',
                    'test' => $app . '/config'
                // where are my runtime folders?
                'runtime' => array(
                    // heads up! only the folder location as "/config"
will be
                    // appended
                'assets' => array(
                    // where to write the "assets folders"?
                    $root . '/www'
            'files' => array(
                // files to merge the main configuration file with
                // initializr will merge it automatically
                'config' => array(
                    'main' => array('env', 'local'),
                    'console' => array('env', 'local'),
This class requires a little bit more attention and I hope you take a bit of
time reading its code to truly understand what it does. Improving this class,
can make our lives easier with <a
target="_blank">Yii</a> and I explain why:
	<li>It tries to solve the problem with YII_DEBUG dynamically (no need to
hardcode true|false on your index.php bootstrap file) - check the <a
target="_blank">setOptions function</a>.</li>
	<li>It creates all necessary read+write folders like runtime and assets
(Yiinitializr works in conjunction with Composer but I am pretty sure some of
you are already thinking on why is not <a
target="_blank">Yii</a> creating those folders when they are
not found? Initializer class may provide the solution:<strong> if it
doesn't exist, build it don't fail on error!</strong>)</li>
	<li>Automatically merges different configuration files that you may have
for the different type of applications - check the <a
target="_blank">config function</a>.</li>
#Final Notes<h1>Final Notes</h1>
We truly hope at <a href="http://2amigos.us"
target="_blank">2amigOS!</a> that you find this library as
useful as it is for us. We do not expect that this procedure should be
implemented at <a href="http://www.yiiframework.com"
target="_blank">Yii</a>'s core but some of the features
included in this small library are certainly something to consider on its future
releases. Happy Yii coding!
<h1 id="hh1">References</h1>
Basic Template</a></li>
Intermediate Template</a></li>
Advanced Template</a></li>
<i>web development has never been so fun</i>