## Apache Zeta Components Manual :: File Source for static_connections.php

## Source for file static_connections.php

Documentation is available at static_connections.php

1. <?php
2. /**
3. *
4. * Licensed to the Apache Software Foundation (ASF) under one
5. * or more contributor license agreements. See the NOTICE file
6. * distributed with this work for additional information
7. * regarding copyright ownership. The ASF licenses this file
8. * to you under the Apache License, Version 2.0 (the
9. * "License"); you may not use this file except in compliance
10. * with the License. You may obtain a copy of the License at
11. *
12. * http://www.apache.org/licenses/LICENSE-2.0
13. *
14. * Unless required by applicable law or agreed to in writing,
15. * software distributed under the License is distributed on an
16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17. * KIND, either express or implied. See the License for the
18. * specific language governing permissions and limitations
19. * under the License.
20. *
21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
22. * @version //autogentag//
23. * @filesource
24. * @package SignalSlot
25. */
27. /**
28. * ezcSignalStaticConnections makes it possible to connect to signals through the signals identifier.
29. *
30. * The static connections allow you to:
31. * - connect to a signal sent by any object signal collection with the same identifier. Usually the
32. * identifier is set to the name of the class holding the collection. Using the static connections
33. * you can connect to a signal sent by any object of that class.
34. *
35. * - connect to a signal that does not yet exist. This allows you to delay initialization of the
36. * emitting object until it is needed.
37. *
38. * @property array $connections Holds the internal structure of signals. The format is
39. * array( identifier => array( signalName => array(priority=>array(slots)) ) ).
40. * It can be both read and set in order
41. * to provide easy setup of the static connections from disk.
42. *
43. * @version //autogen//
44. * @mainclass
45. * @package SignalSlot
46. */
47. class ezcSignalStaticConnections implements ezcSignalStaticConnectionsBase
48. {
49. /**
50. * Holds the properties of this class.
51. *
52. * @var array(string=>string)
53. */
54. private $properties = array();
56. /**
57. * ezcSignalStaticConnections singleton instance.
58. *
59. * @var ezcConfigurationManager
60. */
61. private static $instance = null;
63. /**
64. * Returns the instance of the ezcSignalStaticConnections..
65. *
66. * @return ezcConfigurationManager
67. */
68. public static function getInstance ()
69. {
70. if ( self::$instance === null )
71. {
72. self::$instance = new ezcSignalStaticConnections();
73. ezcBaseInit :: fetchConfig ( 'ezcInitSignalStaticConnections', self::$instance );
74. }
75. return self::$instance;
76. }
78. /**
79. * Constructs a new empty static connections class.
80. */
81. private function __construct()
82. {
83. $this->properties['connections'] = array();
84. }
86. /**
87. * Sets the property $name to $value.
88. *
89. * @throws ezcBasePropertyNotFoundException if the property does not exist.
90. * @param string $name
91. * @param mixed $value
92. * @return void
93. */
94. public function __set ( $name, $value )
95. {
96. switch ( $name )
97. {
98. case 'connections':
99. $this->properties[$name] = $value;
100. break;
101. default:
102. throw new ezcBasePropertyNotFoundException( $name );
103. break;
104. }
106. }
108. /**
109. * Returns the property $name.
110. *
111. * @throws ezcBasePropertyNotFoundException if the property does not exist.
112. * @param string $name
113. * @return mixed
114. */
115. public function __get ( $name )
116. {
117. switch ( $name )
118. {
119. case 'connections':
120. return (array) $this->properties[$name];
121. break;
123. default:
124. throw new ezcBasePropertyNotFoundException ( $name );
125. break;
126. }
127. }
129. /**
130. * Returns all the connections for signals $signal in signal collections
131. * with the identifier $identifier.
132. *
133. * @param string $identifier
134. * @param string $signal
135. * @return array(int=>array(callback))
136. */
137. public function getConnections ( $identifier, $signal )
138. {
139. if ( isset( $this->connections[$identifier] ) &&
140. isset( $this->connections[$identifier][$signal] ) )
141. {
142. return $this->connections[$identifier][$signal];
143. }
144. return array();
145. }
147. /**
148. * Connects the signal $signal emited by any ezcSignalCollection with the identifier
149. * $identifier to the slot $slot.
150. *
151. * To control the order in which slots are called you can set a priority
152. * from 1 - 65 536. The lower the number the higher the priority. The default
153. * priority is 1000.
154. * Slots with the same priority may be called with in any order.
155. *
156. * A slot will be called once for every time it is connected. It is possible
157. * to connect a slot more than once.
158. *
159. * See the PHP documentation for examples on the callback type.
160. * http://php.net/callback
161. *
162. * We reccommend avoiding excessive usage of the $priority parameter
163. * since it makes it much harder to track how your program works.
164. *
165. * @param string $identifier
166. * @param string $signal
167. * @param callback $slot
168. * @param int $priority
169. * @return void
170. */
171. public function connect ( $identifier, $signal, $slot, $priority = 1000 )
172. {
173. $this->properties['connections'][$identifier][$signal][$priority][] = $slot;
174. sort ( $this->properties['connections'][$identifier][$signal][$priority], SORT_NUMERIC );
175. }
177. /**
178. * Disconnects the $slot from the $signal with identifier $identifier..
179. *
180. * If the priority is given it will try to disconnect a slot with that priority.
181. * If no such slot is found no slot will be disconnected.
182. *
183. * If no priority is given it will disconnect the matching slot with the lowest priority.
184. *
185. * @param string $identifier
186. * @param string $signal
187. * @param callback $slot
188. * @param int $priority
189. * @return void
190. */
191. public function disconnect ( $identifier, $signal, $slot, $priority = null )
192. {
193. if ( !isset( $this->connections[$identifier] ) ||
194. !isset( $this->connections[$identifier][$signal] ) )
195. {
196. return;
197. }
199. if ( $priority === null ) // delete first found, searched from back
200. {
201. $allKeys = array_keys ( $this->connections[$identifier][$signal] );
202. rsort ( $allKeys, SORT_NUMERIC );
203. foreach ( $allKeys as $priority )
204. {
205. foreach ( $this->connections[$identifier][$signal][$priority] as $key => $callback )
206. {
207. if ( ezcSignalCallbackComparer::compareCallbacks( $slot, $callback ) )
208. {
209. unset( $this->properties['connections'][$identifier][$signal][$priority][$key] );
210. // if the priority is empty now it should be unset
211. if ( count ( $this->properties['connections'][$identifier][$signal][$priority] ) == 0 )
212. {
213. unset( $this->properties['connections'][$identifier][$signal][$priority] );
214. }
215. return;
216. }
217. }
218. }
220. }
221. else // only delete from priority connections
222. {
223. if ( isset( $this->connections[$identifier][$signal][$priority] ) )
224. {
225. foreach ( $this->connections[$identifier][$signal][$priority] as $key => $callback )
226. {
227. if ( ezcSignalCallbackComparer::compareCallbacks( $slot, $callback ) )
228. {
229. unset( $this->properties['connections'][$identifier][$signal][$priority][$key] );
230. // if the priority is empty now it should be unset
231. if ( count ( $this->properties['connections'][$identifier][$signal][$priority] ) == 0 )
232. {
233. unset( $this->properties['connections'][$identifier][$signal][$priority] );
234. }
235. return;
236. }
237. }
238. }
239. }
240. }
241. }
243. ?>

Documentation generated by phpDocumentor 1.4.3