Installation:
composer require aqarmap/utility-bundle
Register the bundle in config/bundles.php:
return [
// ...
Aqarmap\UtilityBundle\AqarmapUtilityBundle::class => ['all' => true],
];
First Use Case:
use Aqarmap\UtilityBundle\Helper\GravatarHelper;
$gravatarHelper = $this->get('aqarmap_utility.gravatar_helper');
$url = $gravatarHelper->getUrl('user@example.com', 50); // 50px image
use Aqarmap\UtilityBundle\Helper\StringHelper;
$stringHelper = $this->get('aqarmap_utility.string_helper');
$truncated = $stringHelper->truncate('Long text here...', 10);
GravatarHelper: For avatar generation.StringHelper: For text manipulation (truncate, slugify, etc.).ArrayHelper: For array operations (flatten, merge, etc.).FileHelper: For file/directory operations.Service Injection: Inject utilities via constructor or setter:
public function __construct(GravatarHelper $gravatarHelper) {
$this->gravatarHelper = $gravatarHelper;
}
Or fetch via Symfony’s container:
$this->get('aqarmap_utility.string_helper');
Twig Integration: Extend Twig with custom filters/extensions:
{{ 'Hello World'|aqarmap_truncate(10) }}
Register in services.yaml:
twig:
globals:
aqarmap_utility: '@aqarmap_utility.twig_extension'
Command-Line Utilities: Use helpers in console commands:
use Aqarmap\UtilityBundle\Helper\FileHelper;
public function handle(FileHelper $fileHelper) {
$fileHelper->createDirectory('path/to/dir');
}
Event Listeners/Subscribers: Leverage utilities in event logic:
public function onKernelRequest(GetResponseEvent $event) {
$slug = $this->get('aqarmap_utility.string_helper')->slugify('User Name');
}
$this->get() for testability.config/packages/aqarmap_utility.yaml (if supported).Outdated Codebase:
symfony/dependency-injection changes).Gravatar Rate Limits:
$cache = $this->get('cache.app');
$url = $cache->get("gravatar_{$email}", function() use ($gravatarHelper, $email) {
return $gravatarHelper->getUrl($email);
});
StringHelper Quirks:
slugify() may not handle all Unicode characters perfectly. Test edge cases (e.g., é, ü).truncate() uses ... by default—customize via method parameters:
$stringHelper->truncate('text', 10, ['append' => ' [+]']);
FileHelper Permissions:
createDirectory()) may fail due to server permissions.isWritable() to check paths before operations.$this->logger->debug('Gravatar URL', ['url' => $url]);
phpstan or psalm to detect unsafe usage.Custom Helpers: Extend existing helpers by subclassing and overriding methods:
class CustomStringHelper extends StringHelper {
public function customTruncate($string, $length) {
return parent::truncate($string, $length) . ' [Custom]';
}
}
Register the new service in services.yaml:
aqarmap_utility.string_helper:
class: App\Helper\CustomStringHelper
Add New Utilities: Create a custom bundle or service to wrap additional logic, then integrate with the existing pattern.
Twig Extensions:
Add more filters/extensions by extending Aqarmap\UtilityBundle\Twig\TwigExtension:
class CustomTwigExtension extends TwigExtension {
public function getFilters() {
return [
new TwigFilter('custom_filter', [$this, 'customFilter']),
];
}
}
How can I help you explore Laravel packages today?