PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/category/sfLuceneCategories.class.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 158 lines | 89 code | 30 blank | 39 comment | 10 complexity | a388525e728a123f08e262fb13cb0a59 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. * This manages and represents a bunch of categories.
  11. * @package sfLucenePlugin
  12. * @subpackage Category
  13. * @author Carl Vondrick <carl@carlsoft.net>
  14. * @version SVN: $Id: sfLuceneCategories.class.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $
  15. */
  16. class sfLuceneCategories
  17. {
  18. /**
  19. * Holds the writer
  20. */
  21. protected $writer;
  22. /**
  23. * Holds all the instances of the categories
  24. */
  25. protected $categories = array();
  26. /**
  27. * If true, the list has been modified.
  28. */
  29. protected $modified = false;
  30. /**
  31. * Singleton constructor
  32. * @param sfLucene $search The search instance to tie to
  33. * @param sfLuceneStorage $writer The writer (optional)
  34. */
  35. public function __construct($search, $writer = null)
  36. {
  37. if (!($search instanceof sfLucene))
  38. {
  39. throw new sfLuceneException('Search must be an instance of sfLucene');
  40. }
  41. if ($writer == null)
  42. {
  43. $writer = new sfLuceneStorageFilesystem($search->getParameter('index_location') . DIRECTORY_SEPARATOR . 'categories.php');
  44. }
  45. if (!($writer instanceof sfLuceneStorage))
  46. {
  47. throw new sfLuceneException('Writer must be an instance of sfLuceneStorage');
  48. }
  49. $this->writer = $writer;
  50. $this->load();
  51. }
  52. public function __destruct()
  53. {
  54. $this->save();
  55. }
  56. /**
  57. * Returns an instance of the category object. If it does not exist, it creates
  58. * an empty one.
  59. */
  60. public function getCategory($category)
  61. {
  62. if (!isset($this->categories[$category]))
  63. {
  64. $this->categories[$category] = new sfLuceneCategory($this, $category);
  65. }
  66. return $this->categories[$category];
  67. }
  68. public function getAllCategories()
  69. {
  70. return $this->categories;
  71. }
  72. public function flagAsModified()
  73. {
  74. $this->modified = true;
  75. }
  76. public function isModified()
  77. {
  78. return $this->modified;
  79. }
  80. public function load()
  81. {
  82. $data = $this->writer->read();
  83. if ($data)
  84. {
  85. eval($data);
  86. if (!isset($categories) || !is_array($categories))
  87. {
  88. throw new sfLuceneException('Categories file found, but it was corrupt.');
  89. }
  90. $this->categories = array();
  91. foreach ($categories as $category => $count)
  92. {
  93. $this->categories[$category] = new sfLuceneCategory($this, $category, $count);
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Writes the list to disk
  100. */
  101. public function save()
  102. {
  103. if ($this->modified)
  104. {
  105. // build data
  106. $data = "\$categories = array();";
  107. foreach ($this->categories as $category)
  108. {
  109. if ($category->worthSaving())
  110. {
  111. $data .= $category->getPhp();
  112. }
  113. }
  114. $this->writer->write($data);
  115. $this->modified = false;
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Clears the entire list
  121. */
  122. public function clear()
  123. {
  124. $this->categories = array();
  125. $this->modified = true;
  126. return $this;
  127. }
  128. }