예제 #1
0
 /**
  * Call and cache a class method
  *
  * @param  string $method  Method name to call
  * @param  array  $args    Method arguments
  * @param  array  $options Cache options
  * @return mixed
  * @throws Exception
  */
 public function call($method, array $args = array(), array $options = array())
 {
     $classOptions = $this->getOptions();
     $classname = $classOptions->getClass();
     $method = strtolower($method);
     $callback = $classname . '::' . $method;
     $cache = $classOptions->getCacheByDefault();
     if ($cache) {
         $cache = !in_array($method, $classOptions->getClassNonCacheMethods());
     } else {
         $cache = in_array($method, $classOptions->getClassCacheMethods());
     }
     if (!$cache) {
         if ($args) {
             return call_user_func_array($callback, $args);
         } else {
             return $classname::$method();
         }
     }
     // speed up key generation
     if (!isset($options['callback_key'])) {
         $options['callback_key'] = $callback;
     }
     return parent::call($callback, $args, $options);
 }
예제 #2
0
 /**
  * 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);
 }
예제 #3
0
 /**
  * Call and cache a class method
  *
  * @param  string $method  Method name to call
  * @param  array  $args    Method arguments
  * @param  array  $options Cache options
  * @return mixed
  * @throws Exception
  */
 public function call($method, array $args = array(), array $options = array())
 {
     $classOptions = $this->getOptions();
     $object = $classOptions->getObject();
     $method = strtolower($method);
     // handle magic methods
     switch ($method) {
         case '__set':
             $property = array_shift($args);
             $value = array_shift($args);
             $object->{$property} = $value;
             if (!$classOptions->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', array($property), $options);
             }
             if (method_exists($object, '__isset')) {
                 $removeKeys[] = $this->generateKey('__isset', array($property), $options);
             }
             if ($removeKeys) {
                 $classOptions->getStorage()->removeMulti($removeKeys);
             }
             return;
         case '__get':
             $property = array_shift($args);
             if (!$classOptions->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);
             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::call(array($object, '__get'), $args, $options);
         case '__isset':
             $property = array_shift($args);
             if (!$classOptions->getObjectCacheMagicProperties() || property_exists($object, $property)) {
                 // no caching if property isn't magic
                 // or caching magic properties is disabled
                 return isset($object->{$property});
             }
             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::call(array($object, '__isset'), array($property), $options);
         case '__unset':
             $property = array_shift($args);
             unset($object->{$property});
             if (!$classOptions->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', array($property), $options);
             }
             if (method_exists($object, '__isset')) {
                 $removeKeys[] = $this->generateKey('__isset', array($property), $options);
             }
             if ($removeKeys) {
                 $classOptions->getStorage()->removeMulti($removeKeys);
             }
             return;
     }
     $cache = $classOptions->getCacheByDefault();
     if ($cache) {
         $cache = !in_array($method, $classOptions->getObjectNonCacheMethods());
     } else {
         $cache = in_array($method, $classOptions->getObjectCacheMethods());
     }
     if (!$cache) {
         if ($args) {
             return call_user_func_array(array($object, $method), $args);
         }
         return $object->{$method}();
     }
     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::call(array($object, $method), $args, $options);
 }
예제 #4
0
 /**
  * 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);
 }