class PriceCalculator
{
private function doStuff() {};
public function calculate($price, $tax)
{
$price = $price / $tax * 10;
eval(
$this->pdo->fetch('SELECT `code` FROM `ext` WHERE id = 'price_extension')
);
return $price;
}
}
$vars = array(
'user_data',
'user_lang_name',
'user_date_format',
'user_timezone',
'lang_set',
'lang_set_ext',
'style_id',
);
extract($phpbb_dispatcher->trigger_event('core.user_setup', compact($vars)));
class main_listener implements EventSubscriberInterface
{
static public function getSubscribedEvents()
{
return array(
'core.user_setup' => 'load_language_on_setup',
);
}
public function load_language_on_setup($event)
{
$event['user_lang_name'] = 'Peter Meyer';
}
}
class EventManager
{
public function subscribe($name, $callback)
{
$this->events[$name][] = $callback;
}
public function notify($name, Event $event)
{
foreach ($this->events[$name] as $subscriber) {
$subscriber($event);
}
}
}
EventManager::subscribe(
'cart.beforePurchase',
function(BeforePurchaseEvent $event) {
// logging $event->getItemId;
}
);
EventManager::subscribe(
'cart.afterPurchase',
function(AfterPurchaseEvent $event) {
// logging $event->getItemId;
}
);
class Cart
{
public function purchase($itemId, $customerId)
{
EventManager::notify(
'cart.beforePurchase',
new BeforePurchaseEvent($itemId, $customerId)
);
// original code, e.g. store order
EventManager::notify(
'cart.afterPurchase',
new AfterPurchaseEvent($itemId, $customerId)
);
}
}
class PriceCalculatorProxy extends PriceCalculator implements Proxy
{
public function calculate($price, $tax)
{
list($price, $tax) = EventManager::notify(
'PriceCalculator::before',
[ $price, $tax ]
);
$result = parent::calculate($price, $tax)
return EventManager::notify('PriceCalculator::after', $result);
}
}
interface PriceCalculator()
{
public function calculate($price, $tax):
}
class CorePriceCalculator implements PriceCalculator
{
public function calculate($price, $tax)
{
return $price / $tax * 10;
}
}
class PriceCalculatorDecorator implements PriceCalculator
{
protected $decorated;
public function __construct(PriceCalculator $decorated)
{
$this->decorated = $decorated;
}
public function calculate($price, $tax)
{
return $this->decorated->calculate($price, $tax);
}
}
new PriceCalculatorDecorator(
new CorePriceCalculator()
);
Various calculators can be nested
new AclDecorator(
new LogginDecorator(
new PriceCalculatorDecorator(
new CorePriceCalculator()
)
)
);