PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/yii/1.1.5/framework/base/CComponent.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 688 lines | 292 code | 30 blank | 366 comment | 80 complexity | 6116f9ecda966d061d424bf786d4551f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file contains the foundation classes for component-based and event-driven programming.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CComponent is the base class for all components.
  12. *
  13. * CComponent implements the protocol of defining, using properties and events.
  14. *
  15. * A property is defined by a getter method, and/or a setter method.
  16. * Properties can be accessed in the way like accessing normal object members.
  17. * Reading or writing a property will cause the invocation of the corresponding
  18. * getter or setter method, e.g
  19. * <pre>
  20. * $a=$component->text; // equivalent to $a=$component->getText();
  21. * $component->text='abc'; // equivalent to $component->setText('abc');
  22. * </pre>
  23. * The signatures of getter and setter methods are as follows,
  24. * <pre>
  25. * // getter, defines a readable property 'text'
  26. * public function getText() { ... }
  27. * // setter, defines a writable property 'text' with $value to be set to the property
  28. * public function setText($value) { ... }
  29. * </pre>
  30. *
  31. * An event is defined by the presence of a method whose name starts with 'on'.
  32. * The event name is the method name. When an event is raised, functions
  33. * (called event handlers) attached to the event will be invoked automatically.
  34. *
  35. * An event can be raised by calling {@link raiseEvent} method, upon which
  36. * the attached event handlers will be invoked automatically in the order they
  37. * are attached to the event. Event handlers must have the following signature,
  38. * <pre>
  39. * function eventHandler($event) { ... }
  40. * </pre>
  41. * where $event includes parameters associated with the event.
  42. *
  43. * To attach an event handler to an event, see {@link attachEventHandler}.
  44. * You can also use the following syntax:
  45. * <pre>
  46. * $component->onClick=$callback; // or $component->onClick->add($callback);
  47. * </pre>
  48. * where $callback refers to a valid PHP callback. Below we show some callback examples:
  49. * <pre>
  50. * 'handleOnClick' // handleOnClick() is a global function
  51. * array($object,'handleOnClick') // using $object->handleOnClick()
  52. * array('Page','handleOnClick') // using Page::handleOnClick()
  53. * </pre>
  54. *
  55. * To raise an event, use {@link raiseEvent}. The on-method defining an event is
  56. * commonly written like the following:
  57. * <pre>
  58. * public function onClick($event)
  59. * {
  60. * $this->raiseEvent('onClick',$event);
  61. * }
  62. * </pre>
  63. * where <code>$event</code> is an instance of {@link CEvent} or its child class.
  64. * One can then raise the event by calling the on-method instead of {@link raiseEvent} directly.
  65. *
  66. * Both property names and event names are case-insensitive.
  67. *
  68. * Starting from version 1.0.2, CComponent supports behaviors. A behavior is an
  69. * instance of {@link IBehavior} which is attached to a component. The methods of
  70. * the behavior can be invoked as if they belong to the component. Multiple behaviors
  71. * can be attached to the same component.
  72. *
  73. * To attach a behavior to a component, call {@link attachBehavior}; and to detach the behavior
  74. * from the component, call {@link detachBehavior}.
  75. *
  76. * A behavior can be temporarily enabled or disabled by calling {@link enableBehavior}
  77. * or {@link disableBehavior}, respectively. When disabled, the behavior methods cannot
  78. * be invoked via the component.
  79. *
  80. * Starting from version 1.1.0, a behavior's properties (either its public member variables or
  81. * its properties defined via getters and/or setters) can be accessed through the component it
  82. * is attached to.
  83. *
  84. * @author Qiang Xue <qiang.xue@gmail.com>
  85. * @version $Id: CComponent.php 2639 2010-11-11 16:12:42Z qiang.xue $
  86. * @package system.base
  87. * @since 1.0
  88. */
  89. class CComponent
  90. {
  91. private $_e;
  92. private $_m;
  93. /**
  94. * Returns a property value, an event handler list or a behavior based on its name.
  95. * Do not call this method. This is a PHP magic method that we override
  96. * to allow using the following syntax to read a property or obtain event handlers:
  97. * <pre>
  98. * $value=$component->propertyName;
  99. * $handlers=$component->eventName;
  100. * </pre>
  101. * @param string $name the property name or event name
  102. * @return mixed the property value, event handlers attached to the event, or the named behavior (since version 1.0.2)
  103. * @throws CException if the property or event is not defined
  104. * @see __set
  105. */
  106. public function __get($name)
  107. {
  108. $getter='get'.$name;
  109. if(method_exists($this,$getter))
  110. return $this->$getter();
  111. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  112. {
  113. // duplicating getEventHandlers() here for performance
  114. $name=strtolower($name);
  115. if(!isset($this->_e[$name]))
  116. $this->_e[$name]=new CList;
  117. return $this->_e[$name];
  118. }
  119. else if(isset($this->_m[$name]))
  120. return $this->_m[$name];
  121. else if(is_array($this->_m))
  122. {
  123. foreach($this->_m as $object)
  124. {
  125. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  126. return $object->$name;
  127. }
  128. }
  129. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  130. array('{class}'=>get_class($this), '{property}'=>$name)));
  131. }
  132. /**
  133. * Sets value of a component property.
  134. * Do not call this method. This is a PHP magic method that we override
  135. * to allow using the following syntax to set a property or attach an event handler
  136. * <pre>
  137. * $this->propertyName=$value;
  138. * $this->eventName=$callback;
  139. * </pre>
  140. * @param string $name the property name or the event name
  141. * @param mixed $value the property value or callback
  142. * @throws CException if the property/event is not defined or the property is read only.
  143. * @see __get
  144. */
  145. public function __set($name,$value)
  146. {
  147. $setter='set'.$name;
  148. if(method_exists($this,$setter))
  149. return $this->$setter($value);
  150. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  151. {
  152. // duplicating getEventHandlers() here for performance
  153. $name=strtolower($name);
  154. if(!isset($this->_e[$name]))
  155. $this->_e[$name]=new CList;
  156. return $this->_e[$name]->add($value);
  157. }
  158. else if(is_array($this->_m))
  159. {
  160. foreach($this->_m as $object)
  161. {
  162. if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
  163. return $object->$name=$value;
  164. }
  165. }
  166. if(method_exists($this,'get'.$name))
  167. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  168. array('{class}'=>get_class($this), '{property}'=>$name)));
  169. else
  170. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  171. array('{class}'=>get_class($this), '{property}'=>$name)));
  172. }
  173. /**
  174. * Checks if a property value is null.
  175. * Do not call this method. This is a PHP magic method that we override
  176. * to allow using isset() to detect if a component property is set or not.
  177. * @param string $name the property name or the event name
  178. * @since 1.0.1
  179. */
  180. public function __isset($name)
  181. {
  182. $getter='get'.$name;
  183. if(method_exists($this,$getter))
  184. return $this->$getter()!==null;
  185. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  186. {
  187. $name=strtolower($name);
  188. return isset($this->_e[$name]) && $this->_e[$name]->getCount();
  189. }
  190. else if(is_array($this->_m))
  191. {
  192. if(isset($this->_m[$name]))
  193. return true;
  194. foreach($this->_m as $object)
  195. {
  196. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. /**
  203. * Sets a component property to be null.
  204. * Do not call this method. This is a PHP magic method that we override
  205. * to allow using unset() to set a component property to be null.
  206. * @param string $name the property name or the event name
  207. * @throws CException if the property is read only.
  208. * @since 1.0.1
  209. */
  210. public function __unset($name)
  211. {
  212. $setter='set'.$name;
  213. if(method_exists($this,$setter))
  214. $this->$setter(null);
  215. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  216. unset($this->_e[strtolower($name)]);
  217. else if(is_array($this->_m))
  218. {
  219. if(isset($this->_m[$name]))
  220. $this->detachBehavior($name);
  221. else
  222. {
  223. foreach($this->_m as $object)
  224. {
  225. if($object->getEnabled())
  226. {
  227. if(property_exists($object,$name))
  228. return $object->$name=null;
  229. else if($object->canSetProperty($name))
  230. return $object->$setter(null);
  231. }
  232. }
  233. }
  234. }
  235. else if(method_exists($this,'get'.$name))
  236. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  237. array('{class}'=>get_class($this), '{property}'=>$name)));
  238. }
  239. /**
  240. * Calls the named method which is not a class method.
  241. * Do not call this method. This is a PHP magic method that we override
  242. * to implement the behavior feature.
  243. * @param string $name the method name
  244. * @param array $parameters method parameters
  245. * @return mixed the method return value
  246. * @since 1.0.2
  247. */
  248. public function __call($name,$parameters)
  249. {
  250. if($this->_m!==null)
  251. {
  252. foreach($this->_m as $object)
  253. {
  254. if($object->getEnabled() && method_exists($object,$name))
  255. return call_user_func_array(array($object,$name),$parameters);
  256. }
  257. }
  258. if(class_exists('Closure', false) && $this->$name instanceof Closure)
  259. return call_user_func_array($this->$name, $parameters);
  260. throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',
  261. array('{class}'=>get_class($this), '{name}'=>$name)));
  262. }
  263. /**
  264. * Returns the named behavior object.
  265. * The name 'asa' stands for 'as a'.
  266. * @param string $behavior the behavior name
  267. * @return IBehavior the behavior object, or null if the behavior does not exist
  268. * @since 1.0.2
  269. */
  270. public function asa($behavior)
  271. {
  272. return isset($this->_m[$behavior]) ? $this->_m[$behavior] : null;
  273. }
  274. /**
  275. * Attaches a list of behaviors to the component.
  276. * Each behavior is indexed by its name and should be an instance of
  277. * {@link IBehavior}, a string specifying the behavior class, or an
  278. * array of the following structure:
  279. * <pre>
  280. * array(
  281. * 'class'=>'path.to.BehaviorClass',
  282. * 'property1'=>'value1',
  283. * 'property2'=>'value2',
  284. * )
  285. * </pre>
  286. * @param array $behaviors list of behaviors to be attached to the component
  287. * @since 1.0.2
  288. */
  289. public function attachBehaviors($behaviors)
  290. {
  291. foreach($behaviors as $name=>$behavior)
  292. $this->attachBehavior($name,$behavior);
  293. }
  294. /**
  295. * Detaches all behaviors from the component.
  296. * @since 1.0.2
  297. */
  298. public function detachBehaviors()
  299. {
  300. if($this->_m!==null)
  301. {
  302. foreach($this->_m as $name=>$behavior)
  303. $this->detachBehavior($name);
  304. $this->_m=null;
  305. }
  306. }
  307. /**
  308. * Attaches a behavior to this component.
  309. * This method will create the behavior object based on the given
  310. * configuration. After that, the behavior object will be initialized
  311. * by calling its {@link IBehavior::attach} method.
  312. * @param string $name the behavior's name. It should uniquely identify this behavior.
  313. * @param mixed $behavior the behavior configuration. This is passed as the first
  314. * parameter to {@link YiiBase::createComponent} to create the behavior object.
  315. * @return IBehavior the behavior object
  316. * @since 1.0.2
  317. */
  318. public function attachBehavior($name,$behavior)
  319. {
  320. if(!($behavior instanceof IBehavior))
  321. $behavior=Yii::createComponent($behavior);
  322. $behavior->setEnabled(true);
  323. $behavior->attach($this);
  324. return $this->_m[$name]=$behavior;
  325. }
  326. /**
  327. * Detaches a behavior from the component.
  328. * The behavior's {@link IBehavior::detach} method will be invoked.
  329. * @param string $name the behavior's name. It uniquely identifies the behavior.
  330. * @return IBehavior the detached behavior. Null if the behavior does not exist.
  331. * @since 1.0.2
  332. */
  333. public function detachBehavior($name)
  334. {
  335. if(isset($this->_m[$name]))
  336. {
  337. $this->_m[$name]->detach($this);
  338. $behavior=$this->_m[$name];
  339. unset($this->_m[$name]);
  340. return $behavior;
  341. }
  342. }
  343. /**
  344. * Enables all behaviors attached to this component.
  345. * @since 1.0.2
  346. */
  347. public function enableBehaviors()
  348. {
  349. if($this->_m!==null)
  350. {
  351. foreach($this->_m as $behavior)
  352. $behavior->setEnabled(true);
  353. }
  354. }
  355. /**
  356. * Disables all behaviors attached to this component.
  357. * @since 1.0.2
  358. */
  359. public function disableBehaviors()
  360. {
  361. if($this->_m!==null)
  362. {
  363. foreach($this->_m as $behavior)
  364. $behavior->setEnabled(false);
  365. }
  366. }
  367. /**
  368. * Enables an attached behavior.
  369. * A behavior is only effective when it is enabled.
  370. * A behavior is enabled when first attached.
  371. * @param string $name the behavior's name. It uniquely identifies the behavior.
  372. * @since 1.0.2
  373. */
  374. public function enableBehavior($name)
  375. {
  376. if(isset($this->_m[$name]))
  377. $this->_m[$name]->setEnabled(true);
  378. }
  379. /**
  380. * Disables an attached behavior.
  381. * A behavior is only effective when it is enabled.
  382. * @param string $name the behavior's name. It uniquely identifies the behavior.
  383. * @since 1.0.2
  384. */
  385. public function disableBehavior($name)
  386. {
  387. if(isset($this->_m[$name]))
  388. $this->_m[$name]->setEnabled(false);
  389. }
  390. /**
  391. * Determines whether a property is defined.
  392. * A property is defined if there is a getter or setter method
  393. * defined in the class. Note, property names are case-insensitive.
  394. * @param string $name the property name
  395. * @return boolean whether the property is defined
  396. * @see canGetProperty
  397. * @see canSetProperty
  398. */
  399. public function hasProperty($name)
  400. {
  401. return method_exists($this,'get'.$name) || method_exists($this,'set'.$name);
  402. }
  403. /**
  404. * Determines whether a property can be read.
  405. * A property can be read if the class has a getter method
  406. * for the property name. Note, property name is case-insensitive.
  407. * @param string $name the property name
  408. * @return boolean whether the property can be read
  409. * @see canSetProperty
  410. */
  411. public function canGetProperty($name)
  412. {
  413. return method_exists($this,'get'.$name);
  414. }
  415. /**
  416. * Determines whether a property can be set.
  417. * A property can be written if the class has a setter method
  418. * for the property name. Note, property name is case-insensitive.
  419. * @param string $name the property name
  420. * @return boolean whether the property can be written
  421. * @see canGetProperty
  422. */
  423. public function canSetProperty($name)
  424. {
  425. return method_exists($this,'set'.$name);
  426. }
  427. /**
  428. * Determines whether an event is defined.
  429. * An event is defined if the class has a method named like 'onXXX'.
  430. * Note, event name is case-insensitive.
  431. * @param string $name the event name
  432. * @return boolean whether an event is defined
  433. */
  434. public function hasEvent($name)
  435. {
  436. return !strncasecmp($name,'on',2) && method_exists($this,$name);
  437. }
  438. /**
  439. * Checks whether the named event has attached handlers.
  440. * @param string $name the event name
  441. * @return boolean whether an event has been attached one or several handlers
  442. */
  443. public function hasEventHandler($name)
  444. {
  445. $name=strtolower($name);
  446. return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0;
  447. }
  448. /**
  449. * Returns the list of attached event handlers for an event.
  450. * @param string $name the event name
  451. * @return CList list of attached event handlers for the event
  452. * @throws CException if the event is not defined
  453. */
  454. public function getEventHandlers($name)
  455. {
  456. if($this->hasEvent($name))
  457. {
  458. $name=strtolower($name);
  459. if(!isset($this->_e[$name]))
  460. $this->_e[$name]=new CList;
  461. return $this->_e[$name];
  462. }
  463. else
  464. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  465. array('{class}'=>get_class($this), '{event}'=>$name)));
  466. }
  467. /**
  468. * Attaches an event handler to an event.
  469. *
  470. * An event handler must be a valid PHP callback, i.e., a string referring to
  471. * a global function name, or an array containing two elements with
  472. * the first element being an object and the second element a method name
  473. * of the object.
  474. *
  475. * An event handler must be defined with the following signature,
  476. * <pre>
  477. * function handlerName($event) {}
  478. * </pre>
  479. * where $event includes parameters associated with the event.
  480. *
  481. * This is a convenient method of attaching a handler to an event.
  482. * It is equivalent to the following code:
  483. * <pre>
  484. * $component->getEventHandlers($eventName)->add($eventHandler);
  485. * </pre>
  486. *
  487. * Using {@link getEventHandlers}, one can also specify the excution order
  488. * of multiple handlers attaching to the same event. For example:
  489. * <pre>
  490. * $component->getEventHandlers($eventName)->insertAt(0,$eventHandler);
  491. * </pre>
  492. * makes the handler to be invoked first.
  493. *
  494. * @param string $name the event name
  495. * @param callback $handler the event handler
  496. * @throws CException if the event is not defined
  497. * @see detachEventHandler
  498. */
  499. public function attachEventHandler($name,$handler)
  500. {
  501. $this->getEventHandlers($name)->add($handler);
  502. }
  503. /**
  504. * Detaches an existing event handler.
  505. * This method is the opposite of {@link attachEventHandler}.
  506. * @param string $name event name
  507. * @param callback $handler the event handler to be removed
  508. * @return boolean if the detachment process is successful
  509. * @see attachEventHandler
  510. */
  511. public function detachEventHandler($name,$handler)
  512. {
  513. if($this->hasEventHandler($name))
  514. return $this->getEventHandlers($name)->remove($handler)!==false;
  515. else
  516. return false;
  517. }
  518. /**
  519. * Raises an event.
  520. * This method represents the happening of an event. It invokes
  521. * all attached handlers for the event.
  522. * @param string $name the event name
  523. * @param CEvent $event the event parameter
  524. * @throws CException if the event is undefined or an event handler is invalid.
  525. */
  526. public function raiseEvent($name,$event)
  527. {
  528. $name=strtolower($name);
  529. if(isset($this->_e[$name]))
  530. {
  531. foreach($this->_e[$name] as $handler)
  532. {
  533. if(is_string($handler))
  534. call_user_func($handler,$event);
  535. else if(is_callable($handler,true))
  536. {
  537. if(is_array($handler))
  538. {
  539. // an array: 0 - object, 1 - method name
  540. list($object,$method)=$handler;
  541. if(is_string($object)) // static method call
  542. call_user_func($handler,$event);
  543. else if(method_exists($object,$method))
  544. $object->$method($event);
  545. else
  546. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  547. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));
  548. }
  549. else // PHP 5.3: anonymous function
  550. call_user_func($handler,$event);
  551. }
  552. else
  553. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  554. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));
  555. // stop further handling if param.handled is set true
  556. if(($event instanceof CEvent) && $event->handled)
  557. return;
  558. }
  559. }
  560. else if(YII_DEBUG && !$this->hasEvent($name))
  561. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  562. array('{class}'=>get_class($this), '{event}'=>$name)));
  563. }
  564. /**
  565. * Evaluates a PHP expression or callback under the context of this component.
  566. *
  567. * Valid PHP callback can be class method name in the form of
  568. * array(ClassName/Object, MethodName), or anonymous function (only available in PHP 5.3.0 or above).
  569. *
  570. * If a PHP callback is used, the corresponding function/method signature should be
  571. * <pre>
  572. * function foo($param1, $param2, ..., $component) { ... }
  573. * </pre>
  574. * where the array elements in the second parameter to this method will be passed
  575. * to the callback as $param1, $param2, ...; and the last parameter will be the component itself.
  576. *
  577. * If a PHP expression is used, the second parameter will be "extracted" into PHP variables
  578. * that can be directly accessed in the expression. See {@link http://us.php.net/manual/en/function.extract.php PHP extract}
  579. * for more details. In the expression, the component object can be accessed using $this.
  580. *
  581. * @param mixed $_expression_ a PHP expression or PHP callback to be evaluated.
  582. * @param array $_data_ additional parameters to be passed to the above expression/callback.
  583. * @return mixed the expression result
  584. * @since 1.1.0
  585. */
  586. public function evaluateExpression($_expression_,$_data_=array())
  587. {
  588. if(is_string($_expression_))
  589. {
  590. extract($_data_);
  591. return eval('return '.$_expression_.';');
  592. }
  593. else
  594. {
  595. $_data_[]=$this;
  596. return call_user_func_array($_expression_, $_data_);
  597. }
  598. }
  599. }
  600. /**
  601. * CEvent is the base class for all event classes.
  602. *
  603. * It encapsulates the parameters associated with an event.
  604. * The {@link sender} property describes who raises the event.
  605. * And the {@link handled} property indicates if the event is handled.
  606. * If an event handler sets {@link handled} to true, those handlers
  607. * that are not invoked yet will not be invoked anymore.
  608. *
  609. * @author Qiang Xue <qiang.xue@gmail.com>
  610. * @version $Id: CComponent.php 2639 2010-11-11 16:12:42Z qiang.xue $
  611. * @package system.base
  612. * @since 1.0
  613. */
  614. class CEvent extends CComponent
  615. {
  616. /**
  617. * @var object the sender of this event
  618. */
  619. public $sender;
  620. /**
  621. * @var boolean whether the event is handled. Defaults to false.
  622. * When a handler sets this true, the rest of the uninvoked event handlers will not be invoked anymore.
  623. */
  624. public $handled=false;
  625. /**
  626. * Constructor.
  627. * @param mixed $sender sender of the event
  628. */
  629. public function __construct($sender=null)
  630. {
  631. $this->sender=$sender;
  632. }
  633. }
  634. /**
  635. * CEnumerable is the base class for all enumerable types.
  636. *
  637. * To define an enumerable type, extend CEnumberable and define string constants.
  638. * Each constant represents an enumerable value.
  639. * The constant name must be the same as the constant value.
  640. * For example,
  641. * <pre>
  642. * class TextAlign extends CEnumerable
  643. * {
  644. * const Left='Left';
  645. * const Right='Right';
  646. * }
  647. * </pre>
  648. * Then, one can use the enumerable values such as TextAlign::Left and
  649. * TextAlign::Right.
  650. *
  651. * @author Qiang Xue <qiang.xue@gmail.com>
  652. * @version $Id: CComponent.php 2639 2010-11-11 16:12:42Z qiang.xue $
  653. * @package system.base
  654. * @since 1.0
  655. */
  656. class CEnumerable
  657. {
  658. }