SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Event_Observer.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana_Exception;
  19.  
  20.  
  21. /**
  22.  * Kohana event observer. Uses the SPL observer pattern.
  23.  */
  24. abstract class Event_Observer {
  25.  
  26. // Calling object
  27. protected $caller;
  28.  
  29. /**
  30.   * Initializes a new observer and attaches the subject as the caller.
  31.   *
  32.   * @param object Event_Subject
  33.   * @return void
  34.   */
  35. public function __construct(Event_Subject $caller)
  36. {
  37. // Update the caller
  38. $this->update($caller);
  39. }
  40.  
  41. /**
  42.   * Updates the observer subject with a new caller.
  43.   *
  44.   * @chainable
  45.   * @param object Event_Subject
  46.   * @return object
  47.   */
  48. public function update(Event_Subject $caller)
  49. {
  50. if ( ! ($caller instanceof Event_Subject))
  51. throw new Kohana_Exception('event.invalid_subject', get_class($caller), get_class($this));
  52.  
  53. // Update the caller
  54. $this->caller = $caller;
  55.  
  56. return $this;
  57. }
  58.  
  59. /**
  60.   * Detaches this observer from the subject.
  61.   *
  62.   * @chainable
  63.   * @return object
  64.   */
  65. public function remove()
  66. {
  67. // Detach this observer from the caller
  68. $this->caller->detach($this);
  69.  
  70. return $this;
  71. }
  72.  
  73. /**
  74.   * Notify the observer of a new message. This function must be defined in
  75.   * all observers and must take exactly one parameter of any type.
  76.   *
  77.   * @param mixed message string, object, or array
  78.   * @return void
  79.   */
  80. abstract public function notify($message);
  81.  
  82. } // End Event Observer
  83.