bisonlab/takardun-bundle
TakardunBundle is a specialised document handling system for Symfony apps. Link documents to any entity, add new documents, and fetch listings via the takardun service from your controllers or templates. Designed for embedding in other applications, usable standalone.
Installation
composer require bisonlab/takardun-bundle
Register the bundle in config/bundles.php (Symfony 5+):
return [
// ...
BisonLab\TakardunBundle\TakardunBundle::class => ['all' => true],
];
Database Migration Run migrations to create the required tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Basic Usage
Inject the TakardunService in your controller:
use BisonLab\TakardunBundle\Service\TakardunService;
class DocumentController extends AbstractController
{
public function __construct(private TakardunService $takardunService) {}
public function index(): Response
{
$documents = $this->takardunService->getDocumentsForEntity('App\Entity\User', 1);
return $this->render('document/index.html.twig', ['documents' => $documents]);
}
}
First Use Case
Attach a document to an entity (e.g., User with ID 1):
$this->takardunService->addDocument(
'App\Entity\User',
1,
'/path/to/document.pdf',
'application/pdf',
'User Contract'
);
Entity-Based Document Management
getDocumentsForEntity($entityClass, $entityId) to fetch documents tied to any entity.Order, Project, or custom entities.Template Integration
{% for document in documents %}
<a href="{{ document.url }}">{{ document.name }}</a>
{% endfor %}
Upload Handling
config/packages/takardun.yaml):
takardun:
upload_dir: '%kernel.project_dir%/public/uploads/documents'
addDocument() with file paths or streams for dynamic uploads.Bulk Operations
$documents = [
['path' => '/doc1.pdf', 'mime' => 'application/pdf', 'name' => 'Doc 1'],
['path' => '/doc2.pdf', 'mime' => 'application/pdf', 'name' => 'Doc 2'],
];
$this->takardunService->addDocuments('App\Entity\User', 1, $documents);
Event-Driven Triggers
DocumentAddedEvent) to log or notify:
// config/services.yaml
BisonLab\TakardunBundle\EventListener\DocumentLogger:
tags:
- { name: kernel.event_listener, event: document.added, method: onDocumentAdded }
Symfony Forms
Use TakardunType for file upload forms:
$builder->add('document', TakardunType::class, [
'label' => 'Attach Document',
'entity_class' => 'App\Entity\User',
'entity_id' => $user->getId(),
]);
API Endpoints
Expose document endpoints with getDocumentsForEntity():
#[Route('/api/users/{id}/documents', methods: ['GET'])]
public function getUserDocuments(User $user): JsonResponse
{
$documents = $this->takardunService->getDocumentsForEntity(
'App\Entity\User',
$user->getId()
);
return $this->json($documents);
}
Custom Metadata
Extend the Document entity to add fields (e.g., isConfidential):
// src/Entity/Document.php
#[ORM\Column(type: 'boolean')]
private bool $isConfidential = false;
File Permissions
upload_dir is writable by the web server:
chmod -R 775 %kernel.project_dir%/public/uploads/documents
var/log/dev.log for Permission denied errors.Entity Class Names
'App\Entity\User') to getDocumentsForEntity().get_class($entity) to avoid typos.MIME Type Validation
fileinfo for dynamic detection:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filePath);
finfo_close($finfo);
Circular Dependencies
User ↔ Document). Use entity_id and entity_class instead of direct relations.Large Files
$this->takardunService->addDocument(
'App\Entity\User',
1,
fopen('/large-file.pdf', 'r'),
'application/pdf',
'Large File'
);
Enable SQL Logging
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Check Events
document.added/document.removed to debug lifecycle:
public function onDocumentAdded(DocumentAddedEvent $event): void
{
error_log('Document added: ' . $event->getDocument()->getName());
}
Common Errors
entity_class and entity_id exist in the database./var/www/html/public/uploads/).Custom Storage
Override the storage adapter by implementing BisonLab\TakardunBundle\Storage\StorageInterface:
class S3Storage implements StorageInterface
{
public function save(string $path, string $content): string
{
// Upload to S3
}
}
Register in services.yaml:
BisonLab\TakardunBundle\Service\TakardunService:
arguments:
$storage: '@app.s3_storage'
Validation Rules
Extend TakardunType to add constraints:
class CustomTakardunType extends TakardunType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'constraints' => [
new File([
'maxSize' => '10M',
'mimeTypes' => ['application/pdf'],
]),
],
]);
}
}
Twig Extensions Add custom filters for document URLs:
// src/Twig/AppExtension.php
class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('document_url', [$this->takardunService, 'getDocumentUrl']),
];
}
}
Usage in Twig:
<a href="{{ document|document_url }}">{{ document.name }}</a>
Async Processing Use Symfony Messenger to handle document processing asynchronously:
$this->messageBus->dispatch(new ProcessDocumentMessage(
'App\Entity\User',
1,
$filePath,
$mime,
$name
));
How can I help you explore Laravel packages today?