mardi 10 mars 2015

Regarding Registry design pattern flow in below code

I'm new to PHP, I'm not able to understand exact the flow and logic of the program for below code, Below init is calling from constructor, I can see it has services like Mailer,facebook,request and It is storing in registry





protected function init()
{

$this->values['Request'] = $this->asShared(function ($c)
{
return new Request($c->Router, $c->Ini);
});

$this->values['Ini'] = $this->asShared(function ($c)
{
return new \Lampcms\Config\Ini();
});

$this->values['Mongo'] = $this->asShared(function ($c)
{
return new \Lampcms\Mongo\DB($c->Ini);
});

$this->values['Mailer'] = $this->asShared(function ($c)
{
return new \Lampcms\Mail\Mailer($c->Ini);
});

$this->values['Router'] = $this->asShared(function ($c)
{
return new \Lampcms\Uri\Router($c->Ini);
});

$this->values['Facebook'] = $this->asShared(function ($c)
{
return new \Lampcms\Modules\Facebook\Client($c);
});

....
.....
}






public function asShared($callable)
{
return function ($c) use ($callable)
{
static $object;
if (\is_null($object)) {
$object = $callable($c);
}

return $object;
};
}



In another module we have seen below code



$callback = function($output) use ($mapper, $translator, $renderTimeString)
{
$scriptTime = '';

/**
* Translate URL placeholders
* These are routes and various url parts like {_WEB_ROOT_} for example
*/
$output = $mapper($output);

/**
* Now replace translation strings
* identified as
*
*
* @@somestring@@
*
*/
$output = $translator($output);


/**
* Timer calculation should be the last replacement
* in order to count all other replacements
*/
if (true == constant('LAMPCMS_SHOW_RENDER_TIME')) {

$scriptTime = $renderTimeString . ' ' . abs((microtime() - INIT_TIMESTAMP));
}

$output = \str_replace('{timer}', $scriptTime, $output);

return $output;
};



Please explain the below code Registry pattern and how it works(flow), and how "use" keyword will work in function


Aucun commentaire:

Enregistrer un commentaire