interfaces.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. /**
  3. * This file contains core interfaces for Yii framework.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * IApplicationComponent is the interface that all application components must implement.
  12. *
  13. * After the application completes configuration, it will invoke the {@link init()}
  14. * method of every loaded application component.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @package system.base
  18. * @since 1.0
  19. */
  20. interface IApplicationComponent
  21. {
  22. /**
  23. * Initializes the application component.
  24. * This method is invoked after the application completes configuration.
  25. */
  26. public function init();
  27. /**
  28. * @return boolean whether the {@link init()} method has been invoked.
  29. */
  30. public function getIsInitialized();
  31. }
  32. /**
  33. * ICache is the interface that must be implemented by cache components.
  34. *
  35. * This interface must be implemented by classes supporting caching feature.
  36. *
  37. * @author Qiang Xue <qiang.xue@gmail.com>
  38. * @package system.caching
  39. * @since 1.0
  40. */
  41. interface ICache
  42. {
  43. /**
  44. * Retrieves a value from cache with a specified key.
  45. * @param string $id a key identifying the cached value
  46. * @return mixed the value stored in cache, false if the value is not in the cache or expired.
  47. */
  48. public function get($id);
  49. /**
  50. * Retrieves multiple values from cache with the specified keys.
  51. * Some caches (such as memcache, apc) allow retrieving multiple cached values at one time,
  52. * which may improve the performance since it reduces the communication cost.
  53. * In case a cache doesn't support this feature natively, it will be simulated by this method.
  54. * @param array $ids list of keys identifying the cached values
  55. * @return array list of cached values corresponding to the specified keys. The array
  56. * is returned in terms of (key,value) pairs.
  57. * If a value is not cached or expired, the corresponding array value will be false.
  58. */
  59. public function mget($ids);
  60. /**
  61. * Stores a value identified by a key into cache.
  62. * If the cache already contains such a key, the existing value and
  63. * expiration time will be replaced with the new ones.
  64. *
  65. * @param string $id the key identifying the value to be cached
  66. * @param mixed $value the value to be cached
  67. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  68. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labelled invalid.
  69. * @return boolean true if the value is successfully stored into cache, false otherwise
  70. */
  71. public function set($id,$value,$expire=0,$dependency=null);
  72. /**
  73. * Stores a value identified by a key into cache if the cache does not contain this key.
  74. * Nothing will be done if the cache already contains the key.
  75. * @param string $id the key identifying the value to be cached
  76. * @param mixed $value the value to be cached
  77. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  78. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labelled invalid.
  79. * @return boolean true if the value is successfully stored into cache, false otherwise
  80. */
  81. public function add($id,$value,$expire=0,$dependency=null);
  82. /**
  83. * Deletes a value with the specified key from cache
  84. * @param string $id the key of the value to be deleted
  85. * @return boolean whether the deletion is successful
  86. */
  87. public function delete($id);
  88. /**
  89. * Deletes all values from cache.
  90. * Be careful of performing this operation if the cache is shared by multiple applications.
  91. * @return boolean whether the flush operation was successful.
  92. */
  93. public function flush();
  94. }
  95. /**
  96. * ICacheDependency is the interface that must be implemented by cache dependency classes.
  97. *
  98. * This interface must be implemented by classes meant to be used as
  99. * cache dependencies.
  100. *
  101. * Objects implementing this interface must be able to be serialized and unserialized.
  102. *
  103. * @author Qiang Xue <qiang.xue@gmail.com>
  104. * @package system.caching
  105. * @since 1.0
  106. */
  107. interface ICacheDependency
  108. {
  109. /**
  110. * Evaluates the dependency by generating and saving the data related with dependency.
  111. * This method is invoked by cache before writing data into it.
  112. */
  113. public function evaluateDependency();
  114. /**
  115. * @return boolean whether the dependency has changed.
  116. */
  117. public function getHasChanged();
  118. }
  119. /**
  120. * IStatePersister is the interface that must be implemented by state persister classes.
  121. *
  122. * This interface must be implemented by all state persister classes (such as
  123. * {@link CStatePersister}.
  124. *
  125. * @package system.base
  126. * @since 1.0
  127. */
  128. interface IStatePersister
  129. {
  130. /**
  131. * Loads state data from a persistent storage.
  132. * @return mixed the state
  133. */
  134. public function load();
  135. /**
  136. * Saves state data into a persistent storage.
  137. * @param mixed $state the state to be saved
  138. */
  139. public function save($state);
  140. }
  141. /**
  142. * IFilter is the interface that must be implemented by action filters.
  143. *
  144. * @package system.base
  145. * @since 1.0
  146. */
  147. interface IFilter
  148. {
  149. /**
  150. * Performs the filtering.
  151. * This method should be implemented to perform actual filtering.
  152. * If the filter wants to continue the action execution, it should call
  153. * <code>$filterChain->run()</code>.
  154. * @param CFilterChain $filterChain the filter chain that the filter is on.
  155. */
  156. public function filter($filterChain);
  157. }
  158. /**
  159. * IAction is the interface that must be implemented by controller actions.
  160. *
  161. * @package system.base
  162. * @since 1.0
  163. */
  164. interface IAction
  165. {
  166. /**
  167. * @return string id of the action
  168. */
  169. public function getId();
  170. /**
  171. * @return CController the controller instance
  172. */
  173. public function getController();
  174. }
  175. /**
  176. * IWebServiceProvider interface may be implemented by Web service provider classes.
  177. *
  178. * If this interface is implemented, the provider instance will be able
  179. * to intercept the remote method invocation (e.g. for logging or authentication purpose).
  180. * @author Qiang Xue <qiang.xue@gmail.com>
  181. * @package system.base
  182. * @since 1.0
  183. */
  184. interface IWebServiceProvider
  185. {
  186. /**
  187. * This method is invoked before the requested remote method is invoked.
  188. * @param CWebService $service the currently requested Web service.
  189. * @return boolean whether the remote method should be executed.
  190. */
  191. public function beforeWebMethod($service);
  192. /**
  193. * This method is invoked after the requested remote method is invoked.
  194. * @param CWebService $service the currently requested Web service.
  195. */
  196. public function afterWebMethod($service);
  197. }
  198. /**
  199. * IViewRenderer interface is implemented by a view renderer class.
  200. *
  201. * A view renderer is {@link CWebApplication::viewRenderer viewRenderer}
  202. * application component whose wants to replace the default view rendering logic
  203. * implemented in {@link CBaseController}.
  204. *
  205. * @author Qiang Xue <qiang.xue@gmail.com>
  206. * @package system.base
  207. * @since 1.0
  208. */
  209. interface IViewRenderer
  210. {
  211. /**
  212. * Renders a view file.
  213. * @param CBaseController $context the controller or widget who is rendering the view file.
  214. * @param string $file the view file path
  215. * @param mixed $data the data to be passed to the view
  216. * @param boolean $return whether the rendering result should be returned
  217. * @return mixed the rendering result, or null if the rendering result is not needed.
  218. */
  219. public function renderFile($context,$file,$data,$return);
  220. }
  221. /**
  222. * IUserIdentity interface is implemented by a user identity class.
  223. *
  224. * An identity represents a way to authenticate a user and retrieve
  225. * information needed to uniquely identity the user. It is normally
  226. * used with the {@link CWebApplication::user user application component}.
  227. *
  228. * @author Qiang Xue <qiang.xue@gmail.com>
  229. * @package system.base
  230. * @since 1.0
  231. */
  232. interface IUserIdentity
  233. {
  234. /**
  235. * Authenticates the user.
  236. * The information needed to authenticate the user
  237. * are usually provided in the constructor.
  238. * @return boolean whether authentication succeeds.
  239. */
  240. public function authenticate();
  241. /**
  242. * Returns a value indicating whether the identity is authenticated.
  243. * @return boolean whether the identity is valid.
  244. */
  245. public function getIsAuthenticated();
  246. /**
  247. * Returns a value that uniquely represents the identity.
  248. * @return mixed a value that uniquely represents the identity (e.g. primary key value).
  249. */
  250. public function getId();
  251. /**
  252. * Returns the display name for the identity (e.g. username).
  253. * @return string the display name for the identity.
  254. */
  255. public function getName();
  256. /**
  257. * Returns the additional identity information that needs to be persistent during the user session.
  258. * @return array additional identity information that needs to be persistent during the user session (excluding {@link id}).
  259. */
  260. public function getPersistentStates();
  261. }
  262. /**
  263. * IWebUser interface is implemented by a {@link CWebApplication::user user application component}.
  264. *
  265. * A user application component represents the identity information
  266. * for the current user.
  267. *
  268. * @author Qiang Xue <qiang.xue@gmail.com>
  269. * @package system.base
  270. * @since 1.0
  271. */
  272. interface IWebUser
  273. {
  274. /**
  275. * Returns a value that uniquely represents the identity.
  276. * @return mixed a value that uniquely represents the identity (e.g. primary key value).
  277. */
  278. public function getId();
  279. /**
  280. * Returns the display name for the identity (e.g. username).
  281. * @return string the display name for the identity.
  282. */
  283. public function getName();
  284. /**
  285. * Returns a value indicating whether the user is a guest (not authenticated).
  286. * @return boolean whether the user is a guest (not authenticated)
  287. */
  288. public function getIsGuest();
  289. /**
  290. * Performs access check for this user.
  291. * @param string $operation the name of the operation that need access check.
  292. * @param array $params name-value pairs that would be passed to business rules associated
  293. * with the tasks and roles assigned to the user.
  294. * @return boolean whether the operations can be performed by this user.
  295. */
  296. public function checkAccess($operation,$params=array());
  297. /**
  298. * Redirects the user browser to the login page.
  299. * Before the redirection, the current URL (if it's not an AJAX url) will be
  300. * kept in {@link returnUrl} so that the user browser may be redirected back
  301. * to the current page after successful login. Make sure you set {@link loginUrl}
  302. * so that the user browser can be redirected to the specified login URL after
  303. * calling this method.
  304. * After calling this method, the current request processing will be terminated.
  305. */
  306. public function loginRequired();
  307. }
  308. /**
  309. * IAuthManager interface is implemented by an auth manager application component.
  310. *
  311. * An auth manager is mainly responsible for providing role-based access control (RBAC) service.
  312. *
  313. * @author Qiang Xue <qiang.xue@gmail.com>
  314. * @package system.base
  315. * @since 1.0
  316. */
  317. interface IAuthManager
  318. {
  319. /**
  320. * Performs access check for the specified user.
  321. * @param string $itemName the name of the operation that we are checking access to
  322. * @param mixed $userId the user ID. This should be either an integer or a string representing
  323. * the unique identifier of a user. See {@link IWebUser::getId}.
  324. * @param array $params name-value pairs that would be passed to biz rules associated
  325. * with the tasks and roles assigned to the user.
  326. * @return boolean whether the operations can be performed by the user.
  327. */
  328. public function checkAccess($itemName,$userId,$params=array());
  329. /**
  330. * Creates an authorization item.
  331. * An authorization item represents an action permission (e.g. creating a post).
  332. * It has three types: operation, task and role.
  333. * Authorization items form a hierarchy. Higher level items inherit permissions representing
  334. * by lower level items.
  335. * @param string $name the item name. This must be a unique identifier.
  336. * @param integer $type the item type (0: operation, 1: task, 2: role).
  337. * @param string $description description of the item
  338. * @param string $bizRule business rule associated with the item. This is a piece of
  339. * PHP code that will be executed when {@link checkAccess} is called for the item.
  340. * @param mixed $data additional data associated with the item.
  341. * @return CAuthItem the authorization item
  342. * @throws CException if an item with the same name already exists
  343. */
  344. public function createAuthItem($name,$type,$description='',$bizRule=null,$data=null);
  345. /**
  346. * Removes the specified authorization item.
  347. * @param string $name the name of the item to be removed
  348. * @return boolean whether the item exists in the storage and has been removed
  349. */
  350. public function removeAuthItem($name);
  351. /**
  352. * Returns the authorization items of the specific type and user.
  353. * @param integer $type the item type (0: operation, 1: task, 2: role). Defaults to null,
  354. * meaning returning all items regardless of their type.
  355. * @param mixed $userId the user ID. Defaults to null, meaning returning all items even if
  356. * they are not assigned to a user.
  357. * @return array the authorization items of the specific type.
  358. */
  359. public function getAuthItems($type=null,$userId=null);
  360. /**
  361. * Returns the authorization item with the specified name.
  362. * @param string $name the name of the item
  363. * @return CAuthItem the authorization item. Null if the item cannot be found.
  364. */
  365. public function getAuthItem($name);
  366. /**
  367. * Saves an authorization item to persistent storage.
  368. * @param CAuthItem $item the item to be saved.
  369. * @param string $oldName the old item name. If null, it means the item name is not changed.
  370. */
  371. public function saveAuthItem($item,$oldName=null);
  372. /**
  373. * Adds an item as a child of another item.
  374. * @param string $itemName the parent item name
  375. * @param string $childName the child item name
  376. * @throws CException if either parent or child doesn't exist or if a loop has been detected.
  377. */
  378. public function addItemChild($itemName,$childName);
  379. /**
  380. * Removes a child from its parent.
  381. * Note, the child item is not deleted. Only the parent-child relationship is removed.
  382. * @param string $itemName the parent item name
  383. * @param string $childName the child item name
  384. * @return boolean whether the removal is successful
  385. */
  386. public function removeItemChild($itemName,$childName);
  387. /**
  388. * Returns a value indicating whether a child exists within a parent.
  389. * @param string $itemName the parent item name
  390. * @param string $childName the child item name
  391. * @return boolean whether the child exists
  392. */
  393. public function hasItemChild($itemName,$childName);
  394. /**
  395. * Returns the children of the specified item.
  396. * @param mixed $itemName the parent item name. This can be either a string or an array.
  397. * The latter represents a list of item names.
  398. * @return array all child items of the parent
  399. */
  400. public function getItemChildren($itemName);
  401. /**
  402. * Assigns an authorization item to a user.
  403. * @param string $itemName the item name
  404. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  405. * @param string $bizRule the business rule to be executed when {@link checkAccess} is called
  406. * for this particular authorization item.
  407. * @param mixed $data additional data associated with this assignment
  408. * @return CAuthAssignment the authorization assignment information.
  409. * @throws CException if the item does not exist or if the item has already been assigned to the user
  410. */
  411. public function assign($itemName,$userId,$bizRule=null,$data=null);
  412. /**
  413. * Revokes an authorization assignment from a user.
  414. * @param string $itemName the item name
  415. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  416. * @return boolean whether removal is successful
  417. */
  418. public function revoke($itemName,$userId);
  419. /**
  420. * Returns a value indicating whether the item has been assigned to the user.
  421. * @param string $itemName the item name
  422. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  423. * @return boolean whether the item has been assigned to the user.
  424. */
  425. public function isAssigned($itemName,$userId);
  426. /**
  427. * Returns the item assignment information.
  428. * @param string $itemName the item name
  429. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  430. * @return CAuthAssignment the item assignment information. Null is returned if
  431. * the item is not assigned to the user.
  432. */
  433. public function getAuthAssignment($itemName,$userId);
  434. /**
  435. * Returns the item assignments for the specified user.
  436. * @param mixed $userId the user ID (see {@link IWebUser::getId})
  437. * @return array the item assignment information for the user. An empty array will be
  438. * returned if there is no item assigned to the user.
  439. */
  440. public function getAuthAssignments($userId);
  441. /**
  442. * Saves the changes to an authorization assignment.
  443. * @param CAuthAssignment $assignment the assignment that has been changed.
  444. */
  445. public function saveAuthAssignment($assignment);
  446. /**
  447. * Removes all authorization data.
  448. */
  449. public function clearAll();
  450. /**
  451. * Removes all authorization assignments.
  452. */
  453. public function clearAuthAssignments();
  454. /**
  455. * Saves authorization data into persistent storage.
  456. * If any change is made to the authorization data, please make
  457. * sure you call this method to save the changed data into persistent storage.
  458. */
  459. public function save();
  460. /**
  461. * Executes a business rule.
  462. * A business rule is a piece of PHP code that will be executed when {@link checkAccess} is called.
  463. * @param string $bizRule the business rule to be executed.
  464. * @param array $params additional parameters to be passed to the business rule when being executed.
  465. * @param mixed $data additional data that is associated with the corresponding authorization item or assignment
  466. * @return boolean whether the execution returns a true value.
  467. * If the business rule is empty, it will also return true.
  468. */
  469. public function executeBizRule($bizRule,$params,$data);
  470. }
  471. /**
  472. * IBehavior interfaces is implemented by all behavior classes.
  473. *
  474. * A behavior is a way to enhance a component with additional methods that
  475. * are defined in the behavior class and not available in the component class.
  476. *
  477. * @author Qiang Xue <qiang.xue@gmail.com>
  478. * @package system.base
  479. */
  480. interface IBehavior
  481. {
  482. /**
  483. * Attaches the behavior object to the component.
  484. * @param CComponent $component the component that this behavior is to be attached to.
  485. */
  486. public function attach($component);
  487. /**
  488. * Detaches the behavior object from the component.
  489. * @param CComponent $component the component that this behavior is to be detached from.
  490. */
  491. public function detach($component);
  492. /**
  493. * @return boolean whether this behavior is enabled
  494. */
  495. public function getEnabled();
  496. /**
  497. * @param boolean $value whether this behavior is enabled
  498. */
  499. public function setEnabled($value);
  500. }
  501. /**
  502. * IWidgetFactory is the interface that must be implemented by a widget factory class.
  503. *
  504. * When calling {@link CBaseController::createWidget}, if a widget factory is available,
  505. * it will be used for creating the requested widget.
  506. *
  507. * @author Qiang Xue <qiang.xue@gmail.com>
  508. * @package system.web
  509. * @since 1.1
  510. */
  511. interface IWidgetFactory
  512. {
  513. /**
  514. * Creates a new widget based on the given class name and initial properties.
  515. * @param CBaseController $owner the owner of the new widget
  516. * @param string $className the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache)
  517. * @param array $properties the initial property values (name=>value) of the widget.
  518. * @return CWidget the newly created widget whose properties have been initialized with the given values.
  519. */
  520. public function createWidget($owner,$className,$properties=array());
  521. }
  522. /**
  523. * IDataProvider is the interface that must be implemented by data provider classes.
  524. *
  525. * Data providers are components that can feed data for widgets such as data grid, data list.
  526. * Besides providing data, they also support pagination and sorting.
  527. *
  528. * @author Qiang Xue <qiang.xue@gmail.com>
  529. * @package system.web
  530. * @since 1.1
  531. */
  532. interface IDataProvider
  533. {
  534. /**
  535. * @return string the unique ID that identifies the data provider from other data providers.
  536. */
  537. public function getId();
  538. /**
  539. * Returns the number of data items in the current page.
  540. * This is equivalent to <code>count($provider->getData())</code>.
  541. * When {@link pagination} is set false, this returns the same value as {@link totalItemCount}.
  542. * @param boolean $refresh whether the number of data items should be re-calculated.
  543. * @return integer the number of data items in the current page.
  544. */
  545. public function getItemCount($refresh=false);
  546. /**
  547. * Returns the total number of data items.
  548. * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
  549. * @param boolean $refresh whether the total number of data items should be re-calculated.
  550. * @return integer total number of possible data items.
  551. */
  552. public function getTotalItemCount($refresh=false);
  553. /**
  554. * Returns the data items currently available.
  555. * @param boolean $refresh whether the data should be re-fetched from persistent storage.
  556. * @return array the list of data items currently available in this data provider.
  557. */
  558. public function getData($refresh=false);
  559. /**
  560. * Returns the key values associated with the data items.
  561. * @param boolean $refresh whether the keys should be re-calculated.
  562. * @return array the list of key values corresponding to {@link data}. Each data item in {@link data}
  563. * is uniquely identified by the corresponding key value in this array.
  564. */
  565. public function getKeys($refresh=false);
  566. /**
  567. * @return CSort the sorting object. If this is false, it means the sorting is disabled.
  568. */
  569. public function getSort();
  570. /**
  571. * @return CPagination the pagination object. If this is false, it means the pagination is disabled.
  572. */
  573. public function getPagination();
  574. }
  575. /**
  576. * ILogFilter is the interface that must be implemented by log filters.
  577. *
  578. * A log filter preprocesses the logged messages before they are handled by a log route.
  579. * You can attach classes that implement ILogFilter to {@link CLogRoute::$filter}.
  580. *
  581. * @package system.logging
  582. * @since 1.1.11
  583. */
  584. interface ILogFilter
  585. {
  586. /**
  587. * This method should be implemented to perform actual filtering of log messages
  588. * by working on the array given as the first parameter.
  589. * Implementation might reformat, remove or add information to logged messages.
  590. * @param array $logs list of messages. Each array element represents one message
  591. * with the following structure:
  592. * array(
  593. * [0] => message (string)
  594. * [1] => level (string)
  595. * [2] => category (string)
  596. * [3] => timestamp (float, obtained by microtime(true));
  597. */
  598. public function filter(&$logs);
  599. }