/lib/category/sfLuceneCategories.class.php
PHP | 158 lines | 89 code | 30 blank | 39 comment | 10 complexity | a388525e728a123f08e262fb13cb0a59 MD5 | raw file
Possible License(s): BSD-3-Clause
- <?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;
- }
- }