darvinstudio/darvin-fileman-bundle
Installation:
composer require darvinstudio/darvin-fileman-bundle
Register the bundle in config/bundles.php (Symfony 4.3+):
return [
// ...
Darvin\DatabaserBundle\DarvinFilemanBundle::class => ['all' => true],
];
First Use Case: Pull files from a remote server to your local Symfony project:
php bin/console fileman:pull user@example.com:/remote/path /local/path
user@example.com with the remote server credentials./remote/path with the remote directory containing files./local/path with your local Symfony project’s uploads directory (e.g., public/uploads).Configuration:
Check config/packages/darvin_fileman.yaml (auto-generated) for default settings. Override as needed:
darvin_fileman:
default_port: 22 # SSH default port
timeout: 30 # Connection timeout in seconds
File Synchronization:
fileman:pull to sync files from a staging server to your local environment before testing.
php bin/console fileman:pull --key=/path/to/ssh_key user@staging.example.com:/uploads public/uploads
php bin/console fileman:push --password user@prod.example.com:/uploads public/uploads
Environment-Specific Paths:
Store remote/local paths in .env for consistency:
REMOTE_UPLOADS_PATH=/var/www/uploads
LOCAL_UPLOADS_PATH=public/uploads
Reference them in commands:
php bin/console fileman:pull user@example.com:$REMOTE_UPLOADS_PATH $LOCAL_UPLOADS_PATH
Integration with Symfony Events:
Trigger file syncs post-deployment using Symfony’s kernel.terminate event:
// src/EventListener/FileSyncListener.php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class FileSyncListener
{
public function onTerminate(TerminateEvent $event)
{
if ($event->getRequest()->getPathInfo() === '/deploy') {
shell_exec('php bin/console fileman:push user@prod.example.com:/uploads public/uploads');
}
}
}
Register the listener in config/services.yaml:
services:
App\EventListener\FileSyncListener:
tags:
- { name: kernel.event_listener, event: kernel.terminate }
Batch Processing:
Use --verbose to log progress and debug:
php bin/console fileman:pull -v user@example.com:/large/dir public/large_dir
For large directories, add --parameters="--exclude='*.log'" to skip unnecessary files.
SSH Key Authentication:
chmod 600 /path/to/ssh_key
--key to avoid permission errors.Path Permissions:
php bin/console fileman:pull user@example.com:/test public/test
www-data):
chown -R www-data:www-data public/uploads
Firewall/Network Issues:
--port to specify a custom port:
php bin/console fileman:push -P 2222 user@example.com:/uploads public/uploads
ssh -p 2222 user@example.com "ls /uploads"
File Overwrites:
--parameters="--delete" to remove local files not present remotely (use with caution):
php bin/console fileman:pull --parameters="--delete" user@example.com:/uploads public/uploads
Dry Runs:
Use --verbose to log actions without executing them:
php bin/console fileman:pull -vv user@example.com:/uploads public/uploads
Error Logs:
Check Symfony’s var/log/dev.log for SSH connection errors or file permission issues.
Timeouts:
Increase the timeout in config/packages/darvin_fileman.yaml for slow connections:
darvin_fileman:
timeout: 60
Custom Commands:
Extend the bundle by creating a custom command (e.g., fileman:sync):
// src/Command/SyncCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncCommand extends Command
{
protected static $defaultName = 'fileman:sync';
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Pulling files...');
shell_exec('php bin/console fileman:pull user@example.com:/uploads public/uploads');
$output->writeln('Pushing files...');
shell_exec('php bin/console fileman:push user@example.com:/uploads public/uploads');
return Command::SUCCESS;
}
}
Pre/Post-Sync Hooks:
Use Symfony’s kernel.terminal event to run scripts before/after sync:
// src/EventListener/SyncHookListener.php
namespace App\EventListener;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SyncHookListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
ConsoleEvents::TERMINATE => 'onTerminate',
];
}
public function onTerminate(ConsoleTerminateEvent $event)
{
$command = $event->getCommand()->getName();
if (strpos($command, 'fileman:') === 0) {
// Run post-sync tasks (e.g., optimize images)
shell_exec('php bin/console app:optimize-images');
}
}
}
Configuration Overrides: Dynamically override settings per environment using Symfony’s parameter bag:
# config/packages/dev/darvin_fileman.yaml
darvin_fileman:
timeout: 120 # Longer timeout for dev
How can I help you explore Laravel packages today?