- How do I install this package in a Laravel project?
- Run `composer require spatie/workshop-unit-conversions` in your project root. The package has no Laravel-specific dependencies, so it integrates cleanly with Laravel’s service container. Register it as a singleton in `AppServiceProvider` or use Laravel’s autowiring for dependency injection.
- Which Laravel and PHP versions does this package support?
- The package is tested on PHP 7.4–8.0 and likely works with Laravel 7+. However, since the last update was in 2021, compatibility with PHP 8.2+ or Laravel 10+ isn’t guaranteed. Test thoroughly in a staging environment if using newer versions.
- Can I extend the package to support custom units (e.g., cryptocurrency or niche measurements)?
- Yes, the package allows custom unit definitions via configuration. Publish the config using `php artisan vendor:publish` and add your units in the `units.php` file. This makes it flexible for specialized use cases beyond the default length, weight, and temperature conversions.
- How should I handle invalid unit inputs (e.g., 'lightyears') in production?
- The package throws exceptions for invalid units. Catch these exceptions in your application logic and handle them gracefully, such as logging the error or returning a user-friendly message. For critical systems, validate inputs before passing them to the converter.
- Is this package actively maintained? Should I consider alternatives like `league/unit`?
- The package hasn’t been updated since 2021 and has minimal adoption. If maintenance is a concern, consider alternatives like `league/unit`, which is more actively maintained. Alternatively, fork the package or build a custom solution if your needs are highly specific.
- How do I integrate this into a Laravel API for consistent unit normalization?
- Inject the `UnitConverter` into your API service classes via Laravel’s dependency injection. Use it to normalize incoming requests (e.g., convert imperial units to metric) or outgoing responses (e.g., standardize product dimensions). Example: `$converter->convert($request->weight, 'pounds', 'kilograms')`.
- Will this package work well in high-throughput systems (e.g., e-commerce checkouts)?
- The package is lightweight, but performance depends on usage. Avoid converting units in tight loops without caching results. For high-throughput systems, benchmark the package or consider caching converted values in Redis or the application cache.
- How do I test this package in my Laravel application?
- Run `composer test` to execute the package’s test suite. For integration testing, mock the `UnitConverter` in your Laravel tests using Laravel’s testing helpers. Validate edge cases like invalid units, zero values, and compound conversions (e.g., speed units).
- Can I use this package in a microservice architecture for unit consistency across services?
- Yes, the package’s simplicity makes it ideal for microservices. Deploy it as a shared library or include it in each service’s dependencies. Use it to enforce consistent unit handling across APIs, ensuring data integrity when services exchange measurements.
- What’s the best way to migrate from hardcoded unit conversions to this package?
- Start by replacing hardcoded logic in models or services with the package’s methods. Use Laravel’s IDE helper (`php artisan ide-helper:generate`) to autowire the converter. Test incrementally in non-production environments, focusing on high-impact areas like checkout flows or inventory systems.