aldaflux/swissarmyknife-bundle
Installation Add the bundle via Composer:
composer require aldaflux/swissarmyknife-bundle
Enable it in config/bundles.php:
return [
// ...
AldaFlux\SwissArmyKnifeBundle\AldaFluxSwissArmyKnifeBundle::class => ['all' => true],
];
First Use Case: Image Optimization
The bundle provides a ImageOptimizer service. Inject it into a controller or service:
use AldaFlux\SwissArmyKnifeBundle\Service\ImageOptimizer;
class MyController extends AbstractController
{
public function optimizeImage(ImageOptimizer $optimizer)
{
$path = $this->getParameter('kernel.project_dir') . '/public/uploads/image.jpg';
$optimizer->optimize($path);
return new Response('Image optimized!');
}
}
First Use Case: Documentation Generation
Use the DocGenerator service to generate documentation from Twig templates:
use AldaFlux\SwissArmyKnifeBundle\Service\DocGenerator;
class MyController extends AbstractController
{
public function generateDocs(DocGenerator $generator)
{
$output = $generator->generate('path/to/templates', 'path/to/output');
return new Response($output);
}
}
Image Handling
ImageOptimizer for resizing with predefined or custom dimensions:
$optimizer->resize($path, 800, 600);
$optimizer->compress($path, 80); // 80% quality
foreach ($imagePaths as $path) {
$optimizer->optimize($path);
}
Documentation
$data = ['title' => 'My API', 'endpoints' => [...]];
$generator->generate('templates/docs', 'public/docs', $data);
$markdown = file_get_contents('README.md');
$html = $generator->markdownToHtml($markdown);
Integration with Symfony
// src/EventListener/ImageUploadListener.php
class ImageUploadListener implements KernelEventListener
{
public function __construct(private ImageOptimizer $optimizer) {}
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMainRequest()) {
$this->optimizer->optimizeUploadedFiles($_FILES);
}
}
}
{{ asset(optimize_path(imagePath, 300)) }}
Register in services.yaml:
services:
AldaFlux\SwissArmyKnifeBundle\Twig\OptimizeExtension:
tags: ['twig.extension']
Configuration
config/packages/alda_flux_swiss_army_knife.yaml:
alda_flux_swiss_army_knife:
image_optimizer:
default_quality: 75
allowed_formats: ['jpg', 'png', 'webp']
doc_generator:
output_format: 'html'
PHP Version Compatibility
Missing Dependencies
symfony/framework-bundle:>=3.0, but some features (e.g., Twig extensions) may fail on older Symfony versions.composer.json aligns with Symfony 3.0+ requirements.Image Optimization Dependencies
ImageOptimizer likely relies on external tools like imagemagick or gd. Missing these will cause failures.sudo apt-get install imagemagick # Linux
brew install imagemagick # macOS
Twig Template Paths
DocGenerator assumes templates exist in specific paths. Hardcoded paths may break in custom setups.kernel.project_dir.No Clear Service Naming
debug:container to inspect service IDs:
php bin/console debug:container AldaFlux
Enable Debug Mode
Add this to config/packages/dev/alda_flux_swiss_army_knife.yaml:
alda_flux_swiss_army_knife:
debug: true
This may log additional info to var/log/dev.log.
Check for Exceptions Wrap service calls in try-catch blocks:
try {
$optimizer->optimize($path);
} catch (\Exception $e) {
$this->addFlash('error', 'Image optimization failed: ' . $e->getMessage());
}
Verify File Permissions Ensure the output directory for generated docs has write permissions:
chmod -R 775 public/docs
Custom Image Filters
Extend the ImageOptimizer by creating a decorator:
services:
app.image_optimizer.decorator:
decorates: alda_flux_swiss_army_knife.image_optimizer
arguments: ['@app.image_optimizer.decorator.inner']
Then implement logic in a custom service.
Add New Doc Formats
If the DocGenerator lacks support for a format (e.g., PDF), create a new service and integrate it via Symfony’s compiler passes.
Override Twig Extensions
Replace or extend existing Twig extensions by redefining them in services.yaml:
services:
AldaFlux\SwissArmyKnifeBundle\Twig\OptimizeExtension:
class: App\Twig\CustomOptimizeExtension
tags: ['twig.extension']
Default Values
The bundle may use hardcoded defaults (e.g., default_quality: 80). Override them explicitly in your config to avoid surprises.
Caching
If the DocGenerator caches output, clear the cache after updates:
php bin/console cache:clear
Environment-Specific Settings
Use %kernel.environment% to conditionally load configs:
alda_flux_swiss_army_knife:
image_optimizer:
default_quality: %env(int:IMAGE_QUALITY, 75)%
How can I help you explore Laravel packages today?