|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Server |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Cache.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * Zend_Server_Cache: cache server definitions |
|
24 * |
|
25 * @category Zend |
|
26 * @package Zend_Server |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Server_Cache |
|
31 { |
|
32 /** |
|
33 * @var array Methods to skip when caching server |
|
34 */ |
|
35 protected static $_skipMethods = array(); |
|
36 |
|
37 /** |
|
38 * Cache a file containing the dispatch list. |
|
39 * |
|
40 * Serializes the server definition stores the information |
|
41 * in $filename. |
|
42 * |
|
43 * Returns false on any error (typically, inability to write to file), true |
|
44 * on success. |
|
45 * |
|
46 * @param string $filename |
|
47 * @param Zend_Server_Interface $server |
|
48 * @return bool |
|
49 */ |
|
50 public static function save($filename, Zend_Server_Interface $server) |
|
51 { |
|
52 if (!is_string($filename) |
|
53 || (!file_exists($filename) && !is_writable(dirname($filename)))) |
|
54 { |
|
55 return false; |
|
56 } |
|
57 |
|
58 $methods = $server->getFunctions(); |
|
59 |
|
60 if ($methods instanceof Zend_Server_Definition) { |
|
61 $definition = new Zend_Server_Definition(); |
|
62 foreach ($methods as $method) { |
|
63 if (in_array($method->getName(), self::$_skipMethods)) { |
|
64 continue; |
|
65 } |
|
66 $definition->addMethod($method); |
|
67 } |
|
68 $methods = $definition; |
|
69 } |
|
70 |
|
71 if (0 === @file_put_contents($filename, serialize($methods))) { |
|
72 return false; |
|
73 } |
|
74 |
|
75 return true; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Load server definition from a file |
|
80 * |
|
81 * Unserializes a stored server definition from $filename. Returns false if |
|
82 * it fails in any way, true on success. |
|
83 * |
|
84 * Useful to prevent needing to build the server definition on each |
|
85 * request. Sample usage: |
|
86 * |
|
87 * <code> |
|
88 * if (!Zend_Server_Cache::get($filename, $server)) { |
|
89 * require_once 'Some/Service/Class.php'; |
|
90 * require_once 'Another/Service/Class.php'; |
|
91 * |
|
92 * // Attach Some_Service_Class with namespace 'some' |
|
93 * $server->attach('Some_Service_Class', 'some'); |
|
94 * |
|
95 * // Attach Another_Service_Class with namespace 'another' |
|
96 * $server->attach('Another_Service_Class', 'another'); |
|
97 * |
|
98 * Zend_Server_Cache::save($filename, $server); |
|
99 * } |
|
100 * |
|
101 * $response = $server->handle(); |
|
102 * echo $response; |
|
103 * </code> |
|
104 * |
|
105 * @param string $filename |
|
106 * @param Zend_Server_Interface $server |
|
107 * @return bool |
|
108 */ |
|
109 public static function get($filename, Zend_Server_Interface $server) |
|
110 { |
|
111 if (!is_string($filename) |
|
112 || !file_exists($filename) |
|
113 || !is_readable($filename)) |
|
114 { |
|
115 return false; |
|
116 } |
|
117 |
|
118 |
|
119 if (false === ($dispatch = @file_get_contents($filename))) { |
|
120 return false; |
|
121 } |
|
122 |
|
123 if (false === ($dispatchArray = @unserialize($dispatch))) { |
|
124 return false; |
|
125 } |
|
126 |
|
127 $server->loadFunctions($dispatchArray); |
|
128 |
|
129 return true; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Remove a cache file |
|
134 * |
|
135 * @param string $filename |
|
136 * @return boolean |
|
137 */ |
|
138 public static function delete($filename) |
|
139 { |
|
140 if (is_string($filename) && file_exists($filename)) { |
|
141 unlink($filename); |
|
142 return true; |
|
143 } |
|
144 |
|
145 return false; |
|
146 } |
|
147 } |