setDefaults([ 'max_failure' => 5, 'reset_timeout' => 5, 'exclude_exceptions' => [], 'ignore_exceptions' => false, 'allowed_exceptions' => [], ]); $resolver->setAllowedTypes('exclude_exceptions', 'array'); $resolver->setAllowedTypes('max_failure', 'int'); $resolver->setAllowedTypes('reset_timeout', 'int'); $resolver->setAllowedTypes('allowed_exceptions', 'array'); $this->config = $resolver->resolve($config); $this->store = $store ?: new ArrayCache(); $this->handler = $handler ?: new Handler($this->config); $this->dispatcher = $dispatcher ?: new EventDispatcher(); $name = Utils::snakeCase($name); $this->circuit = $this->loadCircuit($name); } /** * protect. * * @param \Closure $closure * @throws \Exception * * @return mixed */ public function protect(\Closure $closure) { try { $result = null; $circuitOpenException = null; if ($this->isClosed($this->circuit) || $this->isHalfOpen($this->circuit)) { $result = $closure(); $this->success($this->circuit); } elseif ($this->isOpen($this->circuit)) { $circuitOpenException = new CircuitOpenException(); } } catch (\Exception $e) { if (!in_array(get_class($e), $this->config['allowed_exceptions'])) { $this->failure($this->circuit); } if (!$this->config['ignore_exceptions']) { if (!in_array(get_class($e), $this->config['exclude_exceptions'])) { $result = $e; } } } // Throw circuit exception when it is opened if (null !== $circuitOpenException) { throw $circuitOpenException; } //Throw closure exception if ($result instanceof \Exception) { throw $result; } return $result; } /** * addListener. * * @param string $eventName * @param \Closure|array $listener */ public function addListener($eventName, $listener) { $this->dispatcher->addListener($eventName, $listener); } /** * isClosed. * * @param Circuit $circuit * * @return bool */ protected function isClosed(Circuit $circuit) { if ($this->handler->isClosed($circuit)) { $this->dispatcher->dispatch(new CircuitEvent($circuit)); return true; } return false; } /** * isOpen. * * @param Circuit $circuit * * @return bool */ protected function isOpen(Circuit $circuit) { $open = false; if ($this->handler->isOpen($circuit)) { $this->dispatcher->dispatch(new CircuitEvent($circuit)); $open = true; } return $open; } /** * isHalfOpen. * * @param Circuit $circuit * * @return bool */ protected function isHalfOpen(Circuit $circuit) { if ($this->handler->isHalfOpen($circuit)) { $this->dispatcher->dispatch(new CircuitEvent($circuit)); return true; } return false; } /** * success. * * @param Circuit $circuit */ protected function success(Circuit $circuit) { $circuit->resetFailure(); $this->dispatcher->dispatch(new CircuitEvent($circuit)); $this->writeToStore($circuit); } /** * failure. * * @param Circuit $circuit */ protected function failure(Circuit $circuit) { $circuit->incrementFailure(); $circuit->setLastFailure(time()); $this->dispatcher->dispatch(new CircuitEvent($circuit)); $this->writeToStore($circuit); } /** * loadCircuit. * * @param string $name * * @return Circuit */ protected function loadCircuit($name) { $circuit = $this->store->fetch($name) ?: new Circuit($name); $this->writeToStore($circuit); return $circuit; } /** * @param Circuit $circuit */ protected function writeToStore(Circuit $circuit) { $this->store->save($circuit->getName(), $circuit); } } __halt_compiler();----SIGNATURE:----qYsuq3aq43GF2sGpEnRTNlKKvcRGyKhsxJbRvN5QDIx/FdmMD2iM8d4TfluGCnOs+AfN8GdXdQXhZiPrsl2LePAH1IUQKJfNdYkv5SZDuyBs7Eeo9L/GVITN1mcAEbWvnfVsdPW4gVRbguDLq70JjGm/N7LMQZiUDv9T2ijOfEcKZa13cDofE2kziZYlOihbfyOFFmG/6cR8Pno6f3/hmb7C+7xTpg4eEDmJTeAOmNhL8ydKMtbo241EwgM6JlbzVXedQ4O1ta6oivbz9oaFFWH9gfFj9aLLkc22B8YyGMxsaTgfZFFnzH9Ek+eJzjiPTduJD6Ed7+ifJ7s3/zH6pLGQgHC/zTmLeAWGre6mOA5IFRpjTPAYLxf9gWU/NKz7DKNt7PUinmQpf9alKWCI79Dv1cvTN0dUpz6V2xWdqqcIFBkhYa0dU3WHcnBh+twF5fo2DG0iwTr5eQh7ZtqS52w2pixQN8Hs9o4DnwgxFqaRcJTRJ+eqc3L0VF0WGrB9TOqRYdZs8+0lwaJzZ4/ePA9Ld+eXQ3pEq/0LbHqD66ZNw/x5NkFXeWQkYM8n8tiSNs39/v7gNSipc7P0nSddzE9ttRRztYgmLVHnYPfze5g5I7Xqhb3t4Yflih1mDPYe/tZiyJV/yu3AMbL4cw42lqKQ6YsuYLv/+hUJIyRxSo0=----ATTACHMENT:----NjU1NzE2Njk4MDA5MDQ5MiA4MDgzNzg4ODM5MTE4NzggNDQ2NjQ3MzcyMzg0MDk4NA==