/** * @param HasEmitterInterface $subject * @param array $parameters * @return AuthSubscriber */ public static function attach(HasEmitterInterface $subject, array $parameters) { $subscriber = new self($parameters); $emitter = $subject->getEmitter(); $emitter->attach($subscriber); return $subscriber; }
/** * Attaches event listeners and properly sets their priorities and whether * or not they are are only executed once. * * @param HasEmitterInterface $object Object that has the event emitter. * @param array $listeners Array of hashes representing event * event listeners. Each item contains * "name", "fn", "priority", & "once". */ private function attachListeners(HasEmitterInterface $object, array $listeners) { $emitter = $object->getEmitter(); foreach ($listeners as $el) { if ($el['once']) { $emitter->once($el['name'], $el['fn'], $el['priority']); } else { $emitter->on($el['name'], $el['fn'], $el['priority']); } } }
/** * Helper method used to easily attach a cache to a request or client. * * This method accepts an array of options that are used to control the * caching behavior: * * - storage: An optional GuzzleHttp\Subscriber\Cache\CacheStorageInterface. * If no value is not provided, an in-memory array cache will be used. * - validate: Boolean value that determines if cached response are ever * validated against the origin server. Defaults to true but can be * disabled by passing false. * - purge: Boolean value that determines if cached responses are purged * when non-idempotent requests are sent to their URI. Defaults to true * but can be disabled by passing false. * - can_cache: An optional callable used to determine if a request can be * cached. The callable accepts a RequestInterface and returns a boolean * value. If no value is provided, the default behavior is utilized. * * @param HasEmitterInterface $subject Client or request to attach to, * @param array $options Options used to control the cache. * * @return array Returns an associative array containing a 'subscriber' key * that holds the created CacheSubscriber, and a 'storage' * key that contains the cache storage used by the subscriber. */ public static function attach(HasEmitterInterface $subject, array $options = []) { if (!isset($options['storage'])) { $options['storage'] = new CacheStorage(new ArrayCache()); } if (!isset($options['can_cache'])) { $options['can_cache'] = ['GuzzleHttp\\Subscriber\\Cache\\Utils', 'canCacheRequest']; } $emitter = $subject->getEmitter(); $cache = new self($options['storage'], $options['can_cache']); $emitter->attach($cache); if (!isset($options['validate']) || $options['validate'] === true) { $emitter->attach(new ValidationSubscriber($options['storage'], $options['can_cache'])); } if (!isset($options['purge']) || $options['purge'] === true) { $emitter->attach(new PurgeSubscriber($options['storage'])); } return ['subscriber' => $cache, 'storage' => $options['storage']]; }
/** * @param HasEmitterInterface $client */ public function subscribe(HasEmitterInterface $client) { $client->getEmitter()->attach($this); }