/** * Call and cache a class method * * @param string $method Method name to call * @param array $args Method arguments * @return mixed * @throws Exception */ public function call($method, array $args = array()) { $options = $this->getOptions(); $classname = $options->getClass(); $method = strtolower($method); $callback = $classname . '::' . $method; $cache = $options->getCacheByDefault(); if ($cache) { $cache = !in_array($method, $options->getClassNonCacheMethods()); } else { $cache = in_array($method, $options->getClassCacheMethods()); } if (!$cache) { if ($args) { return call_user_func_array($callback, $args); } else { return $classname::$method(); } } return parent::call($callback, $args); }
/** * Call and cache a class method * * @param string $method Method name to call * @param array $args Method arguments * @return mixed * @throws Exception\RuntimeException * @throws \Exception */ public function call($method, array $args = []) { $options = $this->getOptions(); $object = $options->getObject(); $method = strtolower($method); // handle magic methods switch ($method) { case '__set': $property = array_shift($args); $value = array_shift($args); $object->{$property} = $value; if (!$options->getObjectCacheMagicProperties() || property_exists($object, $property)) { // no caching if property isn't magic // or caching magic properties is disabled return; } // remove cached __get and __isset $removeKeys = null; if (method_exists($object, '__get')) { $removeKeys[] = $this->generateKey('__get', [$property]); } if (method_exists($object, '__isset')) { $removeKeys[] = $this->generateKey('__isset', [$property]); } if ($removeKeys) { $options->getStorage()->removeItems($removeKeys); } return; case '__get': $property = array_shift($args); if (!$options->getObjectCacheMagicProperties() || property_exists($object, $property)) { // no caching if property isn't magic // or caching magic properties is disabled return $object->{$property}; } array_unshift($args, $property); return parent::call([$object, '__get'], $args); case '__isset': $property = array_shift($args); if (!$options->getObjectCacheMagicProperties() || property_exists($object, $property)) { // no caching if property isn't magic // or caching magic properties is disabled return isset($object->{$property}); } return parent::call([$object, '__isset'], [$property]); case '__unset': $property = array_shift($args); unset($object->{$property}); if (!$options->getObjectCacheMagicProperties() || property_exists($object, $property)) { // no caching if property isn't magic // or caching magic properties is disabled return; } // remove previous cached __get and __isset calls $removeKeys = null; if (method_exists($object, '__get')) { $removeKeys[] = $this->generateKey('__get', [$property]); } if (method_exists($object, '__isset')) { $removeKeys[] = $this->generateKey('__isset', [$property]); } if ($removeKeys) { $options->getStorage()->removeItems($removeKeys); } return; } $cache = $options->getCacheByDefault(); if ($cache) { $cache = !in_array($method, $options->getObjectNonCacheMethods()); } else { $cache = in_array($method, $options->getObjectCacheMethods()); } if (!$cache) { if ($args) { return call_user_func_array([$object, $method], $args); } return $object->{$method}(); } return parent::call([$object, $method], $args); }
/** * Generate a unique key from the method name and arguments * * @param string $method The method name * @param array $args Method arguments * @param array $options Options * @return string * @throws Exception */ public function generateKey($method, array $args = array(), array $options = array()) { $classOptions = $this->getOptions(); if (!isset($options['callback_key'])) { if (isset($options['entity_key']) && ($entityKey = $options['entity_key']) !== null || ($entityKey = $classOptions->getObjectKey() !== null)) { $options['callback_key'] = $entityKey . '::' . strtolower($method); unset($options['entity_key']); } } return parent::generateKey(array($classOptions->getObject(), $method), $args, $options); }
/** * Generate a key from the method name and arguments * * @param string $method The method name * @param array $args Method arguments * @return string * @throws Exception */ public function generateKey($method, array $args = array(), array $options = array()) { // speed up key generation $classOptions = $this->getOptions(); if (!isset($options['callback_key'])) { $callback = $classOptions->getClass() . '::' . strtolower($method); $options['callback_key'] = $callback; } else { $callback = $classOptions->getClass() . '::' . $method; } return parent::generateKey($callback, $args, $options); }