Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * This class can be used to cache the result and output of any PHP callable (function and method calls).
13
 *
14
 * @package    symfony
15
 * @subpackage cache
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfFunctionCache.class.php 23939 2009-11-14 17:46:14Z fabien $
18
 */
19
class sfFunctionCache
20
{
21
  protected $cache = null;
22
 
23
  /**
24
   * Constructor.
25
   *
26
   * @param sfCache $cache An sfCache object instance
27
   */
28
  public function __construct(sfCache $cache)
29
  {
30
    $this->cache = $cache;
31
  }
32
 
33
  /**
34
   * Calls a cacheable function or method (or not if there is already a cache for it).
35
   *
36
   * Arguments of this method are read with func_get_args. So it doesn't appear in the function definition.
37
   *
38
   * The first argument can be any PHP callable:
39
   *
40
   * $cache->call('functionName', array($arg1, $arg2));
41
   * $cache->call(array($object, 'methodName'), array($arg1, $arg2));
42
   *
43
   * @param mixed $callable  A PHP callable
44
   * @param array $arguments An array of arguments to pass to the callable
45
   *
46
   * @return mixed The result of the function/method
47
   */
48
  public function call($callable, $arguments = array())
49
  {
50
    // Generate a cache id
51
    $key = $this->computeCacheKey($callable, $arguments);
52
 
53
    $serialized = $this->cache->get($key);
54
    if ($serialized !== null)
55
    {
56
      $data = unserialize($serialized);
57
    }
58
    else
59
    {
60
      $data = array();
61
 
62
      if (!is_callable($callable))
63
      {
64
        throw new sfException('The first argument to call() must be a valid callable.');
65
      }
66
 
67
      ob_start();
68
      ob_implicit_flush(false);
69
 
70
      try
71
      {
72
        $data['result'] = call_user_func_array($callable, $arguments);
73
      }
74
      catch (Exception $e)
75
      {
76
        ob_end_clean();
77
        throw $e;
78
      }
79
 
80
      $data['output'] = ob_get_clean();
81
 
82
      $this->cache->set($key, serialize($data));
83
    }
84
 
85
    echo $data['output'];
86
 
87
    return $data['result'];
88
  }
89
 
90
  /**
91
   * Returns the cache instance.
92
   *
93
   * @return sfCache The sfCache instance
94
   */
95
  public function getCache()
96
  {
97
    return $this->cache;
98
  }
99
 
100
  /**
101
   * Computes the cache key for a given callable and the arguments.
102
   *
103
   * @param mixed $callable  A PHP callable
104
   * @param array $arguments An array of arguments to pass to the callable
105
   *
106
   * @return string The associated cache key
107
   */
108
  public function computeCacheKey($callable, $arguments = array())
109
  {
110
    return md5(serialize($callable).serialize($arguments));
111
  }
112
}