Skip to content

Core\Sharer

Fariz Luqman edited this page Jun 29, 2016 · 4 revisions

Sharer

The Sharer is a tool used in the dependency injection. This process of injecting dependency is nicknamed as "sharing" in Damn Stupid Simple framework.

Sharer acts as a "bridge" between two totally different scopes, for example the bootstrap and View files. With sharer you will be able to access references to objects or variables defined not in the current scope.

Sharer's main responsibility is to get the App container Core\App and then share it to be extracted in View files. See this file.

However, users will be able to use the same tool to share any other variables or objects.

Sharer has been used in the bootstrap.php file:

/*
|--------------------------------------------------------------------------
| Creating the Container
|--------------------------------------------------------------------------
|
| Damn Stupid Simple uses the Container to simplify coordinations, while
| maintaining only one instantiation of a class.
|
*/
$app = new Core\App;

/*
|--------------------------------------------------------------------------
| Linking to the Database Connector
|--------------------------------------------------------------------------
|
| Connect the database for only once. Save the planet.
|
*/
$app->link('database', Core\Database::connect());

/*
|--------------------------------------------------------------------------
| Share the Container $app with the Template Files
|--------------------------------------------------------------------------
|
| Eliminate complexity, get the job done.
|
*/
Core\Sharer::share('app', $app);

Which will let you do this in the View files (or template):

$users = $app->database
         ->table('users')
         ->where('id','1')
         ->get();

Sharing and getting variables

First, let say you will create these variables:

$myinteger = 100;
$myboolean = true;
$myobject = new MyObject();

Then, you can do this to share references to those variables:

Core\Sharer::share('myinteger', $myinteger);
Core\Sharer::share('myboolean', $myboolean);
Core\Sharer::share('anyothername', $myobject);

You can get all of the shared variables and then extract them later anywhere (this has been done automatically in Core\Viewer):

extract(Core\Sharer::get());

Which will allow you to access them directly in the View files or anywhere else:

<html>
    <body>
          <p>Value of myinteger <?= $myinteger ?></p>
          <p>Value of myboolean <?= $myboolean ?></p>
          <p>Value of myobject <? $anyothername->output(); ?></p>
    </body>
</html>

Visit our website for tutorials: stupidlysimple.github.io

Clone this wiki locally