PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/ApiSearchController.php

https://bitbucket.org/d1rk/li3_docs
PHP | 68 lines | 37 code | 12 blank | 19 comment | 6 complexity | 6633296d36182636ada5d307cc34f69e MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace li3_docs\controllers;
  9. use li3_docs\models\Symbols;
  10. /**
  11. * This is the Lithium API search controller. Once the symbol harvester has run,
  12. * this class queries that database for quick, ranked API search results.
  13. */
  14. class ApiSearchController extends \lithium\action\Controller {
  15. /**
  16. * AJAX response action for search autocomplete/results.
  17. *
  18. * @return array Returns an array detailing the symbol and its metadata.
  19. */
  20. public function query() {
  21. $this->_render['type'] = 'json';
  22. $query = $this->request->params['query'];
  23. $conditions = array();
  24. // If the leading character is upper-case, only search models.
  25. if (preg_match('/^[A-Z]/', $query)) {
  26. $conditions['type'] = 'class';
  27. }
  28. // If it contains a '$', only search properties.
  29. if (preg_match('/\$/', $query)) {
  30. $query = str_replace('$', '', $query);
  31. $conditions['type'] = 'property';
  32. }
  33. // If it contains parens, only search methods.
  34. if (preg_match('/[\(\)]/', $query)) {
  35. $query = str_replace('(', '', $query);
  36. $query = str_replace(')', '', $query);
  37. $conditions['type'] = 'method';
  38. }
  39. $conditions['name'] = array(
  40. 'like' => '%' . $query . '%'
  41. );
  42. $results = Symbols::find('all', array(
  43. 'conditions' => $conditions
  44. ));
  45. // Lack of results might be due to no data in db...
  46. if (count($results) < 1) {
  47. if (Symbols::find('count') == 0) {
  48. $results = array(
  49. 'error' => 'Please run `$ li3 harvest` to enable search.'
  50. );
  51. }
  52. }
  53. $this->set(compact('results'));
  54. }
  55. }
  56. ?>