Installation
composer require aperturelabo/core-bundle
Register the bundle in config/bundles.php:
return [
// ...
ApertureLabo\CoreBundle\ApertureCoreBundle::class => ['all' => true],
];
First Use Case: Asset Management
The bundle provides a LiipImagineBundle wrapper for image processing. Start by configuring it in config/packages/liip_imagine.yaml:
liip_imagine:
driver: gd
filter_sets:
thumbnail:
quality: 75
filters:
thumbnail: { size: [200, 200], mode: outbound }
Use the service in a controller:
use ApertureLabo\CoreBundle\Service\ImageService;
class ImageController extends AbstractController
{
public function resize(ImageService $imageService, string $path): Response
{
return $imageService->resize($path, 'thumbnail');
}
}
Doctrine ORM Integration The bundle includes common Doctrine entities. Extend them in your project:
use ApertureLabo\CoreBundle\Entity\BaseEntity;
#[ORM\Entity]
class CustomEntity extends BaseEntity
{
// Your custom fields
}
Upload Assets
Use Symfony’s UploadedFile with a custom validator (e.g., ApertureLabo\CoreBundle\Validator\FileType):
#[Assert\File(mimeTypes: ['image/jpeg', 'image/png'])]
#[Assert\File(maxSize: '1024k')]
private ?UploadedFile $image;
Process Assets
Chain ImageService with LiipImagineBundle:
$imageService->resize($filePath, 'thumbnail')
->optimize('webp');
Store Metadata Attach metadata to processed assets via Doctrine:
$asset = new Asset();
$asset->setOriginalPath($originalPath)
->setProcessedPath($processedPath)
->setMetadata(['width' => 200, 'height' => 200]);
$entityManager->persist($asset);
symfony/yaml for dynamic config loading:
$config = Yaml::parseFile(__DIR__.'/config/imagine.yaml');
$imageService->setConfig($config);
AssetProcessedEvent) for post-processing:
public function onAssetProcessed(AssetProcessedEvent $event): void
{
$event->getAsset()->setProcessedAt(new \DateTime());
}
ImageService and EntityManager:
public function __construct(
private ImageService $imageService,
private EntityManagerInterface $em
) {}
LiipImagineBundle Dependencies
gd or imagick PHP extensions are installed. Debug with:
php -m | grep -E 'gd|imagick'
Dockerfile:
RUN docker-php-ext-install gd
Doctrine Entity Inheritance
BaseEntity methods unless necessary. Use traits for reusable logic:
trait SoftDeletable {
public function isDeleted(): bool { /* ... */ }
}
YAML Parsing Quirks
Yaml::parse() for arrays, Yaml::parseFile() for files. Unescaped keys may cause issues:
# ❌ Fails (unquoted key with special chars)
filter_sets:
thumbnail@2x: { ... }
# ✅ Works
filter_sets:
"thumbnail@2x": { ... }
Image Processing Errors
Check LiipImagineBundle logs in var/log/dev.log for filter failures. Validate input paths:
if (!file_exists($path)) {
throw new \RuntimeException("File not found: {$path}");
}
Doctrine Events Disable listeners during tests:
$this->entityManager->getEventManager()->removeEventListeners();
Custom Filters
Extend ImageService to add filters:
class CustomImageService extends ImageService
{
public function addWatermark(string $path, string $watermarkPath): string
{
// Implement logic
}
}
Register as a service in services.yaml:
services:
App\Service\CustomImageService:
decorates: aperturelabo.core.image_service
Asset Metadata
Add custom metadata fields to Asset entity:
#[ORM\Column(type: 'json', nullable: true)]
private ?array $customMetadata = [];
Use json type for flexibility in Doctrine 2.10+.
How can I help you explore Laravel packages today?