PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/qeephp/library/q.php

http://enaj.googlecode.com/
PHP | 979 lines | 711 code | 24 blank | 244 comment | 25 complexity | c98c55761b5722d9405024c688dd6710 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // $Id: q.php 2361 2009-04-01 15:36:05Z dualface $
  3. /**
  4. * ?? QeePHP ??????????????
  5. *
  6. * @link http://qeephp.com/
  7. * @copyright Copyright (c) 2006-2009 Qeeyuan Inc. {@link http://www.qeeyuan.com}
  8. * @license New BSD License {@link http://qeephp.com/license/}
  9. * @version $Id: q.php 2361 2009-04-01 15:36:05Z dualface $
  10. * @package core
  11. */
  12. /**
  13. * QeePHP ?????????
  14. */
  15. define('Q_DIR', dirname(__FILE__));
  16. /**
  17. * DIRECTORY_SEPARATOR ???
  18. */
  19. define('DS', DIRECTORY_SEPARATOR);
  20. /**
  21. * CURRENT_TIMESTAMP ?????????????? time() ???
  22. */
  23. define('CURRENT_TIMESTAMP', time());
  24. global $G_CLASS_FILES;
  25. if (empty($G_CLASS_FILES))
  26. {
  27. require Q_DIR . '/_config/qeephp_class_files.php';
  28. }
  29. /**
  30. * ? Q ? QeePHP ?????????????????????
  31. *
  32. * ? Q ?? QeePHP ???????????
  33. *
  34. * - ?????????
  35. * - ????????????
  36. * - ????????????????????
  37. * - ???????
  38. * - ???????
  39. *
  40. * @author YuLei Liao <liaoyulei@qeeyuan.com>
  41. * @version $Id: q.php 2361 2009-04-01 15:36:05Z dualface $
  42. * @package core
  43. */
  44. class Q
  45. {
  46. /**
  47. * ??????????
  48. */
  49. // ??????
  50. const RUN_MODE_DEVEL = 'devel';
  51. // ??????
  52. const RUN_MODE_DEPLOY = 'deploy';
  53. // ????
  54. const RUN_MODE_TEST = 'test';
  55. /**
  56. * ?????
  57. *
  58. * @var array
  59. */
  60. private static $_objects = array();
  61. /**
  62. * ?????
  63. *
  64. * @var array
  65. */
  66. private static $_class_path = array();
  67. /**
  68. * ????????
  69. *
  70. * @var array
  71. */
  72. private static $_class_path_options = array();
  73. /**
  74. * ??????
  75. *
  76. * @var array
  77. */
  78. private static $_ini = array();
  79. /**
  80. * ?? QeePHP ???
  81. *
  82. * @return string QeePHP ???
  83. */
  84. static function version()
  85. {
  86. return '2.1';
  87. }
  88. /**
  89. * ?????????
  90. *
  91. * $option ????????????
  92. * ?????????????????? $default ???????
  93. *
  94. * @code php
  95. * $option_value = Q::ini('my_option');
  96. * @endcode
  97. *
  98. * ???????????????? $option ???“/”??????
  99. *
  100. * ??????? option_group ???????????????
  101. * ???????? my_option ???????
  102. *
  103. * @code php
  104. * // +--- option_group
  105. * // +-- my_option = this is my_option
  106. * // +-- my_option2 = this is my_option2
  107. * // \-- my_option3 = this is my_option3
  108. *
  109. * // ?? option_group ?????? my_option ?
  110. * // ???? this is my_option
  111. * echo Q::ini('option_group/my_option');
  112. * @endcode
  113. *
  114. * ???????????????????“/”??????????????????
  115. *
  116. * ??????????????? $option ????? '/' ???
  117. *
  118. * @code php
  119. * // ??????????
  120. * $all = Q::ini('/');
  121. * @endcode
  122. *
  123. * @param string $option ?????????
  124. * @param mixed $default ????????????????
  125. *
  126. * @return mixed ???????
  127. */
  128. static function ini($option, $default = null)
  129. {
  130. if ($option == '/') return self::$_ini;
  131. if (strpos($option, '/') === false)
  132. {
  133. return array_key_exists($option, self::$_ini)
  134. ? self::$_ini[$option]
  135. : $default;
  136. }
  137. $parts = explode('/', $option);
  138. $pos =& self::$_ini;
  139. foreach ($parts as $part)
  140. {
  141. if (!isset($pos[$part])) return $default;
  142. $pos =& $pos[$part];
  143. }
  144. return $pos;
  145. }
  146. /**
  147. * ?????????
  148. *
  149. * ? $option ????????$option ???????????
  150. * $data ???????????????
  151. *
  152. * @code php
  153. * // ???????
  154. * Q::changeIni('option_group/my_option2', 'new value');
  155. * @endcode
  156. *
  157. * ?? $option ??????????????????
  158. * ?? $option ???????????????????????????????
  159. *
  160. * @code php
  161. * // ????????
  162. * // +--- option_1 = old value
  163. * // +--- option_group
  164. * // +-- option1 = old value
  165. * // +-- option2 = old value
  166. * // \-- option3 = old value
  167. *
  168. * // ???????
  169. * $arr = array(
  170. * 'option_1' => 'value 1',
  171. * 'option_2' => 'value 2',
  172. * 'option_group/option2' => 'new value',
  173. * );
  174. * Q::changeIni($arr);
  175. *
  176. * // ???
  177. * // +--- option_1 = value 1
  178. * // +--- option_2 = value 2
  179. * // +--- option_group
  180. * // +-- option1 = old value
  181. * // +-- option2 = new value
  182. * // \-- option3 = old value
  183. * @endcode
  184. *
  185. * ??????? Q::changeIni() ????????????????????
  186. *
  187. * ???????????????????????? Q::replaceIni() ???
  188. *
  189. * @param string|array $option ??????????????????????
  190. * @param mixed $data ????????
  191. */
  192. static function changeIni($option, $data = null)
  193. {
  194. if (is_array($option))
  195. {
  196. foreach ($option as $key => $value)
  197. {
  198. self::changeIni($key, $value);
  199. }
  200. return;
  201. }
  202. if (!is_array($data))
  203. {
  204. if (strpos($option, '/') === false)
  205. {
  206. self::$_ini[$option] = $data;
  207. return;
  208. }
  209. $parts = explode('/', $option);
  210. $max = count($parts) - 1;
  211. $pos =& self::$_ini;
  212. for ($i = 0; $i <= $max; $i ++)
  213. {
  214. $part = $parts[$i];
  215. if ($i < $max)
  216. {
  217. if (!isset($pos[$part]))
  218. {
  219. $pos[$part] = array();
  220. }
  221. $pos =& $pos[$part];
  222. }
  223. else
  224. {
  225. $pos[$part] = $data;
  226. }
  227. }
  228. }
  229. else
  230. {
  231. foreach ($data as $key => $value)
  232. {
  233. self::changeIni($option . '/' . $key, $value);
  234. }
  235. }
  236. }
  237. /**
  238. * ????????
  239. *
  240. * Q::replaceIni() ????? Q::changeIni() ???
  241. * ?? Q::replaceIni() ??????????????
  242. * ???????????????????
  243. *
  244. * @code php
  245. * // ????????
  246. * // +--- option_1 = old value
  247. * // +--- option_group
  248. * // +-- option1 = old value
  249. * // +-- option2 = old value
  250. * // \-- option3 = old value
  251. *
  252. * // ???????
  253. * $arr = array(
  254. * 'option_1' => 'value 1',
  255. * 'option_2' => 'value 2',
  256. * 'option_group/option2' => 'new value',
  257. * );
  258. * Q::replaceIni($arr);
  259. *
  260. * // ???
  261. * // +--- option_1 = value 1
  262. * // +--- option_2 = value 2
  263. * // +--- option_group
  264. * // +-- option2 = new value
  265. * @endcode
  266. *
  267. * ?????????????? Q::replaceIni() ? Q::changeIni() ??????
  268. *
  269. * ???? Q::replaceIni() ??? Q::changeIni() ????
  270. * ???????? Q::replaceIni() ??? Q::changeIni()?
  271. *
  272. * @param string|array $option ??????????????????????
  273. * @param mixed $data ????????
  274. */
  275. static function replaceIni($option, $data = null)
  276. {
  277. if (is_array($option))
  278. {
  279. self::$_ini = array_merge(self::$_ini, $option);
  280. }
  281. else
  282. {
  283. self::$_ini[$option] = $data;
  284. }
  285. }
  286. /**
  287. * ???????
  288. *
  289. * Q::cleanIni() ?????????????????
  290. *
  291. * @param mixed $option ?????????
  292. */
  293. static function cleanIni($option)
  294. {
  295. if (strpos($option, '/') === false)
  296. {
  297. unset(self::$_ini[$option]);
  298. }
  299. else
  300. {
  301. $parts = explode('/', $option);
  302. $max = count($parts) - 1;
  303. $pos =& self::$_ini;
  304. for ($i = 0; $i <= $max; $i ++)
  305. {
  306. $part = $parts[$i];
  307. if ($i < $max)
  308. {
  309. if (!isset($pos[$part]))
  310. {
  311. $pos[$part] = array();
  312. }
  313. $pos =& $pos[$part];
  314. }
  315. else
  316. {
  317. unset($pos[$part]);
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * ?????????????????????
  324. *
  325. * @code php
  326. * Q::loadClass('Table_Posts');
  327. * @endcode
  328. *
  329. * $dirs ???????? PATH_SEPARATOR ?????????
  330. * ?????????????????
  331. *
  332. * @code php
  333. * Q::loadClass('Table_Posts', array('/www/mysite/app', '/www/mysite/lib'));
  334. * @endcode
  335. *
  336. * @param string $class_name ?????
  337. * @param string|array $dirs ??????????
  338. *
  339. * @return string|boolean ??????????? false
  340. */
  341. static function loadClass($class_name, $dirs = null, $throw = true)
  342. {
  343. if (class_exists($class_name, false) || interface_exists($class_name, false))
  344. {
  345. return $class_name;
  346. }
  347. global $G_CLASS_FILES;
  348. $class_name_l = strtolower($class_name);
  349. if (isset($G_CLASS_FILES[$class_name_l]))
  350. {
  351. require Q_DIR . DS . $G_CLASS_FILES[$class_name_l];
  352. return $class_name_l;
  353. }
  354. $filename = str_replace('_', DS, $class_name);
  355. if ($filename != $class_name)
  356. {
  357. $dirname = dirname($filename);
  358. if (!empty($dirs))
  359. {
  360. if (!is_array($dirs))
  361. {
  362. $dirs = explode(PATH_SEPARATOR, $dirs);
  363. }
  364. }
  365. else
  366. {
  367. $dirs = self::$_class_path;
  368. }
  369. $filename = basename($filename) . '.php';
  370. return self::loadClassFile($filename, $dirs, $class_name, $dirname, $throw);
  371. }
  372. else
  373. {
  374. return self::loadClassFile("{$filename}.php", self::$_class_path, $class_name, '', $throw);
  375. }
  376. }
  377. /**
  378. * ?????????
  379. *
  380. * ????? Q::loadClass() ??? QeePHP ??????? Q::import() ?????????
  381. *
  382. * ????Q::import() ?????????????
  383. *
  384. * ??????? Vendor_Smarty_Adapter???????????????? vendor/smarty/adapter.php?
  385. * ???? Q::import() ?? Vendor_Smarty_Adapter ????????
  386. * ???? vendor/smarty/adapter.php ?????
  387. *
  388. * @code php
  389. * Q::import('/www/app');
  390. * Q::loadClass('Vendor_Smarty_Adapter');
  391. * // ???????? /www/app/vendor/smarty/adapter.php
  392. * @endcode
  393. *
  394. * ?? QeePHP ?????????????????????????????????
  395. * ???? import() ?????????
  396. *
  397. * @code php
  398. * Q::import('/www/app/vendor', true);
  399. * Q::loadClass('Zend_Mail');
  400. * // ???????? /www/app/vendor/Zend/Mail.php
  401. * @endcode
  402. *
  403. * @param string $dir ????????
  404. * @param boolean $case_sensitive ?????????????????????
  405. */
  406. static function import($dir, $case_sensitive = false)
  407. {
  408. $real_dir = realpath($dir);
  409. if ($real_dir)
  410. {
  411. $dir = rtrim($real_dir, '/\\');
  412. if (!isset(self::$_class_path[$dir]))
  413. {
  414. self::$_class_path[$dir] = $dir;
  415. self::$_class_path_options[$dir] = $case_sensitive;
  416. }
  417. }
  418. }
  419. /**
  420. * ????????????????????
  421. *
  422. * ???? $dirs ????????????? $filename ????????
  423. * ???????????? $class_name ???????
  424. *
  425. * ????????????????
  426. *
  427. * @code php
  428. * Q::loadClassFile('Smarty.class.php', $dirs, 'Smarty');
  429. * @endcode
  430. *
  431. * @param string $filename ???????????????
  432. * @param string|array $dirs ???????
  433. * @param string $class_name ?????
  434. * @param string $dirname ??????????????
  435. * @param string $throw ????????????
  436. */
  437. static function loadClassFile($filename, $dirs, $class_name, $dirname = '', $throw = true)
  438. {
  439. if (!is_array($dirs))
  440. {
  441. $dirs = explode(PATH_SEPARATOR, $dirs);
  442. }
  443. if ($dirname)
  444. {
  445. $filename = rtrim($dirname, '/\\') . DS . $filename;
  446. }
  447. $filename_l = strtolower($filename);
  448. foreach ($dirs as $dir)
  449. {
  450. if (isset(self::$_class_path[$dir]))
  451. {
  452. $path = $dir . DS . (self::$_class_path_options[$dir] ? $filename : $filename_l);
  453. }
  454. else
  455. {
  456. $path = rtrim($dir, '/\\') . DS . $filename;
  457. }
  458. if (is_file($path))
  459. {
  460. require $path;
  461. break;
  462. }
  463. }
  464. // ????????????????????
  465. if (!class_exists($class_name, false) && ! interface_exists($class_name, false))
  466. {
  467. if ($throw)
  468. {
  469. throw new Q_ClassNotDefinedException($class_name, $path);
  470. }
  471. return false;
  472. }
  473. return $class_name;
  474. }
  475. /**
  476. * ???????
  477. *
  478. * ???? $dirs ????????????? $filename ????????
  479. * ??????????? $throw ???????????
  480. *
  481. * ? PHP ??? require ? include ???Q::loadFile() ????????
  482. *
  483. * <ul>
  484. * <li>???????????????</li>
  485. * <li>?????????</li>
  486. * <li>?????????????</li>
  487. * </ul>
  488. *
  489. * @code php
  490. * Q::loadFile('my_file.php', $dirs);
  491. * @endcode
  492. *
  493. * @param string $filename ???????????????
  494. * @param array $dirs ???????
  495. * @param boolean $throw ?????????????
  496. *
  497. * @return mixed
  498. */
  499. static function loadFile($filename, $dirs = null, $throw = true)
  500. {
  501. if (preg_match('/[^a-z0-9\-_.]/i', $filename))
  502. {
  503. throw new Q_IllegalFilenameException($filename);
  504. }
  505. if (is_null($dirs))
  506. {
  507. $dirs = array();
  508. }
  509. elseif (is_string($dirs))
  510. {
  511. $dirs = explode(PATH_SEPARATOR, $dirs);
  512. }
  513. foreach ($dirs as $dir)
  514. {
  515. $path = rtrim($dir, '\\/') . DS . $filename;
  516. if (is_file($path)) return include $path;
  517. }
  518. if ($throw) throw new Q_FileNotFoundException($filename);
  519. return false;
  520. }
  521. /**
  522. * ???????????
  523. *
  524. * Q::singleton() ???????
  525. *
  526. * <ul>
  527. * <li>????????????????????????</li>
  528. * <li>??????????????</li>
  529. * <li>?????????????????????????</li>
  530. * <li>?????????????????????????</li>
  531. * <li>???????????</li>
  532. * </ul>
  533. *
  534. * ?? Q::singleton() ?????????????????????????
  535. *
  536. * @code php
  537. * // ??? A ????? My_Object
  538. * $obj = Q::singleton('My_Object');
  539. * ...
  540. * ...
  541. * // ??? B ????? My_Object
  542. * $obj2 = Q::singleton('My_Object');
  543. * // $obj ? $obj2 ?????????????????????????
  544. * @endcode
  545. *
  546. * @param string $class_name ??????????
  547. *
  548. * @return object ??????
  549. */
  550. static function singleton($class_name)
  551. {
  552. $key = strtolower($class_name);
  553. if (isset(self::$_objects[$key]))
  554. {
  555. return self::$_objects[$key];
  556. }
  557. self::loadClass($class_name);
  558. return self::register(new $class_name(), $class_name);
  559. }
  560. /**
  561. * ??????????????????
  562. *
  563. * ????????????????????????????????? Q::registry() ???????
  564. * ????????????????????????????????
  565. *
  566. * @code php
  567. * // ??????
  568. * Q::register(new MyObject());
  569. * .....
  570. * // ??????
  571. * $obj = Q::regitry('MyObject');
  572. * @endcode
  573. *
  574. * ? $persistent ??? true ??????????????
  575. * ?????????????? Q::registry() ????????????????????????
  576. *
  577. * ??????????????????????????????????
  578. * ?????????????????????
  579. *
  580. * ???????????????????? object_persistent_provier ???
  581. * ???????????????????
  582. *
  583. * @code php
  584. * if (!Q::isRegistered('MyObject'))
  585. * {
  586. * Q::register(new MyObject(), 'MyObject', true);
  587. * }
  588. * $app = Q::registry('MyObject');
  589. * @endcode
  590. *
  591. * @param object $obj ??????
  592. * @param string $name ???????
  593. * @param boolean $persistent ?????????????
  594. *
  595. * @return object
  596. */
  597. static function register($obj, $name = null, $persistent = false)
  598. {
  599. if (!is_object($obj))
  600. {
  601. // LC_MSG: Type mismatch. $obj expected is object, actual is "%s".
  602. throw new QException(__('Type mismatch. $obj expected is object, actual is "%s".',
  603. gettype($obj)));
  604. }
  605. // TODO: ??? $persistent ?????
  606. if (is_null($name))
  607. {
  608. $name = get_class($obj);
  609. }
  610. $name = strtolower($name);
  611. self::$_objects[$name] = $obj;
  612. return $obj;
  613. }
  614. /**
  615. * ?????????????????????????????
  616. *
  617. * @code php
  618. * // ??????
  619. * Q::register(new MyObject(), 'obj1');
  620. * .....
  621. * // ??????
  622. * $obj = Q::regitry('obj1');
  623. * @endcode
  624. *
  625. * @param string $name ????????
  626. *
  627. * @return object ??????
  628. */
  629. static function registry($name)
  630. {
  631. $name = strtolower($name);
  632. if (isset(self::$_objects[$name]))
  633. {
  634. return self::$_objects[$name];
  635. }
  636. // LC_MSG: No object is registered of name "%s".
  637. throw new QException(__('No object is registered of name "%s".', $name));
  638. }
  639. /**
  640. * ???????????????
  641. *
  642. * @param string $name ????????
  643. *
  644. * @return boolean ????????
  645. */
  646. static function isRegistered($name)
  647. {
  648. $name = strtolower($name);
  649. return isset(self::$_objects[$name]);
  650. }
  651. /**
  652. * ?????????????????????????? false
  653. *
  654. * ???????????????? ID??????????????? ID?
  655. * ???? A ??? ID ? data-a???? B ??? ID ? data-b?
  656. *
  657. * ??????????????????????? ID????????????
  658. *
  659. * <ul>
  660. * <li>?????????????????? page?db ??</li>
  661. * <li>??????????????????????????????? ID?</li>
  662. * </ul>
  663. *
  664. * ????????? ID ????? page.news.1?db.members.userid?
  665. *
  666. * Q::cache() ???? $policy ?????????????????
  667. * ????????????????????????
  668. *
  669. * $backend_class ???????????????????? QCache_File?QCache_APC ??
  670. *
  671. * @code php
  672. * $data = Q::cache($cache_id);
  673. * if ($data === false)
  674. * {
  675. * $data = ....
  676. * Q::writeCache($cache_id, $data);
  677. * }
  678. * @endcode
  679. *
  680. * @param string $id ??? ID
  681. * @param array $policy ????
  682. * @param string $backend_class ????????
  683. *
  684. * @return mixed ????????????? false
  685. */
  686. static function cache($id, array $policy = null, $backend_class = null)
  687. {
  688. static $obj = null;
  689. if (is_null($backend_class))
  690. {
  691. if (is_null($obj))
  692. {
  693. $obj = self::singleton(self::ini('runtime_cache_backend'));
  694. }
  695. return $obj->get($id, $policy);
  696. }
  697. else
  698. {
  699. $cache = self::singleton($backend_class);
  700. return $cache->get($id, $policy);
  701. }
  702. }
  703. /**
  704. * ????????????????
  705. *
  706. * $data ????????????? $data ?????????????????? serialize ??? true?
  707. * $policy ????????????????????????????????????
  708. *
  709. * ????? Q::cache()?
  710. *
  711. * @param string $id ??? ID
  712. * @param mixed $data ??????
  713. * @param array $policy ????
  714. * @param string $backend_class ????????
  715. */
  716. static function writeCache($id, $data, array $policy = null, $backend_class = null)
  717. {
  718. static $obj = null;
  719. if (is_null($backend_class))
  720. {
  721. if (is_null($obj))
  722. {
  723. $obj = self::singleton(self::ini('runtime_cache_backend'));
  724. }
  725. $obj->set($id, $data, $policy);
  726. }
  727. else
  728. {
  729. $cache = self::singleton($backend_class);
  730. $cache->set($id, $data, $policy);
  731. }
  732. }
  733. /**
  734. * ?????????
  735. *
  736. * ?????????????????????????????????????
  737. * ????????????????????????????????????????????
  738. *
  739. * @code php
  740. * Q::cleanCache($cache_id);
  741. * @endcode
  742. *
  743. * @param string $id ??? ID
  744. * @param array $policy ????
  745. * @param string $backend_class ????????
  746. */
  747. static function cleanCache($id, array $policy = null, $backend_class = null)
  748. {
  749. static $obj = null;
  750. if (is_null($backend_class))
  751. {
  752. if (is_null($obj))
  753. {
  754. $obj = self::singleton(self::ini('runtime_cache_backend'));
  755. }
  756. $obj->remove($id, $policy);
  757. }
  758. else
  759. {
  760. $cache = self::singleton($backend_class);
  761. $cache->remove($id, $policy);
  762. }
  763. }
  764. /**
  765. * ??????????????????????
  766. *
  767. * $input ?????????????“,”?????????????????
  768. * ?????????????? trim() ????????????????????????
  769. *
  770. * ?????????????“item1, item2, item3” ????????????
  771. *
  772. * @code php
  773. * $input = 'item1, item2, item3';
  774. * $output = Q::normalize($input);
  775. * // $output ?????????????
  776. * // $output = array(
  777. * // 'item1',
  778. * // 'item2',
  779. * // 'item3',
  780. * // );
  781. *
  782. * $input = 'item1|item2|item3';
  783. * // ?????????????
  784. * $output = Q::normalize($input, '|');
  785. * @endcode
  786. *
  787. * @param array|string $input ???????????
  788. * @param string $delimiter ??????????
  789. *
  790. * @return array ?????
  791. */
  792. static function normalize($input, $delimiter = ',')
  793. {
  794. if (!is_array($input))
  795. {
  796. $input = explode($delimiter, $input);
  797. }
  798. $input = array_map('trim', $input);
  799. return array_filter($input, 'strlen');
  800. }
  801. /**
  802. * ????????????
  803. *
  804. * ?? Q::control() ?????????????????????????
  805. *
  806. * @param string $type ???????????
  807. * @param string $id ??ID
  808. * @param array $attrs ???????????
  809. *
  810. * @return QUI_Control_Abstract ???????????
  811. */
  812. static function control($type, $id = null, array $attrs = null)
  813. {
  814. $id = (empty($id)) ? strtolower($type) : strtolower($id);
  815. if (!is_array($attrs)) $attrs = array();
  816. $class_name = 'Control_' . ucfirst(strtolower($type));
  817. return new $class_name($id, $attrs);
  818. }
  819. /**
  820. * ?? QeePHP ????????????????
  821. *
  822. * @param string $class_name
  823. */
  824. static function autoload($class_name)
  825. {
  826. self::loadClass($class_name, null, false);
  827. }
  828. /**
  829. * ????????????????
  830. *
  831. * ????? Zend Framework?
  832. *
  833. * @param string $class ??????????
  834. * @param boolean $enabled ????????
  835. */
  836. static function registerAutoload($class = 'Q', $enabled = true)
  837. {
  838. if (!function_exists('spl_autoload_register'))
  839. {
  840. require_once Q_DIR . '/core/exception.php';
  841. throw new QException('spl_autoload does not exist in this PHP installation');
  842. }
  843. if ($enabled === true)
  844. {
  845. spl_autoload_register(array($class, 'autoload'));
  846. }
  847. else
  848. {
  849. spl_autoload_unregister(array($class, 'autoload'));
  850. }
  851. }
  852. }
  853. /**
  854. * QeePHP ????????????
  855. *
  856. * ???????? QTranslate ??????????
  857. *
  858. * @return $msg
  859. */
  860. function __()
  861. {
  862. $args = func_get_args();
  863. $msg = array_shift($args);
  864. $language = strtolower(Q::ini('error_language'));
  865. $messages = Q::loadFile('lc_messages.php', Q_DIR . '/_lang/' . $language, false);
  866. if (isset($messages[$msg]))
  867. {
  868. $msg = $messages[$msg];
  869. }
  870. array_unshift($args, $msg);
  871. return call_user_func_array('sprintf', $args);
  872. }
  873. /**
  874. * ?? HTML ???????? htmlspecialchars()
  875. *
  876. * @param string $text
  877. *
  878. * @return string
  879. */
  880. function h($text)
  881. {
  882. return htmlspecialchars($text);
  883. }
  884. function nl2p($text)
  885. {
  886. $ps = split("(\r\n)+|\n+", trim($text));
  887. $ret = "";
  888. foreach($ps as $p){
  889. $ret .= "<p>$p</p>\n";
  890. }
  891. return $ret;
  892. }
  893. /**
  894. * QDebug::dump() ???????????????
  895. *
  896. * @param mixed $vars ??????
  897. * @param string $label ??????????
  898. * @param boolean $return ????????
  899. *
  900. * @return string
  901. */
  902. function dump($vars, $label = null, $return = false)
  903. {
  904. return QDebug::dump($vars, $label, $return);
  905. }
  906. /**
  907. * QContext::url() ???????????? URL ??
  908. *
  909. * url() ????????????? QContext::url() ????????
  910. *
  911. * @param string $udi UDI ???
  912. * @param array|string $params ??????
  913. * @param string $route_name ???
  914. * @param array $opts ?????? URL ???
  915. *
  916. * @return string ??? URL ??
  917. */
  918. function url($udi, $params = null, $route_name = null, array $opts = null)
  919. {
  920. return QContext::instance()->url($udi, $params, $route_name, $opts);
  921. }
  922. /**
  923. * ?????????
  924. */
  925. Q::registerAutoload();