Guest User

Untitled

a guest
Jul 15th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Kdyby\Tools;
  4.  
  5. use Nette;
  6. use Nette\Environment;
  7. use Nette\ObjectMixin;
  8. use Kdyby;
  9.  
  10.  
  11.  
  12. /**
  13. * @author Filip Procházka <hosiplan@kdyby.org>
  14. */
  15. class ModelTools extends Nette\Object
  16. {
  17.  
  18. const CACHED_PREFIX = 'c_';
  19.  
  20. private static $methodCache;
  21.  
  22.  
  23.  
  24. /**
  25. * Call to cached or undefined method.
  26. * @param string method name
  27. * @param array arguments
  28. * @return mixed
  29. * @throws \MemberAccessException
  30. * @throws \InvalidStateException
  31. */
  32. public static function tryCall($_this, $name, $args)
  33. {
  34. if (substr($name, 0, strlen(self::CACHED_PREFIX)) != self::CACHED_PREFIX) {
  35. return ObjectMixin::call($_this, $name, $args);
  36. }
  37.  
  38. $method = substr($name, strlen(self::CACHED_PREFIX));
  39. if (!\method_exists($_this, $method)){
  40. throw new \InvalidStateException('Invalid call for cached output of ' . get_class($_this) . '::' . $method . ', method not set.');
  41. }
  42.  
  43. $reflection = new Nette\Reflection\MethodReflection($_this, $method);
  44. $annotations = $reflection->getAnnotations();
  45.  
  46. if (isset($annotations['Cache'])) {
  47. $key = get_class($_this) . '::' . $method . '?' . md5(serialize($args));
  48. $cache = self::getMethodCache();
  49.  
  50. if (!isset($cache[$key])) {
  51. $data = call_user_func_array(array($_this, $method), $args);
  52.  
  53. if (is_array($annotations['Cache'])) {
  54. $depends = array();
  55.  
  56. $options = $annotations['Cache'];
  57. $options = is_array($options) ? current($options) : array();
  58. $options = $options instanceof \ArrayObject ? $options->getArrayCopy() : $options;
  59.  
  60. if (isset($annotations['Tags'])) {
  61. $tags = is_array($tags = $annotations['Tags']) ? current($tags) : array();
  62. $tags = $tags instanceof \ArrayObject ? $tags->getArrayCopy() : $tags;
  63. $options[Nette\Caching\Cache::TAGS] = $tags;
  64. }
  65.  
  66. foreach($options as $index => $value) {
  67. $const = constant('Nette\Caching\Cache::' . strtoupper($index));
  68. if ($const) {
  69. $depends += array($const => $value);
  70.  
  71. } else {
  72. throw new \InvalidArgumentException('Invalid cache options set for `' . $key . '`, dependency on `' . $index . '` not found.');
  73. }
  74. }
  75. $cache->save($key, $data, $depends);
  76.  
  77. } else {
  78. $cache[$key] = $data;
  79. }
  80. }
  81.  
  82. return $cache[$key];
  83. }
  84.  
  85. return call_user_func_array(array($_this, $method), $args);
  86. }
  87.  
  88.  
  89.  
  90. /**
  91. * @return Nette\Caching\Cache
  92. */
  93. private static function getMethodCache()
  94. {
  95. if (self::$methodCache === NULL) {
  96. self::$methodCache = Environment::getCache('Kdyby.ClassMethods.Data');
  97. }
  98.  
  99. return self::$methodCache;
  100. }
  101.  
  102.  
  103.  
  104. /**
  105. * @param array $conditions
  106. * @return <type>
  107. */
  108. public static function cleanCache($conditions)
  109. {
  110. self::getMethodCache()->clean($conditions);
  111. }
  112.  
  113. }
Add Comment
Please, Sign In to add comment