sfluceneplugin /lib/category/sfLuceneCategories.class.php

Language PHP Lines 158
MD5 Hash a388525e728a123f08e262fb13cb0a59 Estimated Cost $2,080 (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
<?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.
 */

/**
 * This manages and represents a bunch of categories.
 * @package    sfLucenePlugin
 * @subpackage Category
 * @author     Carl Vondrick <carl@carlsoft.net>
 * @version SVN: $Id: sfLuceneCategories.class.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $
 */

class sfLuceneCategories
{
  /**
   * Holds the writer
   */
  protected $writer;

  /**
   * Holds all the instances of the categories
   */
  protected $categories = array();

  /**
   * If true, the list has been modified.
   */
  protected $modified = false;

  /**
   * Singleton constructor
   * @param sfLucene $search The search instance to tie to
   * @param sfLuceneStorage $writer The writer (optional)
   */
  public function __construct($search, $writer = null)
  {
    if (!($search instanceof sfLucene))
    {
      throw new sfLuceneException('Search must be an instance of sfLucene');
    }

    if ($writer == null)
    {
      $writer = new sfLuceneStorageFilesystem($search->getParameter('index_location') . DIRECTORY_SEPARATOR . 'categories.php');
    }

    if (!($writer instanceof sfLuceneStorage))
    {
      throw new sfLuceneException('Writer must be an instance of sfLuceneStorage');
    }

    $this->writer = $writer;

    $this->load();
  }

  public function __destruct()
  {
    $this->save();
  }

  /**
   * Returns an instance of the category object.  If it does not exist, it creates
   * an empty one.
   */
  public function getCategory($category)
  {
    if (!isset($this->categories[$category]))
    {
      $this->categories[$category] = new sfLuceneCategory($this, $category);
    }

    return $this->categories[$category];
  }


  public function getAllCategories()
  {
    return $this->categories;
  }

  public function flagAsModified()
  {
    $this->modified = true;
  }

  public function isModified()
  {
    return $this->modified;
  }

  public function load()
  {
    $data = $this->writer->read();

    if ($data)
    {
      eval($data);

      if (!isset($categories) || !is_array($categories))
      {
        throw new sfLuceneException('Categories file found, but it was corrupt.');
      }

      $this->categories = array();

      foreach ($categories as $category => $count)
      {
        $this->categories[$category] = new sfLuceneCategory($this, $category, $count);
      }
    }

    return $this;
  }

  /**
   * Writes the list to disk
   */
  public function save()
  {
    if ($this->modified)
    {
      // build data
      $data = "\$categories = array();";

      foreach ($this->categories as $category)
      {
        if ($category->worthSaving())
        {
          $data .= $category->getPhp();
        }
      }

      $this->writer->write($data);

      $this->modified = false;
    }

    return $this;
  }

  /**
   * Clears the entire list
   */
  public function clear()
  {
    $this->categories = array();

    $this->modified = true;

    return $this;
  }
}
Back to Top