File indexing completed on 2024-12-22 05:37:01
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Server 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Zend_Server_Cache: cache server definitions 0024 * 0025 * @category Zend 0026 * @package Zend_Server 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Server_Cache 0031 { 0032 /** 0033 * @var array Methods to skip when caching server 0034 */ 0035 protected static $_skipMethods = array(); 0036 0037 /** 0038 * Cache a file containing the dispatch list. 0039 * 0040 * Serializes the server definition stores the information 0041 * in $filename. 0042 * 0043 * Returns false on any error (typically, inability to write to file), true 0044 * on success. 0045 * 0046 * @param string $filename 0047 * @param Zend_Server_Interface $server 0048 * @return bool 0049 */ 0050 public static function save($filename, Zend_Server_Interface $server) 0051 { 0052 if (!is_string($filename) 0053 || (!file_exists($filename) && !is_writable(dirname($filename)))) 0054 { 0055 return false; 0056 } 0057 0058 $methods = $server->getFunctions(); 0059 0060 if ($methods instanceof Zend_Server_Definition) { 0061 $definition = new Zend_Server_Definition(); 0062 foreach ($methods as $method) { 0063 if (in_array($method->getName(), self::$_skipMethods)) { 0064 continue; 0065 } 0066 $definition->addMethod($method); 0067 } 0068 $methods = $definition; 0069 } 0070 0071 if (0 === @file_put_contents($filename, serialize($methods))) { 0072 return false; 0073 } 0074 0075 return true; 0076 } 0077 0078 /** 0079 * Load server definition from a file 0080 * 0081 * Unserializes a stored server definition from $filename. Returns false if 0082 * it fails in any way, true on success. 0083 * 0084 * Useful to prevent needing to build the server definition on each 0085 * request. Sample usage: 0086 * 0087 * <code> 0088 * if (!Zend_Server_Cache::get($filename, $server)) { 0089 * // require_once 'Some/Service/Class.php'; 0090 * // require_once 'Another/Service/Class.php'; 0091 * 0092 * // Attach Some_Service_Class with namespace 'some' 0093 * $server->attach('Some_Service_Class', 'some'); 0094 * 0095 * // Attach Another_Service_Class with namespace 'another' 0096 * $server->attach('Another_Service_Class', 'another'); 0097 * 0098 * Zend_Server_Cache::save($filename, $server); 0099 * } 0100 * 0101 * $response = $server->handle(); 0102 * echo $response; 0103 * </code> 0104 * 0105 * @param string $filename 0106 * @param Zend_Server_Interface $server 0107 * @return bool 0108 */ 0109 public static function get($filename, Zend_Server_Interface $server) 0110 { 0111 if (!is_string($filename) 0112 || !file_exists($filename) 0113 || !is_readable($filename)) 0114 { 0115 return false; 0116 } 0117 0118 0119 if (false === ($dispatch = @file_get_contents($filename))) { 0120 return false; 0121 } 0122 0123 if (false === ($dispatchArray = @unserialize($dispatch))) { 0124 return false; 0125 } 0126 0127 $server->loadFunctions($dispatchArray); 0128 0129 return true; 0130 } 0131 0132 /** 0133 * Remove a cache file 0134 * 0135 * @param string $filename 0136 * @return boolean 0137 */ 0138 public static function delete($filename) 0139 { 0140 if (is_string($filename) && file_exists($filename)) { 0141 unlink($filename); 0142 return true; 0143 } 0144 0145 return false; 0146 } 0147 }