Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
mapper.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Observer
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Observer mapping pattern implementation for Joomla
14  *
15  * @package Joomla.Platform
16  * @subpackage Observer
17  * @link http://docs.joomla.org/JObserverMapper
18  * @since 3.1.2
19  */
21 {
22  /**
23  * Array: array( JObservableInterface_classname => array( JObserverInterface_classname => array( paramname => param, .... ) ) )
24  *
25  * @var array
26  * @since 3.1.2
27  */
28  protected static $observations = array();
29 
30  /**
31  * Adds a mapping to observe $observerClass subjects with $observableClass observer/listener, attaching it on creation with $params
32  * on $observableClass instance creations
33  *
34  * @param string $observerClass The name of the observer class (implementing JObserverInterface)
35  * @param string $observableClass The name of the observable class (implementing JObservableInterface)
36  * @param array|boolean $params The params to give to the JObserverInterface::createObserver() function, or false to remove mapping
37  *
38  * @return void
39  *
40  * @since 3.1.2
41  */
42  public static function addObserverClassToClass($observerClass, $observableClass, $params = array())
43  {
44  if ($params !== false)
45  {
46  static::$observations[$observableClass][$observerClass] = $params;
47  }
48  else
49  {
50  unset(static::$observations[$observableClass][$observerClass]);
51  }
52  }
53 
54  /**
55  * Attaches all applicable observers to an $observableObject
56  *
57  * @param JObservableInterface $observableObject The observable subject object
58  *
59  * @return void
60  *
61  * @since 3.1.2
62  */
63  public static function attachAllObservers(JObservableInterface $observableObject)
64  {
65  $observableClass = get_class($observableObject);
66 
67  while ($observableClass != false)
68  {
69  // Attach applicable Observers for the class to the Observable subject:
70  if (isset(static::$observations[$observableClass]))
71  {
72  foreach (static::$observations[$observableClass] as $observerClass => $params)
73  {
74  // Attach an Observer to the Observable subject:
75  /**
76  * @var JObserverInterface $observerClass
77  */
78  $observerClass::createObserver($observableObject, $params);
79  }
80  }
81 
82  // Get parent class name (or false if none), and redo the above on it:
83  $observableClass = get_parent_class($observableClass);
84  }
85  }
86 }