Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Created November 10, 2010 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fprochazka/670938 to your computer and use it in GitHub Desktop.
Save fprochazka/670938 to your computer and use it in GitHub Desktop.
LogicDelegator
<?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();
}
}
<?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;
<?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;
{cache 'articles', expire => '+30 minutes'}
{foreach $articles->list as $article}
{$article['title']}
{/foreach}
{/cache}
{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