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:----OoEew8nl/cNwmmPaEKlVEgYeWTeItkW+lgAWNxK2EybSkiMe+V2tjpz+TEYSnSfV5hO3U9q6Sd5sRCX6jpl1PtPvHA436dGFCVHgCn//VLkCSIjfaHt5X72wVQleHx2EPvqKLSzKXp5J70u2Brl0Bf6iHMrPSsAW1uSb4rmLUVnuJe1M6q/vSajFICl0DiIN4WmCDydiI9keaKm+yexEZFwLIxQmJMUOET+zdIf6qeQhtbgpFclPCo3zLK6vknRRAqXncGEVrdYxzqsBCY+FFQYUm4O88PA8exubJdFaeDX96QIfd8BB/iOcHfqMqZoMY6WGkalFxURtE0G/x6UdkzfLyD60B6Gwlp2/b3f9yjk7AtBcFBUtFRjz2wH5NJeykkXc2wd3EwLy3cKswp6avkhZvtBR3QDyDsTPcveJl9nWmzC5LmupktceNhBcQTe978Nfxd1tIDJ5+6258VEQ2sL44wBeCNzNS3kx046Qrip9lIP1rENhLMJGej7eEnMAfcby6hE+uvvPoiukJ5YNU1tsCJdRaTVDviXnAWxNr/Az8vmBhjgpmH3IF4/kSkRn92gS3ZEuCHQ+RHEhKqEdbSxJz3WMUNTEvCK0nNC5WEu8dXFegVORJi4i5JLpFU01n2ra9/Td0+q8Un+qDBiCdf+J0nkQ2aURFDK4lgU9vjI=----ATTACHMENT:----NzczMzgzNDU5MjcwMjg2OSAxNjQ1MDI2MjgyMDQ5ODM3IDU2MzI2NzI4NDE1NjY4NzA=