Mecha CMS

Mecha CMS blog and documentation.

Class Config

Updated: Sunday, 07 August 2016

Configuration getter and setter, as well as a registry.

Perform a Test

var_dump(Config::get());

Results:

array(
    'timezone' => 'Asia/Jakarta',
    'charset' => 'utf-8',
    'language' => 'en_US',
    'language_direction' => 'ltr',
    'shield' => 'normal',
    'per_page' => 7,
    'comments' => array(
        'allow' => true,
        'moderation' => false
    ),
    'excerpt' => array(
        'length' => 300,
        'prefix' => "",
        'suffix' => '…',
        'id' => 'more:%d'
    ),
    'html_parser' => array(
        'type' => array(
            'HTML' => 'HTML',
            'Markdown Extra' => 'Markdown Extra'
        ),
        'active' => 'HTML'
    ),
    'widget_include_css' => true,
    'widget_include_js' => true,
    'title' => 'My Awesome Site',
    'title_separator' => ' – ',
    'slogan' => 'Site slogan goes here.',
    'description' => 'Site description goes here.',
    'keywords' => 'blog, diary, notes, personal',
    'keywords_spam' => "",
    'author' => array(
        'name' => 'Author Name',
        'email' => 'email@domain.com',
        'url' => ""
    ),
    'index' => array(
        'title' => 'Article',
        'slug' => 'article',
        'per_page' => 7
    ),
    'tag' => array(
        'title' => 'Tagged in %s',
        'slug' => 'tag',
        'per_page' => 7
    ),
    'archive' => array(
        'title' => 'Archive %s',
        'slug' => 'archive',
        'per_page' => 7
    ),
    'search' => array(
        'title' => 'Search Results for “%s”',
        'slug' => 'search',
        'per_page' => 7
    ),
    'manager' => array(
        'title' => 'Manager',
        'slug' => 'manager',
        'per_page' => 7
    ),
    'defaults' => array(
        'article_title' => "",
        'article_content' => "",
        'article_css' => "<style media=\"screen\">\n\n</style>",
        'article_js' => "<script>\n\n</script>",
        'page_title' => "",
        'page_content' => "",
        'page_css' => "<style media=\"screen\">\n\n</style>",
        'page_js' => "<script>\n\n</script>"
    )
);

Automatic Configuration Data

The automated data configurations are…

KeyPossible ValuesDescription
schemehttp or httpsReturn the URL scheme.
protocolhttp:// or https://Return the URL protocol.
hostexample.orgReturn the URL host name.
baseEmpty or a folder name.Return the base folder where the CMS is installed. If you installed this CMS in a sub-folder, then this property will return the folder name.
urlhttp://example.orgReturn the home page URL.
pathsome/pathReturn the URL path relative to home URL.
query?foo=bar&baz=quxReturn the URL query string.
currenthttp://example.org/some/pathReturn the current page URL.
originhttp://example.org/some/pathReturn the previous URL before page redirection via [<code>Guardian::kick()</code>][article:class-guardian#reference:url-redirection].
url_schemeAlias for scheme.
url_protocolAlias for protocol.
url_hostAlias for host.
url_baseAlias for base.
url_urlAlias for url.
url_pathAlias for path.
url_queryAlias for query.
url_currentAlias for current.
url_originAlias for origin.
page_titleLorem Ipsum – Blog TitleReturn the current page title.
page_typehome, index, tag, archive, search, manager, 404Return the current [page type][article:page-types].
offset1Return the current page offset (for paginated pages).
index_queryReturn the query of index page for page results filtering.
tag_querytag-slugReturn the query of tag page for page results filtering.
archive_query2014, 2014-04, 2014-04-21Return the query of archive page for page results filtering.
search_querysearch queryReturn the query of search page for page results filtering.
pageReturn the current page data, or false.
pagesReturn the current pages data, or false.
articleReturn the current article data, or false.
articlesReturn the current articles data, or false.
paginationReturn the current pagination URL and anchor.
cargopath\to\workers\file.phpReturn the file path of cargo to be loaded into page content (especially in manager page).
total_articles20Return the total of the articles.
total_pages4Return the total of the pages.
total_comments200Return the total of the comments.
__total_articles20Return the total of the articles, including the drafts.
__total_pages4Return the total of the pages including the drafts.
__total_comments200Return the total of the comments, including the unapproved comments.
speakReturn the language data to speak.

