Core.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. class Zend_Cache_Core
  27. {
  28. /**
  29. * Backend Object
  30. *
  31. * @var object $_backend
  32. */
  33. protected $_backend = null;
  34. /**
  35. * Available options
  36. *
  37. * ====> (boolean) write_control :
  38. * - Enable / disable write control (the cache is read just after writing to detect corrupt entries)
  39. * - Enable write control will lightly slow the cache writing but not the cache reading
  40. * Write control can detect some corrupt cache files but maybe it's not a perfect control
  41. *
  42. * ====> (boolean) caching :
  43. * - Enable / disable caching
  44. * (can be very useful for the debug of cached scripts)
  45. *
  46. * =====> (string) cache_id_prefix :
  47. * - prefix for cache ids (namespace)
  48. *
  49. * ====> (boolean) automatic_serialization :
  50. * - Enable / disable automatic serialization
  51. * - It can be used to save directly datas which aren't strings (but it's slower)
  52. *
  53. * ====> (int) automatic_cleaning_factor :
  54. * - Disable / Tune the automatic cleaning process
  55. * - The automatic cleaning process destroy too old (for the given life time)
  56. * cache files when a new cache file is written :
  57. * 0 => no automatic cache cleaning
  58. * 1 => systematic cache cleaning
  59. * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
  60. *
  61. * ====> (int) lifetime :
  62. * - Cache lifetime (in seconds)
  63. * - If null, the cache is valid forever.
  64. *
  65. * ====> (boolean) logging :
  66. * - If set to true, logging is activated (but the system is slower)
  67. *
  68. * ====> (boolean) ignore_user_abort
  69. * - If set to true, the core will set the ignore_user_abort PHP flag inside the
  70. * save() method to avoid cache corruptions in some cases (default false)
  71. *
  72. * @var array $_options available options
  73. */
  74. protected $_options = array(
  75. 'write_control' => true,
  76. 'caching' => true,
  77. 'cache_id_prefix' => null,
  78. 'automatic_serialization' => false,
  79. 'automatic_cleaning_factor' => 10,
  80. 'lifetime' => 3600,
  81. 'logging' => false,
  82. 'logger' => null,
  83. 'ignore_user_abort' => false
  84. );
  85. /**
  86. * Array of options which have to be transfered to backend
  87. *
  88. * @var array $_directivesList
  89. */
  90. protected static $_directivesList = array('lifetime', 'logging', 'logger');
  91. /**
  92. * Not used for the core, just a sort a hint to get a common setOption() method (for the core and for frontends)
  93. *
  94. * @var array $_specificOptions
  95. */
  96. protected $_specificOptions = array();
  97. /**
  98. * Last used cache id
  99. *
  100. * @var string $_lastId
  101. */
  102. private $_lastId = null;
  103. /**
  104. * True if the backend implements Zend_Cache_Backend_ExtendedInterface
  105. *
  106. * @var boolean $_extendedBackend
  107. */
  108. protected $_extendedBackend = false;
  109. /**
  110. * Array of capabilities of the backend (only if it implements Zend_Cache_Backend_ExtendedInterface)
  111. *
  112. * @var array
  113. */
  114. protected $_backendCapabilities = array();
  115. /**
  116. * Constructor
  117. *
  118. * @param array|Zend_Config $options Associative array of options or Zend_Config instance
  119. * @throws Zend_Cache_Exception
  120. * @return void
  121. */
  122. public function __construct($options = array())
  123. {
  124. if ($options instanceof Zend_Config) {
  125. $options = $options->toArray();
  126. }
  127. if (!is_array($options)) {
  128. Zend_Cache::throwException("Options passed were not an array"
  129. . " or Zend_Config instance.");
  130. }
  131. while (list($name, $value) = each($options)) {
  132. $this->setOption($name, $value);
  133. }
  134. $this->_loggerSanity();
  135. }
  136. /**
  137. * Set options using an instance of type Zend_Config
  138. *
  139. * @param Zend_Config $config
  140. * @return Zend_Cache_Core
  141. */
  142. public function setConfig(Zend_Config $config)
  143. {
  144. $options = $config->toArray();
  145. while (list($name, $value) = each($options)) {
  146. $this->setOption($name, $value);
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Set the backend
  152. *
  153. * @param object $backendObject
  154. * @throws Zend_Cache_Exception
  155. * @return void
  156. */
  157. public function setBackend(Zend_Cache_Backend $backendObject)
  158. {
  159. $this->_backend= $backendObject;
  160. // some options (listed in $_directivesList) have to be given
  161. // to the backend too (even if they are not "backend specific")
  162. $directives = array();
  163. foreach (Zend_Cache_Core::$_directivesList as $directive) {
  164. $directives[$directive] = $this->_options[$directive];
  165. }
  166. $this->_backend->setDirectives($directives);
  167. if (in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_backend))) {
  168. $this->_extendedBackend = true;
  169. $this->_backendCapabilities = $this->_backend->getCapabilities();
  170. }
  171. }
  172. /**
  173. * Returns the backend
  174. *
  175. * @return object backend object
  176. */
  177. public function getBackend()
  178. {
  179. return $this->_backend;
  180. }
  181. /**
  182. * Public frontend to set an option
  183. *
  184. * There is an additional validation (relatively to the protected _setOption method)
  185. *
  186. * @param string $name Name of the option
  187. * @param mixed $value Value of the option
  188. * @throws Zend_Cache_Exception
  189. * @return void
  190. */
  191. public function setOption($name, $value)
  192. {
  193. if (!is_string($name)) {
  194. Zend_Cache::throwException("Incorrect option name : $name");
  195. }
  196. $name = strtolower($name);
  197. if (array_key_exists($name, $this->_options)) {
  198. // This is a Core option
  199. $this->_setOption($name, $value);
  200. return;
  201. }
  202. if (array_key_exists($name, $this->_specificOptions)) {
  203. // This a specic option of this frontend
  204. $this->_specificOptions[$name] = $value;
  205. return;
  206. }
  207. }
  208. /**
  209. * Public frontend to get an option value
  210. *
  211. * @param string $name Name of the option
  212. * @throws Zend_Cache_Exception
  213. * @return mixed option value
  214. */
  215. public function getOption($name)
  216. {
  217. if (is_string($name)) {
  218. $name = strtolower($name);
  219. if (array_key_exists($name, $this->_options)) {
  220. // This is a Core option
  221. return $this->_options[$name];
  222. }
  223. if (array_key_exists($name, $this->_specificOptions)) {
  224. // This a specic option of this frontend
  225. return $this->_specificOptions[$name];
  226. }
  227. }
  228. Zend_Cache::throwException("Incorrect option name : $name");
  229. }
  230. /**
  231. * Set an option
  232. *
  233. * @param string $name Name of the option
  234. * @param mixed $value Value of the option
  235. * @throws Zend_Cache_Exception
  236. * @return void
  237. */
  238. private function _setOption($name, $value)
  239. {
  240. if (!is_string($name) || !array_key_exists($name, $this->_options)) {
  241. Zend_Cache::throwException("Incorrect option name : $name");
  242. }
  243. $this->_options[$name] = $value;
  244. }
  245. /**
  246. * Force a new lifetime
  247. *
  248. * The new value is set for the core/frontend but for the backend too (directive)
  249. *
  250. * @param int $newLifetime New lifetime (in seconds)
  251. * @return void
  252. */
  253. public function setLifetime($newLifetime)
  254. {
  255. $this->_options['lifetime'] = $newLifetime;
  256. $this->_backend->setDirectives(array(
  257. 'lifetime' => $newLifetime
  258. ));
  259. }
  260. /**
  261. * Test if a cache is available for the given id and (if yes) return it (false else)
  262. *
  263. * @param string $id Cache id
  264. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  265. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  266. * @return mixed|false Cached datas
  267. */
  268. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  269. {
  270. if (!$this->_options['caching']) {
  271. return false;
  272. }
  273. $id = $this->_id($id); // cache id may need prefix
  274. $this->_lastId = $id;
  275. self::_validateIdOrTag($id);
  276. $data = $this->_backend->load($id, $doNotTestCacheValidity);
  277. if ($data===false) {
  278. // no cache available
  279. return false;
  280. }
  281. if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
  282. // we need to unserialize before sending the result
  283. return unserialize($data);
  284. }
  285. return $data;
  286. }
  287. /**
  288. * Test if a cache is available for the given id
  289. *
  290. * @param string $id Cache id
  291. * @return boolean True is a cache is available, false else
  292. */
  293. public function test($id)
  294. {
  295. if (!$this->_options['caching']) {
  296. return false;
  297. }
  298. $id = $this->_id($id); // cache id may need prefix
  299. self::_validateIdOrTag($id);
  300. $this->_lastId = $id;
  301. return $this->_backend->test($id);
  302. }
  303. /**
  304. * Save some data in a cache
  305. *
  306. * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
  307. * @param string $id Cache id (if not set, the last cache id will be used)
  308. * @param array $tags Cache tags
  309. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  310. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  311. * @throws Zend_Cache_Exception
  312. * @return boolean True if no problem
  313. */
  314. public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
  315. {
  316. if (!$this->_options['caching']) {
  317. return true;
  318. }
  319. if ($id === null) {
  320. $id = $this->_lastId;
  321. } else {
  322. $id = $this->_id($id);
  323. }
  324. self::_validateIdOrTag($id);
  325. self::_validateTagsArray($tags);
  326. if ($this->_options['automatic_serialization']) {
  327. // we need to serialize datas before storing them
  328. $data = serialize($data);
  329. } else {
  330. if (!is_string($data)) {
  331. Zend_Cache::throwException("Datas must be string or set automatic_serialization = true");
  332. }
  333. }
  334. // automatic cleaning
  335. if ($this->_options['automatic_cleaning_factor'] > 0) {
  336. $rand = rand(1, $this->_options['automatic_cleaning_factor']);
  337. if ($rand==1) {
  338. if ($this->_extendedBackend) {
  339. // New way
  340. if ($this->_backendCapabilities['automatic_cleaning']) {
  341. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  342. } else {
  343. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  344. }
  345. } else {
  346. // Deprecated way (will be removed in next major version)
  347. if (method_exists($this->_backend, 'isAutomaticCleaningAvailable') && ($this->_backend->isAutomaticCleaningAvailable())) {
  348. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  349. } else {
  350. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  351. }
  352. }
  353. }
  354. }
  355. if ($this->_options['ignore_user_abort']) {
  356. $abort = ignore_user_abort(true);
  357. }
  358. if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) {
  359. $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority);
  360. } else {
  361. $result = $this->_backend->save($data, $id, $tags, $specificLifetime);
  362. }
  363. if ($this->_options['ignore_user_abort']) {
  364. ignore_user_abort($abort);
  365. }
  366. if (!$result) {
  367. // maybe the cache is corrupted, so we remove it !
  368. if ($this->_options['logging']) {
  369. $this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)");
  370. }
  371. $this->remove($id);
  372. return false;
  373. }
  374. if ($this->_options['write_control']) {
  375. $data2 = $this->_backend->load($id, true);
  376. if ($data!=$data2) {
  377. $this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match');
  378. $this->_backend->remove($id);
  379. return false;
  380. }
  381. }
  382. return true;
  383. }
  384. /**
  385. * Remove a cache
  386. *
  387. * @param string $id Cache id to remove
  388. * @return boolean True if ok
  389. */
  390. public function remove($id)
  391. {
  392. if (!$this->_options['caching']) {
  393. return true;
  394. }
  395. $id = $this->_id($id); // cache id may need prefix
  396. self::_validateIdOrTag($id);
  397. return $this->_backend->remove($id);
  398. }
  399. /**
  400. * Clean cache entries
  401. *
  402. * Available modes are :
  403. * 'all' (default) => remove all cache entries ($tags is not used)
  404. * 'old' => remove too old cache entries ($tags is not used)
  405. * 'matchingTag' => remove cache entries matching all given tags
  406. * ($tags can be an array of strings or a single string)
  407. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  408. * ($tags can be an array of strings or a single string)
  409. * 'matchingAnyTag' => remove cache entries matching any given tags
  410. * ($tags can be an array of strings or a single string)
  411. *
  412. * @param string $mode
  413. * @param array|string $tags
  414. * @throws Zend_Cache_Exception
  415. * @return boolean True if ok
  416. */
  417. public function clean($mode = 'all', $tags = array())
  418. {
  419. if (!$this->_options['caching']) {
  420. return true;
  421. }
  422. if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
  423. Zend_Cache::CLEANING_MODE_OLD,
  424. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  425. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  426. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
  427. Zend_Cache::throwException('Invalid cleaning mode');
  428. }
  429. self::_validateTagsArray($tags);
  430. return $this->_backend->clean($mode, $tags);
  431. }
  432. /**
  433. * Return an array of stored cache ids which match given tags
  434. *
  435. * In case of multiple tags, a logical AND is made between tags
  436. *
  437. * @param array $tags array of tags
  438. * @return array array of matching cache ids (string)
  439. */
  440. public function getIdsMatchingTags($tags = array())
  441. {
  442. if (!$this->_extendedBackend) {
  443. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  444. }
  445. if (!($this->_backendCapabilities['tags'])) {
  446. Zend_Cache::throwException('tags are not supported by the current backend');
  447. }
  448. return $this->_backend->getIdsMatchingTags($tags);
  449. }
  450. /**
  451. * Return an array of stored cache ids which don't match given tags
  452. *
  453. * In case of multiple tags, a logical OR is made between tags
  454. *
  455. * @param array $tags array of tags
  456. * @return array array of not matching cache ids (string)
  457. */
  458. public function getIdsNotMatchingTags($tags = array())
  459. {
  460. if (!$this->_extendedBackend) {
  461. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  462. }
  463. if (!($this->_backendCapabilities['tags'])) {
  464. Zend_Cache::throwException('tags are not supported by the current backend');
  465. }
  466. return $this->_backend->getIdsNotMatchingTags($tags);
  467. }
  468. /**
  469. * Return an array of stored cache ids
  470. *
  471. * @return array array of stored cache ids (string)
  472. */
  473. public function getIds()
  474. {
  475. if (!$this->_extendedBackend) {
  476. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  477. }
  478. $array = $this->_backend->getIds();
  479. if ((!isset($this->_options['cache_id_prefix'])) || ($this->_options['cache_id_prefix'] == '')) return $array;
  480. // we need to remove cache_id_prefix from ids (see #ZF-6178)
  481. $res = array();
  482. while (list(,$id) = each($array)) {
  483. if (strpos($id, $this->_options['cache_id_prefix']) === 0) {
  484. $res[] = preg_replace("~^{$this->_options['cache_id_prefix']}~", '', $id);
  485. } else {
  486. $res[] = $id;
  487. }
  488. }
  489. return $res;
  490. }
  491. /**
  492. * Return an array of stored tags
  493. *
  494. * @return array array of stored tags (string)
  495. */
  496. public function getTags()
  497. {
  498. if (!$this->_extendedBackend) {
  499. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  500. }
  501. if (!($this->_backendCapabilities['tags'])) {
  502. Zend_Cache::throwException('tags are not supported by the current backend');
  503. }
  504. return $this->_backend->getTags();
  505. }
  506. /**
  507. * Return the filling percentage of the backend storage
  508. *
  509. * @return int integer between 0 and 100
  510. */
  511. public function getFillingPercentage()
  512. {
  513. if (!$this->_extendedBackend) {
  514. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  515. }
  516. return $this->_backend->getFillingPercentage();
  517. }
  518. /**
  519. * Return an array of metadatas for the given cache id
  520. *
  521. * The array will include these keys :
  522. * - expire : the expire timestamp
  523. * - tags : a string array of tags
  524. * - mtime : timestamp of last modification time
  525. *
  526. * @param string $id cache id
  527. * @return array array of metadatas (false if the cache id is not found)
  528. */
  529. public function getMetadatas($id)
  530. {
  531. if (!$this->_extendedBackend) {
  532. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  533. }
  534. $id = $this->_id($id); // cache id may need prefix
  535. return $this->_backend->getMetadatas($id);
  536. }
  537. /**
  538. * Give (if possible) an extra lifetime to the given cache id
  539. *
  540. * @param string $id cache id
  541. * @param int $extraLifetime
  542. * @return boolean true if ok
  543. */
  544. public function touch($id, $extraLifetime)
  545. {
  546. if (!$this->_extendedBackend) {
  547. Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
  548. }
  549. $id = $this->_id($id); // cache id may need prefix
  550. return $this->_backend->touch($id, $extraLifetime);
  551. }
  552. /**
  553. * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
  554. *
  555. * Throw an exception if a problem is found
  556. *
  557. * @param string $string Cache id or tag
  558. * @throws Zend_Cache_Exception
  559. * @return void
  560. */
  561. protected static function _validateIdOrTag($string)
  562. {
  563. if (!is_string($string)) {
  564. Zend_Cache::throwException('Invalid id or tag : must be a string');
  565. }
  566. if (substr($string, 0, 9) == 'internal-') {
  567. Zend_Cache::throwException('"internal-*" ids or tags are reserved');
  568. }
  569. if (!preg_match('~^[\w]+$~D', $string)) {
  570. Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
  571. }
  572. }
  573. /**
  574. * Validate a tags array (security, reliable filenames, reserved prefixes...)
  575. *
  576. * Throw an exception if a problem is found
  577. *
  578. * @param array $tags Array of tags
  579. * @throws Zend_Cache_Exception
  580. * @return void
  581. */
  582. protected static function _validateTagsArray($tags)
  583. {
  584. if (!is_array($tags)) {
  585. Zend_Cache::throwException('Invalid tags array : must be an array');
  586. }
  587. foreach($tags as $tag) {
  588. self::_validateIdOrTag($tag);
  589. }
  590. reset($tags);
  591. }
  592. /**
  593. * Make sure if we enable logging that the Zend_Log class
  594. * is available.
  595. * Create a default log object if none is set.
  596. *
  597. * @throws Zend_Cache_Exception
  598. * @return void
  599. */
  600. protected function _loggerSanity()
  601. {
  602. if (!isset($this->_options['logging']) || !$this->_options['logging']) {
  603. return;
  604. }
  605. if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
  606. return;
  607. }
  608. // Create a default logger to the standard output stream
  609. require_once 'Zend/Log/Writer/Stream.php';
  610. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  611. $this->_options['logger'] = $logger;
  612. }
  613. /**
  614. * Log a message at the WARN (4) priority.
  615. *
  616. * @param string $message
  617. * @throws Zend_Cache_Exception
  618. * @return void
  619. */
  620. protected function _log($message, $priority = 4)
  621. {
  622. if (!$this->_options['logging']) {
  623. return;
  624. }
  625. if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
  626. Zend_Cache::throwException('Logging is enabled but logger is not set');
  627. }
  628. $logger = $this->_options['logger'];
  629. $logger->log($message, $priority);
  630. }
  631. /**
  632. * Make and return a cache id
  633. *
  634. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  635. *
  636. * @param string $id Cache id
  637. * @return string Cache id (with or without prefix)
  638. */
  639. protected function _id($id)
  640. {
  641. if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
  642. return $this->_options['cache_id_prefix'] . $id; // return with prefix
  643. }
  644. return $id; // no prefix, just return the $id passed
  645. }
  646. }