smarty_internal_cache.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: smarty_internal_cache.php
  5. * SVN: $Id: $
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. * smarty-discussion-subscribe@googlegroups.com
  24. *
  25. * @link http://www.smarty.net/
  26. * @copyright 2008 New Digital Group, Inc.
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Uwe Tews
  29. * @package Smarty
  30. * @subpackage PluginsInternal
  31. * @version 3-SVN$Rev: 3286 $
  32. */
  33. class Smarty_Internal_Cache {
  34. protected $smarty;
  35. function __construct($smarty) {
  36. $this->smarty = $smarty;
  37. }
  38. /**
  39. * Loads cache resource.
  40. *
  41. * @return object of cache resource
  42. */
  43. public function loadResource($type = null) {
  44. if (!isset($type)) {
  45. $type = $this->smarty->caching_type;
  46. }
  47. // already loaded?
  48. if (isset($this->smarty->cache_resource_objects[$type])) {
  49. return $this->smarty->cache_resource_objects[$type];
  50. }
  51. if (in_array($type, $this->smarty->cache_resource_types)) {
  52. $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
  53. return $this->smarty->cache_resource_objects[$type] = new $cache_resource_class($this->smarty);
  54. }
  55. else {
  56. // try plugins dir
  57. $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
  58. if ($this->smarty->loadPlugin($cache_resource_class)) {
  59. return $this->smarty->cache_resource_objects[$type] = new $cache_resource_class($this->smarty);
  60. }
  61. else {
  62. throw new Exception("Unable to load cache resource '{$type}'");
  63. }
  64. }
  65. }
  66. /**
  67. * Empty cache folder
  68. *
  69. * @param integer $exp_time expiration time
  70. * @param string $type resource type
  71. * @return integer number of cache files deleted
  72. */
  73. function clearAll($exp_time = null, $type = null)
  74. {
  75. return $this->loadResource($type)->clearAll($exp_time);
  76. }
  77. /**
  78. * Empty cache for a specific template
  79. *
  80. * @param string $template_name template name
  81. * @param string $cache_id cache id
  82. * @param string $compile_id compile id
  83. * @param integer $exp_time expiration time
  84. * @param string $type resource type
  85. * @return integer number of cache files deleted
  86. */
  87. function clear($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
  88. {
  89. // load cache resource
  90. $cacheResource = $this->loadResource($type);
  91. return $cacheResource->clear($template_name, $cache_id, $compile_id, $exp_time);
  92. }
  93. }