Source for file AkObserver.php

Documentation is available at AkObserver.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. // +----------------------------------------------------------------------+
  5. // | Akelos Framework - http://www.akelos.org                             |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
  8. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  9. // +----------------------------------------------------------------------+
  10.  
  11. /**
  12.  * @package ActiveRecord
  13.  * @subpackage Base
  14.  * @author Bermi Ferrer <bermi a.t akelos c.om>
  15.  * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16.  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17.  */
  18.  
  19. require_once(AK_LIB_DIR.DS.'Ak.php');
  20. require_once(AK_LIB_DIR.DS.'AkInflector.php');
  21. require_once(AK_LIB_DIR.DS.'AkActiveRecord.php');
  22.  
  23. /**
  24. * Observer classes respond to life-cycle callbacks to implement trigger-like 
  25. * behavior outside the original class. This is a great way to reduce the clutter
  26. * that normally comes when the model class is burdened with functionality that
  27. * doesn't pertain to the core responsibility of the class. 
  28. * Example:
  29. *     class CommentObserver extends AkObserver
  30. *     {
  31. *         function afterSave($comment)
  32. *         {
  33. *             Ak::mail("admin@example.com", "New comment was posted",
  34. *                     $comment->toString());
  35. *         }
  36. *     }
  37. * This Observer sends an email when a Comment::save is finished.
  38. * ## Observing a class that can't be inferred
  39. * Observers will by default be mapped to the class with which they share a name.
  40. * So CommentObserver will be tied to observing Comment, ProductManagerObserver
  41. * to ProductManager, and so on. If you want to name your observer differently
  42. * than the class you're interested in observing, you can use the
  43. * AkActiveRecord->observe() class method:
  44. *     function afterUpdate(&$account)
  45. *     {
  46. *         $AuditTrail =& new AuditTrail($account, "UPDATED");
  47. *         $AuditTrail->save();
  48. *     }
  49. * If the audit observer needs to watch more than one kind of object, this can be
  50. * specified with multiple arguments:
  51. *     function afterUpdate(&$record)
  52. *     {
  53. *         $ObservedRecord =& new AuditTrail($record, "UPDATED");
  54. *         $ObservedRecord->save();
  55. *     }
  56. * The AuditObserver will now act on both updates to Account and Balance by
  57. * treating them both as records.
  58. * ## Available callback methods
  59. * The observer can implement callback methods for each of these methods:
  60. * beforeCreate, beforeValidation, beforeValidationOnCreate, beforeSave,
  61. * afterValidation, afterValidationOnCreate, afterCreate and afterSave
  62. * ## Triggering Observers
  63. * In order to activate an observer, you need to call create an Observer instance
  64. * and attach it to a model. 
  65. * In the Akelos Framework, this can be done in controllers using the short-hand
  66. * of for example: 
  67. *     $ComentObserverInstance =& new CommentObserver();
  68. *     $Model->addObserver(&$ComentObserverInstance);
  69. *
  70. */
  71. class AkObserver extends AkObject
  72. {
  73.     /**
  74.     * $_observing array of models that we're observing
  75.     */
  76.     var $_observing = array();
  77.  
  78.     function __construct()
  79.     {
  80.         $num_args func_num_args();
  81.         for ($i = 0$i $num_args$i++){
  82.             $target func_get_arg($i);
  83.             if(is_object($target)){
  84.                 $this->observe(&$target);
  85.             }else{
  86.                 $this->setObservedModels($target);
  87.             }
  88.         }
  89.     }
  90.     
  91.     /**
  92.     * Constructs the Observer
  93.     * @param $subject the name or names of the Models to observe
  94.     */
  95.     function observe (&$target)
  96.     {
  97.         static $memo;
  98.         $model_name $target->getModelName();
  99.         $class_name get_class($this);
  100.         if(empty($memo[$class_name]|| !in_array($model_name$memo[$class_name])){
  101.             $memo[$class_name][$model_name;
  102.             $this->_observing[$model_name;
  103.             $target->addObserver(&$this);
  104.         }
  105.     }
  106.     
  107.     /**
  108.     * Constructs the Observer
  109.     * @param $subject the name or names of the Models to observe
  110.     */
  111.     function setObservedModels ()
  112.     {        
  113.         $args func_get_args();
  114.         $models func_num_args(== 1 ? is_array($args[0]$args[0: array($args[0]) ) $args;
  115.  
  116.         foreach ($models as $class_name)
  117.         {   
  118.             /**
  119.             * @todo use Ak::import() instead.
  120.             */
  121.             $class_name AkInflector::camelize($class_name);
  122.             if (!class_exists($class_name)){
  123.                 require_once(AkInflector::toModelFilename($class_name));
  124.             }
  125.             $model =new $class_name();
  126.             $this->observe(&$model);
  127.         }
  128.     }
  129.     
  130.  
  131.     function update($state '')
  132.     {
  133.     }
  134.  
  135. }
  136.  
  137. ?>

Documentation generated on Tue, 17 Jun 2008 14:26:04 +0200 by phpDocumentor 1.3.2