Development

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

You must first sign up to be able to contribute.

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

Revision 23725, 2.8 kB (checked in by FabianLange, 4 years ago)

[1.0] stimulating autoloading in sfFunctionCache by calling class_exists before calling static methods (closes #7540)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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 functions/methods.
13  *
14  * This class is based on the PEAR_Cache_Lite class.
15  * All cache files are stored in files in the [sf_root_dir].'/cache/'.[sf_app].'/function' directory.
16  * To disable all caching, you can set to false [sf_cache] constant.
17  *
18  * @package    symfony
19  * @subpackage cache
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Fabien Marty <fab@php.net>
22  * @version    SVN: $Id$
23  */
24 class sfFunctionCache extends sfFileCache
25 {
26   /**
27    * Calls a cacheable function or method (or not if there is already a cache for it).
28    *
29    * Arguments of this method are read with func_get_args. So it doesn't appear in the function definition. Synopsis :
30    * call('functionName', $arg1, $arg2, ...)
31    * (arg1, arg2... are arguments of 'functionName')
32    *
33    * @return mixed The result of the function/method
34    */
35   public function call()
36   {
37     $arguments = func_get_args();
38
39     // Generate a cache id
40     $id = md5(serialize($arguments));
41
42     $data = $this->get($id);
43     if ($data !== null)
44     {
45       $array = unserialize($data);
46       $output = $array['output'];
47       $result = $array['result'];
48     }
49     else
50     {
51       $target = array_shift($arguments);
52       ob_start();
53       ob_implicit_flush(false);
54       if (is_string($target) && strstr($target, '::'))
55       {
56         // classname::staticMethod
57         list($class, $method) = explode('::', $target);
58         try
59         {
60           class_exists($class);
61           $result = call_user_func_array(array($class, $method), $arguments);
62         }
63         catch (Exception $e)
64         {
65           ob_end_clean();
66           throw $e;
67         }
68       }
69       else if (is_string($target) && strstr($target, '->'))
70       {
71         // object->method
72         // use a stupid name ($objet_123456789 because) of problems when the object
73         // name is the same as this var name
74         list($object_123456789, $method) = explode('->', $target);
75         global $$object_123456789;
76         try
77         {
78           $result = call_user_func_array(array($$object_123456789, $method), $arguments);
79         }
80         catch (Exception $e)
81         {
82           ob_end_clean();
83           throw $e;
84         }
85       }
86       else
87       {
88         // function
89         $result = call_user_func_array($target, $arguments);
90       }
91       $output = ob_get_contents();
92       ob_end_clean();
93
94       $array['output'] = $output;
95       $array['result'] = $result;
96
97       $this->set($id, '', serialize($array));
98     }
99
100     echo($output);
101     return $result;
102   }
103 }
104
Note: See TracBrowser for help on using the browser.