Installation:
composer require ekyna/install-bundle
Add to config/app.php under providers:
Ekyna\InstallBundle\InstallBundle::class,
First Use Case:
Create a basic installer by implementing InstallerInterface in a bundle service:
namespace App\Installers;
use Ekyna\InstallBundle\Installer\InstallerInterface;
class ExampleInstaller implements InstallerInterface
{
public function install()
{
// Your installation logic (e.g., migrations, config generation)
return true;
}
}
Register it as a service in your bundle’s Resources/config/services.yml:
services:
app.example_installer:
class: App\Installers\ExampleInstaller
tags:
- { name: ekyna.installer }
Run Installers:
php artisan ekyna:install
Pre-Install Hooks:
Extend InstallerInterface to support preInstall() for setup tasks:
public function preInstall()
{
// Validate environment or dependencies
}
Post-Install Actions:
Use postInstall() for cleanup or notifications:
public function postInstall()
{
// Log completion or trigger events
}
Conditional Execution:
Implement shouldInstall() to skip installers when unnecessary:
public function shouldInstall()
{
return !file_exists($this->configPath);
}
Dependency Management:
Chain installers via depends tag in services.yml:
tags:
- { name: ekyna.installer, depends: { "app.migration_installer" } }
Order Dependency:
Installers run in arbitrary order unless explicitly tagged with depends. Use postInstall() for post-dependency tasks.
State Management:
Avoid side effects in install()—use shouldInstall() to check for prior runs (e.g., via config files or DB flags).
Error Handling:
Installers must return true on success. Silent failures may go unnoticed. Log errors explicitly:
try {
// Risky operations
} catch (\Exception $e) {
\Log::error("Installer failed: " . $e->getMessage());
return false;
}
Bundle Activation:
Installers only run for active bundles. Verify bundle is enabled in config/bundles.php:
Ekyna\InstallBundle\InstallBundle::class => ['all' => true],
protected function execute(InputInterface $input, OutputInterface $output)
{
$installers = $this->getInstallers();
foreach ($installers as $installer) {
$output->writeln("<comment>Would run:</comment> " . get_class($installer));
}
}
$output->writeln("<info>Running:</info> " . $installer::class);
Custom Commands:
Extend the base command to add flags (e.g., --force, --dry-run):
protected function getOptions()
{
return [
new \Symfony\Component\Console\Input\InputOption('force', 'f', InputOption::VALUE_NONE, 'Force installation'),
];
}
Event Dispatching:
Trigger events (e.g., installer.run) before/after execution using Symfony’s EventDispatcher:
$dispatcher->dispatch(new InstallerEvent($installer), 'installer.run');
Configuration:
Load installer-specific configs via config/installers.php:
$config = $this->container->getParameter('installer.example_config');
How can I help you explore Laravel packages today?