Set New Variable

Set new variable with Config::set('key', 'value'):

Config::set('test', 'OK!');

After that, you can call the value of test anywhere in the page after the declaration above this way:

echo Config::get('test'); // => `OK!`

Or you could also set multiple variables with the help of array:

Config::set(array(
    'test' => 'OK!',
    'foo' => 'bar'
));

There is also a shortcut variable for calling the configuration data called $config. Only available in the shields:

echo $config->test; // => `OK!`

Different Method Calls on the Configuration Data

All of these implementations are work:

echo Config::get('url');
echo Config::get('index')->slug;
$config = Config::get();
echo $config->url;
echo $config->index->slug;

Get array data recursively using dot notation:

Added since version 1.0.2.

echo Config::get('index.slug');
echo Config::get('foo.bar')->baz;
$test = Config::get('foo.bar');
echo $test->baz;

Get Multiple Data

Added since version 1.2.0.

$config = Config::get(array('url', 'base', 'protocol'));

With fallback values:

$config = Config::get(array('url', 'base', 'protocol'), false);
$config = Config::get(array('url', 'base', 'protocol'), array(
    'url' => "",
    'base' => "",
    'protocol' => 'http://'
));

Merging

Combine array with the targetted array data by its key:

Config::merge('speak', array(
    'cute' => 'manis'
));

Merge array data recursively using dot notation:

Added since version 1.0.2.

Config::merge('foo.bar.baz', array(
    'lorem' => 'ipsum',
    'dolor' => array(
        'sit' => 'amet'
    )
));

Shortcut for URL Data

echo Config::url($key);
MethodDescription
Config::url()Is equal to Config::get('url')
Config::url('scheme')Is equal to Config::get('url_scheme')
Config::url('protocol')Is equal to Config::get('url_protocol')
Config::url('host')Is equal to Config::get('url_host')
Config::url('base')Is equal to Config::get('url_base')
Config::url('path')Is equal to Config::get('url_path')
Config::url('query')Is equal to Config::get('url_query')
Config::url('current')Is equal to Config::get('url_current')
Config::url('origin')Is equal to Config::get('url_origin')

Shortcut for Language Data

To call a language item, use this method:

echo Config::speak('my_var');

There is also a shortcut variable for calling the language data called $speak. Only available in the shields:

echo $speak->my_var;

Actually, this is just a shortcut for Config::get('speak'). But, with extra features:

Parsing Values with Formatted Strings

echo Config::speak('foo', array('apple', 'banana'));

… to handle some language data with variables in it:

Lorem ipsum %s dolor sit %s amet.

The result would becomes like this:

Lorem ipsum apple dolor sit banana amet.

Different Method Calls on the Language Data

All of these implementations are work:

echo Config::speak('home');
echo Config::speak('form')->title;
$speak = Config::speak();
echo $speak->home;
echo $speak->form->title;

Get language data recursively using dot notation:

Added since version 1.0.2.

echo Config::speak('form.title');
Config::speak('foo.bar')->baz;
$test = Config::speak('foo.bar');
echo $test->baz;

Creating Your Own Method

Default

You can call any configuration data with the key itself as a method as long as the key you want to call is not defined yet by the Config::plug() method:

echo Config::shield();

… is equal to:

echo Config::get('shield');

Custom

Config::plug('session', function($key = null, $fallback = false) {
    return Session::get($key, $fallback);
});

Usage:

echo Config::session('user');
Donation and Email Subscription