/** * Execute a Closure within a transaction. * \Illuminate\Database\Concerns\ManagesTransactions * * @param \Closure $callback * @param int $attempts * @return mixed * * @throws \Exception|\Throwable */ publicfunctiontransaction(Closure$callback, $attempts = 1) { for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) { $this->beginTransaction();
// We'll simply execute the given callback within a try / catch block and if we // catch any exception we can rollback this transaction so that none of this // gets actually persisted to a database or stored in a permanent fashion. try { return tap($callback($this), function () { $this->commit(); }); }
// If we catch an exception we'll rollback this transaction and try again if we // are not out of attempts. If we are out of attempts we will just throw the // exception back out and let the developer handle an uncaught exceptions. catch (Exception$e) { $this->handleTransactionException( $e, $currentAttempt, $attempts ); } catch (Throwable$e) { $this->rollBack();
/** * Class ContextHandler * @package App\Foundation\Handlers */ classContextHandler { /** * Example of the current class * @var $this ; */ protectedstatic$instance; /** * Data store * @var array */ protectedstatic$data = [];
/** * The protected constructor prohibits the creation of an * instance of the current class * * ContextHandler constructor. */ protectedfunction__construct() { }
/** * The protected clone magic method forbids the current class * from being cloned */ protectedfunction__clone() { }
/** * Returns an instance of the current class * * @return $this; */ publicstaticfunctiongetInstance(): ContextHandler { if (!static::$instance) { static::$instance = newself(); }
returnstatic::$instance; }
/** * Set a data to current class * * @param string $key * @param mixed $value * * @return mixed */ publicfunctionset(string$key, $value) { if ($valueinstanceofClosure) { $value = $value(); }
/** * Get a data by current class * * @param string|array $key * @param null|mixed $default * * @return mixed */ publicfunctionget($key, $default = null) { return Arr::get(static::$data, $key, $default); }
/** * Gets part of the specified data from the data set * * @param array $keys * * @return array */ publicfunctiononly(array$keys): array { return Arr::only(static::$data, $keys); }
/** * Determine if there is any data in the data set * * @param string $key * * @return bool */ publicfunctionhas(string$key): bool { return array_key_exists($key, static::$data); }
/** * If $data contains the data that needs to be retrieved, * return it directly If there is no corresponding * access method * * @param string $key * * @return mixed|null */ publicfunction__get(string$key) { if ($this->has($key)) { return$this->get($key); }
if (method_exists($this, $method)) { $value = $this->$method();
return$this->set($key, $value); }
returnnull; }
/** * Gets the subscript of the data set based on the method name * Determine whether there is data in the data set * Calls private or protected methods when data does not exist * And stored in the data set for reuse * * @param string $method * @param array $parameters * * @return mixed * @throws Exception */ publicfunction__call(string$method, array$parameters) { $key = Str::snake($method);
if ($this->has($key)) { return$this->get($key); }
if (!method_exists($this, $method)) { $message = sprintf('Method %s::%s does not exist.', static::class, $method); thrownewException($message, Response::HTTP_INTERNAL_SERVER_ERROR); }
$value = $this->$method(...$parameters);
return$this->set($key, $value); }
/** * Gets the routing alias for this request * * @return mixed|string */ protectedfunctiongetRouteNameAttribute() { $route_params = Request::route() ?: []; $route_name = '';
useApp\Foundation\Facades\Context; useIlluminate\Support\Str; use \Exception;
/** * Class ClassesStorageHandler * @package App\Foundation\Handlers; */ classClassesStorageHandler { /** * A namespace for the singleton class group is required * * @var string */ protected$namespace;
/** * A suffix for the singleton class group is required * * @var string */ protected$suffix;
classRequestextendsIlluminateRequestimplementsValidatesWhenResolved { useValidatesWhenResolvedTrait; /** * The container instance. * * @var \Illuminate\Container\Container */ protected$container; /** * The redirector instance. * * @var \Laravel\Lumen\Http\Redirector */ protected$redirector; /** * The route to redirect to if validation fails. * * @var string */ protected$redirectRoute; /** * The controller action to redirect to if validation fails. * * @var string */ protected$redirectAction; /** * The key to be used for the view error bag. * * @var string */ protected$errorBag = 'default'; /** * The input keys that should not be flashed on redirect. * * @var array */ protected$dontFlash = ['password', 'password_confirmation'];
/** * Get the validator instance for the request. * * @return \Illuminate\Contracts\Validation\Validator */ protectedfunctiongetValidatorInstance() { $factory = $this->container->make(ValidationFactory::class); if (method_exists($this, 'validator')) { return$this->container->call([$this, 'validator'], compact('factory')); }
/** * Get the proper failed validation response for the request. * * @param array $errors * * @return \Illuminate\Http\JsonResponse */ publicfunctionresponse(array$errors) { returnnew JsonResponse($errors, 422); }
/** * Get the response for a forbidden operation. * * @return \Illuminate\Http\Response */ publicfunctionforbiddenResponse() { returnnew Response('Forbidden', 403); }
/** * Format the errors from the given Validator instance. * * @param \Illuminate\Contracts\Validation\Validator $validator * * @return array */ protectedfunctionformatErrors(Validator $validator) { return$validator->getMessageBag()->toArray(); }