20steps/commons-ensure-bundle
assert() (which can be disabled), this bundle’s checks are always active, improving reliability in production.sprintf-formatted exceptions improve debugging by providing context-specific failure reasons.Ensure::notNull($var, 'Variable %s must not be null', $var)), requiring no complex setup beyond installation.symfony/dependency-injection v3.x), which may conflict with Laravel’s newer dependencies.Illuminate\Support\Facades\Validator).App\Exceptions\Handler)?symfony/flex or manual bootstrapping. It can be integrated as a standalone service provider or via Laravel’s config/app.php.Ensure::greaterThan($user->age, 18)).Ensure::array($request->input('tags'))).assert() calls or if checks with Ensure::* methods.config/app.php:
'providers' => [
// ...
Twentysteps\Commons\EnsureBundle\TwentystepsCommonsEnsureBundle::class,
],
Ensure for logic, Validator for input).Illuminate\Validation\ValidationException if needed.strict_types=1 (enabled in Laravel by default).foreach with references).symfony/console). Use composer why symfony/dependency-injection to audit.if checks in services with Ensure::* methods.// Before
if (empty($request->input('email'))) {
throw new \InvalidArgumentException('Email is required');
}
// After
Ensure::notEmpty($request->input('email'), 'Email %s is required', 'email');
EnsureException to leverage Laravel’s error formatting:
class CustomEnsureException extends \Twentysteps\Commons\EnsureBundle\Exception\EnsureException
{
public function render($request)
{
return response()->json(['error' => $this->getMessage()], 400);
}
}
Ensure::* methods.Ensure for domain logic, not input validation").EnsureException occurrences in Sentry/LogRocket to identify validation hotspots.Ensure::greaterThan($stock, 0).Ensure::exists()).Ensure::exists() with Eloquent models, ensure queries are optimized (e.g., avoid N+1 issues).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid precondition in API | 500 errors, failed transactions | Use in middleware or Form Requests. |
| Postcondition violation in service | Inconsistent data state | Log failures; implement rollback logic. |
| Bundle compatibility issues | Application crashes | Fork and update dependencies. |
| Exception handling misconfiguration | Poor UX (e.g., 500 instead of 400) | Extend EnsureException for Laravel. |
| Performance degradation | Slow responses in high-traffic paths | Profile and optimize critical checks. |
How can I help you explore Laravel packages today?