PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/config/sfLuceneProjectConfigHandler.class.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 204 lines | 154 code | 27 blank | 23 comment | 16 complexity | b2c4c142f26c3a54b135ce502e0f9603 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the sfLucenePlugin package
  4. * (c) 2007 - 2008 Carl Vondrick <carl@carlsoft.net>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @package sfLucenePlugin
  11. * @subpackage Config
  12. * @author Carl Vondrick <carl@carlsoft.net>
  13. * @version SVN: $Id: sfLuceneProjectConfigHandler.class.php 7193 2008-01-27 05:21:33Z Carl.Vondrick $
  14. */
  15. class sfLuceneProjectConfigHandler extends sfYamlConfigHandler
  16. {
  17. /**
  18. * Builds cache file
  19. */
  20. public function execute($config)
  21. {
  22. return $this->buildConfig( $this->prepareConfig($config) );
  23. }
  24. /**
  25. * Given a well formed array of the config, it builds the cache output.
  26. */
  27. protected function buildConfig($config)
  28. {
  29. $retval = "<?php\n".
  30. "// auto-generated by " . __CLASS__ . "\n".
  31. "// date: %s";
  32. $retval = sprintf($retval, date('Y/m/d H:i:s'));
  33. $retval .= "\n\n\$config = array();\n\n";
  34. foreach ($config as $name => $values)
  35. {
  36. $retval .= "\n\n// processing $name now...\n";
  37. $retval .= sprintf("\$config['%s'] = %s;\n", $name, var_export($values, true));
  38. }
  39. return $retval;
  40. }
  41. protected function prepareConfig($config)
  42. {
  43. $config = $this->parseYamls($config);
  44. $retval = array();
  45. foreach ($config as $name => $values)
  46. {
  47. try
  48. {
  49. $retval[$name] = $this->prepareOneConfig($values);
  50. }
  51. catch (Exception $e)
  52. {
  53. throw new sfConfigurationException('Error processing Lucene index "' . $name . '": ' . $e->getMessage());
  54. }
  55. }
  56. return $retval;
  57. }
  58. /**
  59. * Prepares the config files to be well formed.
  60. */
  61. protected function prepareOneConfig($config)
  62. {
  63. if (isset($config['models']))
  64. {
  65. foreach ($config['models'] as $model => &$model_config)
  66. {
  67. if (isset($model_config['fields']))
  68. {
  69. foreach ($model_config['fields'] as &$field)
  70. {
  71. $transform = null;
  72. $boost = null;
  73. $type = null;
  74. if (is_array($field))
  75. {
  76. $type = isset($field['type']) ? $field['type'] : null;
  77. $boost = isset($field['boost']) ? $field['boost'] : null;
  78. $transform = isset($field['transform']) ? $field['transform'] : null;
  79. }
  80. elseif (empty($field))
  81. {
  82. $type = null;
  83. }
  84. elseif (is_string($field))
  85. {
  86. $type = $field;
  87. }
  88. $type = $type ? $type : 'text';
  89. $boost = $boost ? $boost : 1.0;
  90. $transform = $transform || count($transform) ? $transform : null;
  91. $field = array(
  92. 'type' => $type,
  93. 'boost' => $boost,
  94. 'transform' => $transform
  95. );
  96. }
  97. }
  98. else
  99. {
  100. $model_config['fields'] = array();
  101. }
  102. if (!isset($model_config['partial']))
  103. {
  104. $model_config['partial'] = null;
  105. }
  106. if (!isset($model_config['route']))
  107. {
  108. $model_config['route'] = null;
  109. }
  110. if (!isset($model_config['indexer']))
  111. {
  112. $model_config['indexer'] = null;
  113. }
  114. if (!isset($model_config['title']))
  115. {
  116. $model_config['title'] = null;
  117. }
  118. if (!isset($model_config['description']))
  119. {
  120. $model_config['description'] = null;
  121. }
  122. if (!isset($model_config['peer']))
  123. {
  124. $model_config['peer'] = $model . 'Peer';
  125. }
  126. if (!isset($model_config['rebuild_limit']))
  127. {
  128. $model_config['rebuild_limit'] = 250;
  129. }
  130. if (!isset($model_config['validator']))
  131. {
  132. $model_config['validator'] = null;
  133. }
  134. if (!isset($model_config['categories']))
  135. {
  136. $model_config['categories'] = array();
  137. }
  138. }
  139. }
  140. else
  141. {
  142. $config['models'] = array();
  143. }
  144. $encoding = isset($config['index']['encoding']) ? $config['index']['encoding'] : 'utf-8';
  145. $cultures = isset($config['index']['cultures']) ? $config['index']['cultures'] : array(sfConfig::get('sf_default_culture'));
  146. $stop_words = isset($config['index']['stop_words']) ? $config['index']['stop_words'] : array('a', 'an', 'at',' the', 'and', 'or', 'is', 'am', 'are', 'of');
  147. $short_words = isset($config['index']['short_words']) ? $config['index']['short_words'] : 2;
  148. $analyzer = isset($config['index']['analyzer']) ? $config['index']['analyzer'] : 'textnum';
  149. $case_sensitive = isset($config['index']['case_sensitive']) ? $config['index']['case_sensitive'] : false;
  150. $mb_string = isset($config['index']['mb_string']) ? $config['index']['mb_string'] : false;
  151. $param = isset($config['index']['param']) ? $config['index']['param'] : array();
  152. $config['index'] = array(
  153. 'encoding' => $encoding,
  154. 'cultures' => $cultures,
  155. 'stop_words' => $stop_words,
  156. 'short_words' => $short_words,
  157. 'analyzer' => $analyzer,
  158. 'case_sensitive' => (bool) $case_sensitive,
  159. 'mb_string' => (bool) $mb_string,
  160. 'param' => $param
  161. );
  162. // process factories...
  163. if (!isset($config['factories']))
  164. {
  165. $config['factories'] = array();
  166. }
  167. if (!isset($config['factories']['indexers']))
  168. {
  169. $config['factories']['indexers'] = array();
  170. }
  171. if (!isset($config['factories']['results']))
  172. {
  173. $config['factories']['results'] = array();
  174. }
  175. return $config;
  176. }
  177. }