Cache.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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-2009 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 2504 2011-12-28 07:35:29Z liu21st $
  20. */
  21. /**
  22. * Zend_Server_Cache: cache server definitions
  23. *
  24. * @category Zend
  25. * @package Zend_Server
  26. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Server_Cache
  30. {
  31. /**
  32. * @var array Methods to skip when caching server
  33. */
  34. protected static $_skipMethods = array();
  35. /**
  36. * Cache a file containing the dispatch list.
  37. *
  38. * Serializes the server definition stores the information
  39. * in $filename.
  40. *
  41. * Returns false on any error (typically, inability to write to file), true
  42. * on success.
  43. *
  44. * @param string $filename
  45. * @param Zend_Server_Interface $server
  46. * @return bool
  47. */
  48. public static function save($filename, Zend_Server_Interface $server)
  49. {
  50. if (!is_string($filename)
  51. || (!file_exists($filename) && !is_writable(dirname($filename))))
  52. {
  53. return false;
  54. }
  55. $methods = $server->getFunctions();
  56. if ($methods instanceof Zend_Server_Definition) {
  57. $definition = new Zend_Server_Definition();
  58. foreach ($methods as $method) {
  59. if (in_array($method->getName(), self::$_skipMethods)) {
  60. continue;
  61. }
  62. $definition->addMethod($method);
  63. }
  64. $methods = $definition;
  65. }
  66. if (0 === @file_put_contents($filename, serialize($methods))) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Load server definition from a file
  73. *
  74. * Unserializes a stored server definition from $filename. Returns false if
  75. * it fails in any way, true on success.
  76. *
  77. * Useful to prevent needing to build the server definition on each
  78. * request. Sample usage:
  79. *
  80. * <code>
  81. * if (!Zend_Server_Cache::get($filename, $server)) {
  82. * require_once 'Some/Service/Class.php';
  83. * require_once 'Another/Service/Class.php';
  84. *
  85. * // Attach Some_Service_Class with namespace 'some'
  86. * $server->attach('Some_Service_Class', 'some');
  87. *
  88. * // Attach Another_Service_Class with namespace 'another'
  89. * $server->attach('Another_Service_Class', 'another');
  90. *
  91. * Zend_Server_Cache::save($filename, $server);
  92. * }
  93. *
  94. * $response = $server->handle();
  95. * echo $response;
  96. * </code>
  97. *
  98. * @param string $filename
  99. * @param Zend_Server_Interface $server
  100. * @return bool
  101. */
  102. public static function get($filename, Zend_Server_Interface $server)
  103. {
  104. if (!is_string($filename)
  105. || !file_exists($filename)
  106. || !is_readable($filename))
  107. {
  108. return false;
  109. }
  110. if (false === ($dispatch = @file_get_contents($filename))) {
  111. return false;
  112. }
  113. if (false === ($dispatchArray = @unserialize($dispatch))) {
  114. return false;
  115. }
  116. $server->loadFunctions($dispatchArray);
  117. return true;
  118. }
  119. /**
  120. * Remove a cache file
  121. *
  122. * @param string $filename
  123. * @return boolean
  124. */
  125. public static function delete($filename)
  126. {
  127. if (is_string($filename) && file_exists($filename)) {
  128. unlink($filename);
  129. return true;
  130. }
  131. return false;
  132. }
  133. }