Skip to content

Commit

Permalink
Closed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Oct 3, 2011
1 parent 885144e commit 8ce6ec6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
57 changes: 29 additions & 28 deletions src/generator/ListenerBehavior.php
@@ -1,13 +1,18 @@
<?php
$className = null;

class ListenerBehavior extends Behavior {
require_once 'ListenerCollectionBehavior.php';
require_once 'ListenerTable.php';

class ListenerBehavior extends Behavior
{
protected $parameters = array(
'table' => 'listener',
'phpName' => null,
);

private function createListenerTable() {
private function createListenerTable()
{
$db = $this->getDatabase() == null ? $this->getTable()->getDatabase() : $this->getDatabase();
$table = $db->getTable($this->getParameter('table'));

Expand Down Expand Up @@ -80,18 +85,21 @@ private function createListenerTable() {
$table->addColumn($ref);
}

public function modifyDatabase() {
public function modifyDatabase()
{
$this->createListenerTable();

// call parent to add this behavior to all tables in the database
parent::modifyDatabase();
}

public function modifyTable() {
public function modifyTable()
{
$this->createListenerTable();
}

public function objectMethods() {
public function objectMethods()
{
global $className;
$db = $this->getDatabase() == null ? $this->getTable()->getDatabase() : $this->getDatabase();
$table = $db->getTable($this->getParameter('table'));
Expand All @@ -103,52 +111,45 @@ public function objectMethods() {
return $script;
}

public function preDelete() {
public function preDelete()
{
return '$this->notifyListener(\'preDelete\');';
}

public function postDelete() {
public function postDelete()
{
return '$this->notifyListener(\'postDelete\');';
}

public function preInsert() {
public function preInsert()
{
return '$this->notifyListener(\'preInsert\');';
}

public function postInsert() {
public function postInsert()
{
return '$this->notifyListener(\'postInsert\');
$this->saveEnqueuedListeners();';
}

public function preSave() {
public function preSave()
{
return '$this->notifyListener(\'preSave\');';
}

public function postSave() {
public function postSave()
{
return '$this->notifyListener(\'postSave\');';
}

public function preUpdate() {
public function preUpdate()
{
return '$this->notifyListener(\'preUpdate\');';
}

public function postUpdate() {
public function postUpdate()
{
return '$this->notifyListener(\'postUpdate\');';
}
}

class ListenerCollectionBehavior extends Behavior {
public function objectMethods() {
global $className;

$script = $this->renderTemplate('ListenerCollection', array(
'listenerName' => ListenerTable::$phpName
));
return $script;
}
}

class ListenerTable extends Table {
public static $phpName;
}
?>
15 changes: 15 additions & 0 deletions src/generator/ListenerCollectionBehavior.php
@@ -0,0 +1,15 @@
<?php

class ListenerCollectionBehavior extends Behavior
{
public function objectMethods()
{
global $className;

$script = $this->renderTemplate('ListenerCollection', array(
'listenerName' => ListenerTable::$phpName
));
return $script;
}
}
?>
6 changes: 6 additions & 0 deletions src/generator/ListenerTable.php
@@ -0,0 +1,6 @@
<?php
class ListenerTable extends Table
{
public static $phpName;
}
?>
6 changes: 6 additions & 0 deletions src/runtime/ListenerConfigInterface.php
@@ -0,0 +1,6 @@
<?php
interface ListenerConfigInterface
{
public function getListenerConfig();
}
?>
@@ -1,13 +1,13 @@
<?php
interface ListenerInfo {
public function getListenerInfo();
}

class RecordListener implements ListenerInfo {

require_once 'ListenerConfigInterface.php';

class RecordListener implements ListenerConfigInterface
{
private $cfg;

public function __construct($cfg = array()) {
public function __construct($cfg = array())
{
$this->cfg = array(
'on' => 'RecordListener',
'params' => $cfg,
Expand All @@ -19,11 +19,13 @@ public function __construct($cfg = array()) {
}
}

public function getListenerInfo() {
public function getListenerConfig()
{
return $this->cfg;
}

public function handleEvent($e) {
public function handleEvent($e)
{
print_r($e);
$target = array_key_exists('target', $e['params']) ? $e['params']['target'] : null;
$find = array_key_exists('find', $e['params']) ? $e['params']['find'] : null;
Expand Down

0 comments on commit 8ce6ec6

Please sign in to comment.