baks-dev/reference-color
Библиотека справочника цветов для проектов BaksDev: подключение через Composer, сервис ReferenceChoiceColor для вывода цветов в выпадающих списках (tag baks.reference.choice). Требует PHP 8.4+. Лицензия MIT.
Installation
composer require baks-dev/reference-color
Verify PHP 8.4+ compliance in your php -v output.
Basic Configuration
Create config/packages/reference.php with the provided Symfony DI snippet:
<?php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use BaksDev\Reference\Color\Choice\ReferenceChoiceColor;
return static function (ContainerConfigurator $configurator) {
$services = $configurator->services()
->defaults()
->autowire(true)
->autoconfigure(true);
$services->set(ReferenceChoiceColor::class)->tag('baks.reference.choice');
};
First Use Case: Color Dropdown
Inject ReferenceChoiceColor into a Laravel controller or service:
use BaksDev\Reference\Color\Choice\ReferenceChoiceColor;
class AdminController extends Controller {
public function __construct(private ReferenceChoiceColor $colorChoice) {}
public function showColorPicker() {
$colors = $this->colorChoice->getColors(); // Hypothetical method
return view('admin.colors', compact('colors'));
}
}
Render in Blade:
<select>
@foreach($colors as $color)
<option value="{{ $color->hex }}">{{ $color->name }}</option>
@endforeach
</select>
Centralized Color Management Define all colors in a single place (likely via the package’s configuration) and reuse across:
{{ $color->hex }}).return response()->json(['color' => $color->hex])).Dynamic UI Components
Use the ReferenceChoiceColor service to populate:
// Livewire component
public $selectedColor;
public function updatedSelectedColor() {
// Trigger UI updates (e.g., dark/light mode toggle)
}
Accessibility Checks
Leverage hypothetical methods (e.g., getContrastRatio()) to validate:
if ($color->getContrastRatio($backgroundColor) < 4.5) {
throw new \InvalidArgumentException("Color fails contrast ratio.");
}
public function testColorContrast() {
$this->assertGreaterThan(4.5, $color->getContrastRatio('#ffffff'));
}
Theming Systems Implement multi-tenancy or user-preference-driven themes:
// Pseudocode: Theme service using ReferenceChoiceColor
class ThemeService {
public function getThemeColors(User $user) {
return $this->colorChoice->filterByTag($user->preferredTheme);
}
}
Color Definition Workflow
ReferenceChoiceColor) to load these colors.Release Workflow
config/colors.php + package).git tag -a v1.2.0 -m "Updated primary color to #4a6fa5"
Frontend-Backend Sync
Route::get('/api/colors', function () {
return response()->json($this->colorChoice->getColors());
});
<style>
:root {
--primary: {{ $colors['primary']->hex }};
}
</style>
Laravel Service Provider Wrap the package in a Laravel provider to abstract Symfony DI:
// app/Providers/ColorServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use BaksDev\Reference\Color\Choice\ReferenceChoiceColor;
class ColorServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton(ReferenceChoiceColor::class);
}
}
Caching Cache color references to avoid repeated loads:
public function getColors() {
return Cache::remember('color.palette', now()->addHours(1), function () {
return $this->colorChoice->getColors();
});
}
Testing Mock the service in PHPUnit:
$mockColors = collect([new Color('red', '#ff0000')]);
$this->mock(ReferenceChoiceColor::class)->shouldReceive('getColors')->andReturn($mockColors);
Database Integration If colors are stored in a database, use Eloquent:
class Color extends Model {
protected $casts = ['hex' => 'string'];
}
Then bind the package to the model:
$this->colorChoice->setColors(Color::all());
Symfony DI in Laravel
ContainerConfigurator, which may conflict with Laravel’s container.Undocumented API
getColors() or getContrastRatio() are not documented in the README.ReferenceChoiceColor class or use PHPStan to infer usage:
vendor/bin/phpstan analyse --level 5
PHP 8.4+ Dependency
nikic/php-parser for attribute support).No Frontend Integration
Tagging System
baks.reference.choice tag suggests the package is designed for specific use cases (e.g., dropdowns).class ExtendedColorChoice extends ReferenceChoiceColor {
public function getColorByName(string $name) {
return $this->colors->firstWhere('name', $name);
}
}
No Color Space Conversions
php-color) for advanced use cases.Service Not Found
Class ReferenceChoiceColor not found.php artisan container:list | grep ReferenceChoiceColor
AppServiceProvider or the Symfony config.Empty Color List
$colorChoice->getColors() returns an empty collection.$colors = $this->colorChoice->getColors()->isEmpty()
? collect([new Color('default', '#000000')])
: $this->colorChoice->getColors();
Symfony DI Errors
Call to undefined method ContainerConfigurator::....dependency-injection package is installed:
composer require symfony/dependency-injection
extend() to adapt the container:
$this->app->extend(ReferenceChoiceColor::class, function () {
return new ReferenceChoiceColor();
});
config/packages/reference.php file is required for the service to work.config/packages/ and ensure it’s loaded in config/app.php:
'providers' => [
// ...
How can I help you explore Laravel packages today?