sfluceneplugin /lib/config/sfLuceneProjectConfigHandler.class.php

Language PHP Lines 205
MD5 Hash b2c4c142f26c3a54b135ce502e0f9603 Estimated Cost $3,763 (why?)
Repository https://bitbucket.org/anycode/sfluceneplugin.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
 * This file is part of the sfLucenePlugin package
 * (c) 2007 - 2008 Carl Vondrick <carl@carlsoft.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @package    sfLucenePlugin
 * @subpackage Config
 * @author     Carl Vondrick <carl@carlsoft.net>
 * @version SVN: $Id: sfLuceneProjectConfigHandler.class.php 7193 2008-01-27 05:21:33Z Carl.Vondrick $
 */
class sfLuceneProjectConfigHandler extends sfYamlConfigHandler
{
  /**
  * Builds cache file
  */
  public function execute($config)
  {
    return $this->buildConfig( $this->prepareConfig($config) );
  }

  /**
  * Given a well formed array of the config, it builds the cache output.
  */
  protected function buildConfig($config)
  {
    $retval = "<?php\n".
                "// auto-generated by " . __CLASS__ . "\n".
                "// date: %s";
    $retval = sprintf($retval, date('Y/m/d H:i:s'));

    $retval .= "\n\n\$config = array();\n\n";

    foreach ($config as $name => $values)
    {
      $retval .= "\n\n// processing  $name now...\n";

      $retval .= sprintf("\$config['%s'] = %s;\n", $name, var_export($values, true));
    }

    return $retval;
  }

  protected function prepareConfig($config)
  {
    $config = $this->parseYamls($config);
    $retval = array();

    foreach ($config as $name => $values)
    {
      try
      {
        $retval[$name] = $this->prepareOneConfig($values);
      }
      catch (Exception $e)
      {
        throw new sfConfigurationException('Error processing Lucene index "' . $name . '": ' . $e->getMessage());
      }
    }

    return $retval;
  }

  /**
  * Prepares the config files to be well formed.
  */
  protected function prepareOneConfig($config)
  {
    if (isset($config['models']))
    {
      foreach ($config['models'] as $model => &$model_config)
      {
        if (isset($model_config['fields']))
        {
          foreach ($model_config['fields'] as &$field)
          {
            $transform = null;
            $boost = null;

            $type = null;

            if (is_array($field))
            {
              $type = isset($field['type']) ? $field['type'] : null;
              $boost = isset($field['boost']) ? $field['boost'] : null;
              $transform = isset($field['transform']) ? $field['transform'] : null;
            }
            elseif (empty($field))
            {
              $type = null;
            }
            elseif (is_string($field))
            {
              $type = $field;
            }

            $type = $type ? $type : 'text';
            $boost = $boost ? $boost : 1.0;
            $transform = $transform || count($transform) ? $transform : null;

            $field = array(
              'type' => $type,
              'boost' => $boost,
              'transform' => $transform
            );
          }
        }
        else
        {
          $model_config['fields'] = array();
        }

        if (!isset($model_config['partial']))
        {
          $model_config['partial'] = null;
        }

        if (!isset($model_config['route']))
        {
          $model_config['route'] = null;
        }

        if (!isset($model_config['indexer']))
        {
          $model_config['indexer'] = null;
        }

        if (!isset($model_config['title']))
        {
          $model_config['title'] = null;
        }

        if (!isset($model_config['description']))
        {
          $model_config['description'] = null;
        }

        if (!isset($model_config['peer']))
        {
          $model_config['peer'] = $model . 'Peer';
        }

        if (!isset($model_config['rebuild_limit']))
        {
          $model_config['rebuild_limit'] = 250;
        }

        if (!isset($model_config['validator']))
        {
          $model_config['validator'] = null;
        }

        if (!isset($model_config['categories']))
        {
          $model_config['categories'] = array();
        }
      }
    }
    else
    {
      $config['models'] = array();
    }

    $encoding = isset($config['index']['encoding']) ? $config['index']['encoding'] : 'utf-8';
    $cultures = isset($config['index']['cultures']) ? $config['index']['cultures'] : array(sfConfig::get('sf_default_culture'));
    $stop_words = isset($config['index']['stop_words']) ? $config['index']['stop_words'] : array('a', 'an', 'at',' the', 'and', 'or', 'is', 'am', 'are', 'of');
    $short_words = isset($config['index']['short_words']) ? $config['index']['short_words'] : 2;
    $analyzer = isset($config['index']['analyzer']) ? $config['index']['analyzer'] : 'textnum';
    $case_sensitive = isset($config['index']['case_sensitive']) ? $config['index']['case_sensitive'] : false;
    $mb_string = isset($config['index']['mb_string']) ? $config['index']['mb_string'] : false;
    $param = isset($config['index']['param']) ? $config['index']['param'] : array();

    $config['index'] = array(
      'encoding' => $encoding,
      'cultures' => $cultures,
      'stop_words' => $stop_words,
      'short_words' => $short_words,
      'analyzer' => $analyzer,
      'case_sensitive' => (bool) $case_sensitive,
      'mb_string' => (bool) $mb_string,
      'param' => $param
    );

    // process factories...
    if (!isset($config['factories']))
    {
      $config['factories'] = array();
    }
    if (!isset($config['factories']['indexers']))
    {
      $config['factories']['indexers'] = array();
    }
    if (!isset($config['factories']['results']))
    {
      $config['factories']['results'] = array();
    }

    return $config;
  }
}
Back to Top