Development

/branches/1.3/lib/cache/sfFunctionCache.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/cache/sfFunctionCache.class.php

Revision 23810, 2.9 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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($cache)
29   {
30     if (!is_object($cache))
31     {
32       $this->cache = new sfFileCache($cache);
33
34       throw new sfException('DEPRECATED: You must now pass a sfCache object when initializing a sfFunctionCache object. Be warned that the call() method signature has also changed.');
35     }
36
37     $this->cache = $cache;
38   }
39
40   /**
41    * Calls a cacheable function or method (or not if there is already a cache for it).
42    *
43    * Arguments of this method are read with func_get_args. So it doesn't appear in the function definition.
44    *
45    * The first argument can be any PHP callable:
46    *
47    * $cache->call('functionName', array($arg1, $arg2));
48    * $cache->call(array($object, 'methodName'), array($arg1, $arg2));
49    *
50    * @param mixed $callable  A PHP callable
51    * @param array $arguments An array of arguments to pass to the callable
52    *
53    * @return mixed The result of the function/method
54    */
55   public function call($callable, $arguments = array())
56   {
57     // Generate a cache id
58     $key = $this->computeCacheKey($callable, $arguments);
59
60     $serialized = $this->cache->get($key);
61     if ($serialized !== null)
62     {
63       $data = unserialize($serialized);
64     }
65     else
66     {
67       $data = array();
68
69       if (!is_callable($callable))
70       {
71         throw new sfException('The first argument to call() must be a valid callable.');
72       }
73
74       ob_start();
75       ob_implicit_flush(false);
76
77       try
78       {
79         $data['result'] = call_user_func_array($callable, $arguments);
80       }
81       catch (Exception $e)
82       {
83         ob_end_clean();
84         throw $e;
85       }
86
87       $data['output'] = ob_get_clean();
88
89       $this->cache->set($key, serialize($data));
90     }
91
92     echo $data['output'];
93
94     return $data['result'];
95   }
96
97   /**
98    * Returns the cache instance.
99    *
100    * @return sfCache The sfCache instance
101    */
102   public function getCache()
103   {
104     return $this->cache;
105   }
106
107   /**
108    * Computes the cache key for a given callable and the arguments.
109    *
110    * @param mixed $callable  A PHP callable
111    * @param array $arguments An array of arguments to pass to the callable
112    *
113    * @return string The associated cache key
114    */
115   public function computeCacheKey($callable, $arguments = array())
116   {
117     return md5(serialize($callable).serialize($arguments));
118   }
119 }
120
Note: See TracBrowser for help on using the browser.