Created
November 10, 2010 14:49
-
-
Save fprochazka/670938 to your computer and use it in GitHub Desktop.
LogicDelegator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pokud je objekt zmrazen půjde pouze volat properties a metody | |
*/ | |
class LogicDelegator extends Nette\FreezableObject | |
{ | |
/** @var array */ | |
private $callbacks = array(); | |
/** @var mixed */ | |
private $delegate; | |
/** | |
* @param mixed $delegate | |
*/ | |
public function __construct($delegate = NULL) | |
{ | |
$this->delegate = $delegate; | |
} | |
/** | |
* @param string $method | |
* @param callable|NULL $callback | |
* @return LogicDelegator|mixed | |
*/ | |
public function __call($method, $callback = NULL) | |
{ | |
if (is_callable($callback)) { | |
$this->updating(); | |
$this->callbacks[$method] = $callback; | |
return $this; | |
} | |
return $this->callbacks[$method]($this->delegate); | |
// pokud je neznámá metoda zavolána s argumentem uložit do $this->callbacks | |
// pokud je neznámá metoda zavolána bez argumentu a je v poli callbacks tak zavolat a vrátit, jinak vyjímka | |
} | |
/** | |
* @param string $property | |
* @return mixed | |
*/ | |
public function __get($property) | |
{ | |
// pokud je čteno z neznámé property a je v poli callbacks tak zavolat a vrátit, jinak vyjímka | |
return $this->callbacks[$property]($this->delegate); | |
} | |
/** | |
* @param string $property | |
* @param callable $callback | |
*/ | |
public function __set($property, $callback) | |
{ | |
$this->updating(); | |
// pokud je do neznámé property ukládáno uložit do $this->callbacks | |
if (is_callable($callback)) { | |
$this->callbacks[$property] = $callback; | |
} | |
} | |
public function __clone() | |
{ | |
$this->updating(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// v presenteru | |
// $ld = new LogicDelegator($this); // ArticlesPresenter | |
$ld = new LogicDelegator; // TODO: vymyslet lepší konfiguraci objektů, thinkabout: closury | |
$model = new Articles; | |
$ld->list(callback($model, 'getArticles')); // $ld->callbacks['list'] = $callback | |
$ld->list = callback($model, 'getArticles'); // $ld->callbacks['list'] = $callback | |
// $ld->list(); // return $ld->callbacks['list'](); | |
// $ld->list; // return $ld->callbacks['list'](); | |
$ld->freeze(); | |
$this->template->articles = $ld; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// v presenteru | |
$configurator = (object)array( | |
'model' => new Articles, | |
'author' => $presenter->getParam('author') | |
); | |
$ld = new LogicDelegator($configurator); | |
$ld->byAuthor = function ($configurator) { | |
return $configurator->model->getArticlesByAuthor($configurator->author); | |
}; // $ld->callbacks['byAuthor'] = $callback | |
// $ld->byAuthor(); // return $ld->callbacks['list'](); | |
// $ld->byAuthor; // return $ld->callbacks['list'](); | |
$ld->freeze(); | |
$this->template->articles = $ld; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{cache 'articles', expire => '+30 minutes'} | |
{foreach $articles->list as $article} | |
{$article['title']} | |
{/foreach} | |
{/cache} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{cache 'articlesByAuthor', expire => '+30 minutes'} | |
{foreach $articles->byAuthor as $article} | |
{$article['title']} | |
{/foreach} | |
{/cache} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment