Permalink
Browse files

- Added phpName Parameter

- Fixes along this
  • Loading branch information...
1 parent b3d1c8a commit 21d128ba110fb9700134e1b1d5eeb2af6f2272ff @gossi committed Jul 29, 2011
View
@@ -17,7 +17,7 @@
</thead>
<tbody>
<?php
- foreach (Listener::getListeners() as $t) {
+ foreach (Listener::getGlobalListeners() as $t) {
foreach ($t as $e) {
foreach ($e as $l) {
printf('<tr>
View
@@ -48,16 +48,34 @@ Table:
</database>
However, this will create a new table (and thus a new object) in your model to store your
-listeners. By default this table is `listener`. You can change this by passing the `table`
-parameter to the behavior, to prevent interfering usage with your model, like this:
+listeners. By default this table is `listener`. You can change this using parameters.
+
+*Note: Obviously this behavior can be added to tables, there is not much sense in it. This
+behavior is best placed as a database behavior.*
+
+#### Parameters
+Parameters to modify the behavior.
+
+##### Table Parameter
+With the `table` you can name the listener table in your schema. By default this is
+`listener`. By passing the `table` parameter to the behavior, to prevent interfering usage
+with your model, like this:
<behavior name="listener">
- <param name="table" value="my_listener_table"/>
+ <parameter name="table" value="my_listener_table"/>
</behavior>
-*Note: Obviously this behavior can be added to tables, there is not much sense in it. This
-behavior is best placed as a database behavior.*
+##### phpName Parameter
+If you don't want the generated propel object named after your table parameter you can change
+modify this with the `phpName` parameter:
+
+ <behavior name="listener">
+ <parameter name="table" value="my_listener_table"/>
+ <parameter name="table" value="Listener"/>
+ </behavior>
+This will create a table named `my_listener_table` and a propel object `Listener`.
+
### Listener Events
You can add listeners very differently, according to your needs, offering more and more
View
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<database name="listeners">
+ <behavior name="listener"/>
<table name="table">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER"/>
<column name="name" type="VARCHAR" size="64"/>
- <behavior name="listener"/>
</table>
<table name="other_table">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER"/>
@@ -1,7 +1,10 @@
<?php
+$className = null;
+
class ListenerBehavior extends Behavior {
protected $parameters = array(
'table' => 'listener',
+ 'phpName' => null,
);
private function createListenerTable() {
@@ -22,6 +25,12 @@ private function createListenerTable() {
$table = new ListenerTable($this->getParameter('table'));
$table->addBehavior(new ListenerCollectionBehavior());
+
+ if (!is_null($this->getParameter('phpName'))) {
+ $table->setPhpName($this->getParameter('phpName'));
+ }
+ ListenerTable::$phpName = $table->getPhpName();
+
$db->addTable($table);
$id = new Column('id');
@@ -83,11 +92,13 @@ public function modifyTable() {
}
public function objectMethods() {
+ global $className;
$db = $this->getDatabase() == null ? $this->getTable()->getDatabase() : $this->getDatabase();
$table = $db->getTable($this->getParameter('table'));
-
+
$script = $this->renderTemplate('ObjectListener', array(
'className' => $this->getTable()->getPhpName(),
+ 'listenerName' => ListenerTable::$phpName,
));
return $script;
}
@@ -128,10 +139,16 @@ public function postUpdate() {
class ListenerCollectionBehavior extends Behavior {
public function objectMethods() {
- $script = $this->renderTemplate('ListenerCollection');
+ global $className;
+
+ $script = $this->renderTemplate('ListenerCollection', array(
+ 'listenerName' => ListenerTable::$phpName
+ ));
return $script;
}
}
-class ListenerTable extends Table {}
+class ListenerTable extends Table {
+ public static $phpName;
+}
?>
@@ -1,4 +1,4 @@
-private static $listeners;
+private static $globalListeners;
const ALL = 'all';
/**
@@ -31,7 +31,7 @@ public static function getListenerInfo($listener, $target, $refId = null)
'on' => null,
'callback' => null,
'params' => array(),
- 'event' => Listener::ALL,
+ 'event' => self::ALL,
'target' => $target,
'ref_id' => $refId,
);
@@ -53,62 +53,66 @@ public static function getListenerInfo($listener, $target, $refId = null)
/**
* Returns all listeners stored in the database
*/
-public static function getListeners($target = null)
+public static function getGlobalListeners($target = null)
{
- if (is_null(self::$listeners)) {
- $listeners = ListenerQuery::create()->where('ref_id IS NULL')->find();
+ if (is_null(self::$globalListeners)) {
+ self::$globalListeners = array();
+ }
+
+ if (is_null(self::$globalListeners)) {
+ $listeners = <?php echo $listenerName; ?>Query::create()->where('ref_id IS NULL')->find();
foreach ($listeners as $l) {
self::addListenerToRuntime($l);
}
}
if (!is_null($target)) {
- if (!array_key_exists($target, self::$listeners)) {
- self::$listeners[$target] = array();
+ if (!array_key_exists($target, self::$globalListeners)) {
+ self::$globalListeners[$target] = array();
}
- return self::$listeners[$target];
+ return self::$globalListeners[$target];
}
- return self::$listeners;
+ return self::$globalListeners;
}
/**
* Adds a listener to the static listener runtime collection
*
* @private
-* @param Listener $l
+* @param <?php echo $listenerName; ?> $l
*/
-public static function addListenerToRuntime(Listener $l) {
- if (is_null(self::$listeners)) {
- self::$listeners = array();
+public static function addListenerToRuntime(<?php echo $listenerName; ?> $l) {
+ if (is_null(self::$globalListeners)) {
+ self::$globalListeners = array();
}
- if (!array_key_exists($l->getTarget(), self::$listeners)) {
- self::$listeners[$l->getTarget()] = array();
+ if (!array_key_exists($l->getTarget(), self::$globalListeners)) {
+ self::$globalListeners[$l->getTarget()] = array();
}
- if (!array_key_exists($l->getEvent(), self::$listeners[$l->getTarget()])) {
- self::$listeners[$l->getTarget()][$l->getEvent()] = array();
+ if (!array_key_exists($l->getEvent(), self::$globalListeners[$l->getTarget()])) {
+ self::$globalListeners[$l->getTarget()][$l->getEvent()] = array();
}
- if (!in_array($l, self::$listeners[$l->getTarget()][$l->getEvent()])) {
- self::$listeners[$l->getTarget()][$l->getEvent()][] = $l;
+ if (!in_array($l, self::$globalListeners[$l->getTarget()][$l->getEvent()])) {
+ self::$globalListeners[$l->getTarget()][$l->getEvent()][] = $l;
}
}
/**
* Removes a listener from the static listener collection
*
* @private
-* @param Listener $l
+* @param <?php echo $listenerName; ?> $l
*/
-public static function removeListenerFromRuntime(Listener $l) {
+public static function removeListenerFromRuntime(<?php echo $listenerName; ?> $l) {
try {
- self::getListeners();
- $key = array_search($l, self::$listeners[$l->getTarget()][$l->getEvent()]);
+ self::getGlobalListeners();
+ $key = array_search($l, self::$globalListeners[$l->getTarget()][$l->getEvent()]);
if ($key) {
- unset(self::$listeners[$l->getTarget()][$l->getEvent()][$key]);
+ unset(self::$globalListeners[$l->getTarget()][$l->getEvent()][$key]);
}
} catch (Exception $e) {}
}
@@ -135,7 +139,7 @@ public function getParams() {
* Set the value of [params] column.
*
* @param array $v new value
- * @return Listener The current object (for fluent API support)
+ * @return <?php echo $listenerName; ?> The current object (for fluent API support)
*/
public function setParams($v)
{
@@ -149,13 +153,17 @@ public function setParams($v)
* Adds a listener to the static collection, to keep the collection in sync during runtime.
*/
public function postInsert(PropelPDO $con = null) {
- self::addListenerToRuntime($this);
+ if ($this->target == '') {
+ self::addListenerToRuntime($this);
+ }
}
/**
* Removes a listener from the static collection, to keep the collection in sync during runtime.
*/
public function preDelete(PropelPDO $con = null) {
- self::removeListenerFromRuntime($this);
+ if ($this->target == '') {
+ self::removeListenerFromRuntime($this);
+ }
return true;
}
@@ -17,7 +17,7 @@
*/
private static function saveListener($listener, $refId = null)
{
- $listener = Listener::getListenerInfo($listener, get_called_class(), $refId);
+ $listener = <?php echo $listenerName; ?>::getListenerInfo($listener, get_called_class(), $refId);
// prepare for saving
$todb = array();
@@ -26,9 +26,9 @@ private static function saveListener($listener, $refId = null)
}
// save listener
- $l = ListenerQuery::create()->findOneById($listener['id']);
+ $l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if (is_null($l)) {
- $l = new Listener();
+ $l = new <?php echo $listenerName; ?>();
$l->fromArray($todb);
if (array_key_exists('Params', $todb)) {
$l->setParams($todb['Params']);
@@ -46,7 +46,7 @@ private static function saveListener($listener, $refId = null)
*/
public static function addGlobalListener($listener)
{
- Listener::addListenerToRuntime(self::saveListener($listener));
+ <?php echo $listenerName; ?>::addListenerToRuntime(self::saveListener($listener));
}
/**
@@ -88,10 +88,10 @@ public static function removeGlobalListener($listener)
{
$listener = $this->getListenerInfo($listener, get_called_class());
- $l = ListenerQuery::create()->findOneById($listener['id']);
+ $l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if ($l) {
- Listener::removeListenerFromRuntime($l);
+ <?php echo $listenerName; ?>::removeListenerFromRuntime($l);
$l->delete();
}
}
@@ -104,7 +104,7 @@ public static function removeGlobalListener($listener)
public function removeListener($listener) {
$listener = $this->getListenerInfo($listener, get_class($this));
- $l = ListenerQuery::create()->findOneById($listener['id']);
+ $l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if ($l) {
$key = array_search($l, $this->getListeners());
@@ -122,7 +122,7 @@ public function removeListener($listener) {
*/
private function notifyListener($event) {
$target = '<?php echo $className; ?>';
- $allListeners = Listener::getListeners($target);
+ $allListeners = <?php echo $listenerName; ?>::getGlobalListeners($target);
$listeners = array();
// grab static listeners that will receive notification based on the occured event
@@ -131,8 +131,8 @@ private function notifyListener($event) {
}
// grab static listeners that will receive notifications on all events
- if (isset($allListeners[Listener::ALL])) {
- $listeners = array_merge($listeners, $allListeners[Listener::ALL]);
+ if (isset($allListeners[<?php echo $listenerName; ?>::ALL])) {
+ $listeners = array_merge($listeners, $allListeners[<?php echo $listenerName; ?>::ALL]);
}
// grab dynamic listeners
@@ -170,8 +170,8 @@ private function notifyListener($event) {
*/
private function getListeners() {
if (is_null($this->listeners)) {
- $listeners = ListenerQuery::create()
- ->filterByTarget('<?php echo $className; ?>')
+ $listeners = <?php echo $listenerName; ?>Query::create()
+ ->filterByTarget('<?php echo $listenerName; ?>')
->where('ref_id IS NOT NULL')
->find();

0 comments on commit 21d128b

Please sign in to comment.